Skip to content

Commit 714fd69

Browse files
add manage-cert-rotation label to certificates (#996)
Signed-off-by: Rob Hundley <[email protected]> Co-authored-by: Robert W. Hundley <[email protected]>
1 parent cc7be22 commit 714fd69

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

controllers/operator/certificate.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (r *AuthenticationReconciler) handleCertificates(ctx context.Context, req c
4949
certificateSubreconcilers := []subreconciler.Fn{
5050
r.removeV1Alpha1Certs(authCR, certificateFieldsList),
5151
r.createV1CertificatesIfNotPresent(authCR, certificateFieldsList),
52+
r.addLabelIfMissing(certificateFieldsList),
5253
}
5354
reqLogger.Info("Reconciling Certificates")
5455
fnCtx := ctrl.LoggerInto(ctx, reqLogger)
@@ -251,12 +252,69 @@ func (r *AuthenticationReconciler) createV1CertificateIfNotPresent(authCR *opera
251252
}
252253
}
253254

255+
// addLabelIfMissing adds "manage-cert-rotation": "yes" label to the certificate if not exist
256+
func (r *AuthenticationReconciler) addLabelIfMissing(fieldsList []*reconcileCertificateFields) (fn subreconciler.Fn) {
257+
return func(ctx context.Context) (result *ctrl.Result, err error) {
258+
reqLogger := ctrl.LoggerFrom(ctx)
259+
260+
allV1CertReconcilers := make([]subreconciler.Fn, 0)
261+
for _, fields := range fieldsList {
262+
allV1CertReconcilers = append(allV1CertReconcilers, r.updateCertWithLabel(fields))
263+
}
264+
results := []*ctrl.Result{}
265+
errs := []error{}
266+
for _, reconcileV1Cert := range allV1CertReconcilers {
267+
result, err = reconcileV1Cert(ctx)
268+
results = append(results, result)
269+
errs = append(errs, err)
270+
}
271+
272+
result, err = ctrlcommon.ReduceSubreconcilerResultsAndErrors(results, errs)
273+
if subreconciler.ShouldContinue(result, err) {
274+
reqLogger.Info("No certificates to be labeled")
275+
} else if subreconciler.ShouldRequeue(result, err) && err == nil {
276+
reqLogger.Info("Certificates were labeled; requeueing")
277+
} else if err != nil {
278+
reqLogger.Info("Encountered an issue while trying to label certificates")
279+
}
280+
281+
return
282+
}
283+
}
284+
285+
func (r *AuthenticationReconciler) updateCertWithLabel(fields *reconcileCertificateFields) subreconciler.Fn {
286+
return func(ctx context.Context) (result *ctrl.Result, err error) {
287+
reqLogger := ctrl.LoggerFrom(ctx, "Certificate.Name", fields.Name, "Certificate.Namespace", fields.Namespace)
288+
cert := &certmgrv1.Certificate{}
289+
err = r.Get(ctx, fields.NamespacedName, cert)
290+
if err == nil {
291+
certRotationKey := "manage-cert-rotation"
292+
if _, exists := cert.Labels[certRotationKey]; !exists {
293+
reqLogger.Info("Updating Certificate with label")
294+
cert.Labels[certRotationKey] = "yes"
295+
err = r.Update(ctx, cert)
296+
if err != nil {
297+
reqLogger.Error(err, "Failed to update Certificate with label")
298+
return subreconciler.RequeueWithError(err)
299+
}
300+
// Certificate label updated successfully - return and requeue
301+
return subreconciler.RequeueWithDelay(defaultLowerWait)
302+
}
303+
return subreconciler.ContinueReconciling()
304+
} else {
305+
reqLogger.Error(err, "Failed to get Certificate for labelling")
306+
return subreconciler.RequeueWithError(err)
307+
}
308+
}
309+
}
310+
254311
func (r *AuthenticationReconciler) generateCertificateObject(authCR *operatorv1alpha1.Authentication, fields *reconcileCertificateFields) *certmgrv1.Certificate {
255312
metaLabels := map[string]string{
256313
"app": fields.CommonName,
257314
"app.kubernetes.io/instance": "ibm-iam-operator",
258315
"app.kubernetes.io/managed-by": "ibm-iam-operator",
259316
"app.kubernetes.io/name": fields.CommonName,
317+
"manage-cert-rotation": "yes",
260318
}
261319

262320
certificate := &certmgrv1.Certificate{

0 commit comments

Comments
 (0)