Skip to content

Commit 2687c69

Browse files
committed
[tls] Add annotations for duration and renewBefore to issuers
To help what should be the duration and/or renewBefore for a cert being issued, we add annotations to the issuer. - if no duration annotation is set, use the default from certmanager lib-common module, - if no renewBefore annotation is set, the cert-manager default is used Depends-On: openstack-k8s-operators/lib-common#506
1 parent 79cec7c commit 2687c69

5 files changed

Lines changed: 162 additions & 8 deletions

File tree

‎apis/core/v1beta1/openstackcontrolplane_types.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,3 +922,21 @@ func (instance OpenStackControlPlane) GetLibvirtIssuer() string {
922922

923923
return LibvirtCaName
924924
}
925+
926+
// GetDurationHours - returns the duration in hours
927+
func (c CertConfig) GetDurationHours() string {
928+
if c.Duration != nil {
929+
return c.Duration.Duration.String()
930+
}
931+
932+
return ""
933+
}
934+
935+
// GetRenewBeforeHours - returns the renewBefore in hours
936+
func (c CertConfig) GetRenewBeforeHours() string {
937+
if c.RenewBefore != nil {
938+
return c.RenewBefore.Duration.String()
939+
}
940+
941+
return ""
942+
}

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/openstack-k8s-operators/infra-operator/apis v0.3.1-0.20240429104248-25176c735750
2323
github.com/openstack-k8s-operators/ironic-operator/api v0.3.1-0.20240430114001-aa12ec2b2ba4
2424
github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20240429164853-7e1e3b111ee9
25-
github.com/openstack-k8s-operators/lib-common/modules/certmanager v0.0.0-20240429052447-09a614506ca6
25+
github.com/openstack-k8s-operators/lib-common/modules/certmanager v0.0.0-20240522132119-0d829ccc3fd2
2626
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6
2727
github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20240429052447-09a614506ca6
2828
github.com/openstack-k8s-operators/manila-operator/api v0.3.1-0.20240430085856-68c83fbc3041

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ github.com/openstack-k8s-operators/ironic-operator/api v0.3.1-0.20240430114001-a
116116
github.com/openstack-k8s-operators/ironic-operator/api v0.3.1-0.20240430114001-aa12ec2b2ba4/go.mod h1:aKCIsV3tadtnhr5iGca7/k+KnSI5ORFUt0aEAPFYQi0=
117117
github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20240429164853-7e1e3b111ee9 h1:aS7xUxC/uOXfw0T4ARTu0G1qb6eJ0WnB2JRv9donPOE=
118118
github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20240429164853-7e1e3b111ee9/go.mod h1:Y/ge/l24phVaJb9S8mYRjtnDkohFkX/KEOUXLArcyvQ=
119-
github.com/openstack-k8s-operators/lib-common/modules/certmanager v0.0.0-20240429052447-09a614506ca6 h1:z57GyTly+w61GhbwqFEOnrLwzA7be4ZRBGTki2O13HM=
120-
github.com/openstack-k8s-operators/lib-common/modules/certmanager v0.0.0-20240429052447-09a614506ca6/go.mod h1:ORxH/VVsk24ExdxecUiAWhLtV17xIv6lO89LLmSvKPE=
119+
github.com/openstack-k8s-operators/lib-common/modules/certmanager v0.0.0-20240522132119-0d829ccc3fd2 h1:SZ8Eo1aqVlzmHTj1GiTXIWxbxN5saDsDh8De0CCbsik=
120+
github.com/openstack-k8s-operators/lib-common/modules/certmanager v0.0.0-20240522132119-0d829ccc3fd2/go.mod h1:ORxH/VVsk24ExdxecUiAWhLtV17xIv6lO89LLmSvKPE=
121121
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6 h1:WLsG3Ko+phW5xZJjncypLWGASoLqKrt05qN9Zxsad6g=
122122
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6/go.mod h1:lYhFzul37AR/6gAhTAA1KKWbOlzB3F/7014lejn883c=
123123
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.3.1-0.20240429052447-09a614506ca6 h1:/mhzQQ9FF70z00zZD7dpgOoNXvEu9q68oob3oAiJW08=

‎pkg/openstack/ca.go‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
9898
instance.Status.TLS.CAList = []corev1.TLSCAStatus{}
9999
// create CA for ingress and public podLevel termination
100100
issuerLabels := map[string]string{certmanager.RootCAIssuerPublicLabel: ""}
101+
issuerAnnotations := getIssuerAnnotations(&instance.Spec.TLS.Ingress.Cert)
101102
if !instance.Spec.TLS.Ingress.Ca.IsCustomIssuer() {
102103
ctrlResult, err = ensureRootCA(
103104
ctx,
@@ -106,6 +107,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
106107
issuerReq,
107108
tls.DefaultCAPrefix+string(service.EndpointPublic),
108109
issuerLabels,
110+
issuerAnnotations,
109111
bundle,
110112
caOnlyBundle,
111113
instance.Spec.TLS.Ingress.Ca,
@@ -119,7 +121,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
119121
customIssuer := *instance.Spec.TLS.Ingress.Ca.CustomIssuer
120122

121123
// add CA labelselector to issuer
122-
caCertSecretName, err := addIssuerLabel(ctx, helper, customIssuer, instance.Namespace, issuerLabels)
124+
caCertSecretName, err := addIssuerLabelAnnotation(ctx, helper, customIssuer, instance.Namespace, issuerLabels, issuerAnnotations)
123125
if err != nil {
124126
instance.Status.Conditions.Set(condition.FalseCondition(
125127
corev1.OpenStackControlPlaneCAReadyCondition,
@@ -162,6 +164,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
162164

163165
// create CA for internal podLevel termination
164166
issuerLabels = map[string]string{certmanager.RootCAIssuerInternalLabel: ""}
167+
issuerAnnotations = getIssuerAnnotations(&instance.Spec.TLS.PodLevel.Internal.Cert)
165168
if !instance.Spec.TLS.PodLevel.Internal.Ca.IsCustomIssuer() {
166169
ctrlResult, err = ensureRootCA(
167170
ctx,
@@ -170,6 +173,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
170173
issuerReq,
171174
tls.DefaultCAPrefix+string(service.EndpointInternal),
172175
issuerLabels,
176+
issuerAnnotations,
173177
bundle,
174178
caOnlyBundle,
175179
instance.Spec.TLS.PodLevel.Internal.Ca,
@@ -182,7 +186,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
182186
} else {
183187
customIssuer := *instance.Spec.TLS.PodLevel.Internal.Ca.CustomIssuer
184188
// add CA labelselector to issuer
185-
caCertSecretName, err := addIssuerLabel(ctx, helper, customIssuer, instance.Namespace, issuerLabels)
189+
caCertSecretName, err := addIssuerLabelAnnotation(ctx, helper, customIssuer, instance.Namespace, issuerLabels, issuerAnnotations)
186190
if err != nil {
187191
instance.Status.Conditions.Set(condition.FalseCondition(
188192
corev1.OpenStackControlPlaneCAReadyCondition,
@@ -226,6 +230,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
226230

227231
// create CA for libvirt
228232
issuerLabels = map[string]string{certmanager.RootCAIssuerLibvirtLabel: ""}
233+
issuerAnnotations = getIssuerAnnotations(&instance.Spec.TLS.PodLevel.Libvirt.Cert)
229234
if !instance.Spec.TLS.PodLevel.Libvirt.Ca.IsCustomIssuer() {
230235
ctrlResult, err = ensureRootCA(
231236
ctx,
@@ -234,6 +239,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
234239
issuerReq,
235240
corev1.LibvirtCaName,
236241
issuerLabels,
242+
issuerAnnotations,
237243
bundle,
238244
caOnlyBundle,
239245
instance.Spec.TLS.PodLevel.Libvirt.Ca,
@@ -246,7 +252,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
246252
} else {
247253
customIssuer := *instance.Spec.TLS.PodLevel.Libvirt.Ca.CustomIssuer
248254
// add CA labelselector to issuer
249-
caCertSecretName, err := addIssuerLabel(ctx, helper, customIssuer, instance.Namespace, issuerLabels)
255+
caCertSecretName, err := addIssuerLabelAnnotation(ctx, helper, customIssuer, instance.Namespace, issuerLabels, issuerAnnotations)
250256
if err != nil {
251257
instance.Status.Conditions.Set(condition.FalseCondition(
252258
corev1.OpenStackControlPlaneCAReadyCondition,
@@ -289,6 +295,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
289295

290296
// create CA for ovn
291297
issuerLabels = map[string]string{certmanager.RootCAIssuerOvnDBLabel: ""}
298+
issuerAnnotations = getIssuerAnnotations(&instance.Spec.TLS.PodLevel.Ovn.Cert)
292299
if !instance.Spec.TLS.PodLevel.Ovn.Ca.IsCustomIssuer() {
293300
ctrlResult, err = ensureRootCA(
294301
ctx,
@@ -297,6 +304,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
297304
issuerReq,
298305
corev1.OvnDbCaName,
299306
issuerLabels,
307+
issuerAnnotations,
300308
bundle,
301309
caOnlyBundle,
302310
instance.Spec.TLS.PodLevel.Ovn.Ca,
@@ -309,7 +317,7 @@ func ReconcileCAs(ctx context.Context, instance *corev1.OpenStackControlPlane, h
309317
} else {
310318
customIssuer := *instance.Spec.TLS.PodLevel.Ovn.Ca.CustomIssuer
311319
// add CA labelselector to issuer
312-
caCertSecretName, err := addIssuerLabel(ctx, helper, customIssuer, instance.Namespace, issuerLabels)
320+
caCertSecretName, err := addIssuerLabelAnnotation(ctx, helper, customIssuer, instance.Namespace, issuerLabels, issuerAnnotations)
313321
if err != nil {
314322
instance.Status.Conditions.Set(condition.FalseCondition(
315323
corev1.OpenStackControlPlaneCAReadyCondition,
@@ -448,6 +456,7 @@ func ensureRootCA(
448456
issuerReq *certmgrv1.Issuer,
449457
caName string,
450458
labels map[string]string,
459+
annotations map[string]string,
451460
bundle *caBundle,
452461
caOnlyBundle *caBundle,
453462
caCfg corev1.CACertConfig,
@@ -462,6 +471,7 @@ func ensureRootCA(
462471
issuerReq,
463472
caName,
464473
labels,
474+
annotations,
465475
caCfg,
466476
)
467477
if err != nil {
@@ -518,6 +528,7 @@ func createRootCACertAndIssuer(
518528
selfsignedIssuerReq *certmgrv1.Issuer,
519529
caName string,
520530
labels map[string]string,
531+
annotations map[string]string,
521532
caCfg corev1.CACertConfig,
522533
) ([]byte, ctrl.Result, error) {
523534
// create RootCA Certificate used to sign certificates
@@ -570,6 +581,7 @@ func createRootCACertAndIssuer(
570581
caCertReq.Name,
571582
instance.GetNamespace(),
572583
labels,
584+
annotations,
573585
caCertReq.Name,
574586
)
575587

@@ -740,12 +752,13 @@ func (cab *caBundle) getBundlePEM() string {
740752
return bundleData
741753
}
742754

743-
func addIssuerLabel(
755+
func addIssuerLabelAnnotation(
744756
ctx context.Context,
745757
helper *helper.Helper,
746758
name string,
747759
namespace string,
748760
labels map[string]string,
761+
annotations map[string]string,
749762
) (string, error) {
750763
var caCertSecretName string
751764
// get issuer
@@ -764,6 +777,8 @@ func addIssuerLabel(
764777
beforeIssuer := issuer.DeepCopyObject().(client.Object)
765778
// merge labels
766779
issuer.Labels = util.MergeMaps(issuer.Labels, labels)
780+
// merge annotations
781+
issuer.Annotations = util.MergeMaps(issuer.Annotations, annotations)
767782

768783
// patch issuer
769784
patch := client.MergeFrom(beforeIssuer)
@@ -791,3 +806,19 @@ func addIssuerLabel(
791806

792807
return caCertSecretName, nil
793808
}
809+
810+
func getIssuerAnnotations(certConfig *corev1.CertConfig) map[string]string {
811+
annotations := map[string]string{}
812+
certDuration := certConfig.GetDurationHours()
813+
if certDuration == "" {
814+
certDuration = certmanager.CertDefaultDuration
815+
}
816+
annotations[certmanager.CertDurationAnnotation] = certDuration
817+
818+
certRenewBefore := certConfig.GetRenewBeforeHours()
819+
if certRenewBefore != "" {
820+
annotations[certmanager.CertRenewBeforeAnnotation] = certRenewBefore
821+
}
822+
823+
return annotations
824+
}

0 commit comments

Comments
 (0)