Skip to content

Commit

Permalink
chore(controller): Make controller logic consistent
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Cesbron <mathieu.cesbron@protonmail.com>
  • Loading branch information
MathieuCesbron committed Feb 25, 2024
1 parent 74a6eb6 commit fbb977a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
29 changes: 15 additions & 14 deletions controllers/certificate_lifecycle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,29 @@ type CertificateLifecycle struct {
}

func (s *CertificateLifecycle) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
logger := log.FromContext(ctx)
log := log.FromContext(ctx)

logger.Info("starting CertificateLifecycle handling")
log.Info("starting CertificateLifecycle handling")

secret := corev1.Secret{}
if err := s.client.Get(ctx, request.NamespacedName, &secret); err != nil {
if k8serrors.IsNotFound(err) {
logger.Info("resource may have been deleted, skipping")

return reconcile.Result{}, nil
}
err := s.client.Get(ctx, request.NamespacedName, &secret)
if k8serrors.IsNotFound(err) {
log.Info("resource have been deleted, skipping")
return reconcile.Result{}, nil

Check failure on line 46 in controllers/certificate_lifecycle_controller.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}
if err != nil {
log.Error(err, "cannot retrieve the required resource")
return reconcile.Result{}, err

Check failure on line 50 in controllers/certificate_lifecycle_controller.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}

checkType, ok := secret.GetLabels()[constants.ControllerLabelResource]
if !ok {
logger.Info("missing controller label, shouldn't happen")
log.Info("missing controller label, shouldn't happen")

return reconcile.Result{}, nil
}

var crt *x509.Certificate
var err error

switch checkType {
case "x509":
Expand All @@ -68,15 +69,15 @@ func (s *CertificateLifecycle) Reconcile(ctx context.Context, request reconcile.
}

if err != nil {
logger.Error(err, "skipping reconciliation")
log.Error(err, "skipping reconciliation")

return reconcile.Result{}, nil
}

deadline := time.Now().AddDate(0, 0, 1)

if deadline.After(crt.NotAfter) {
logger.Info("certificate near expiration, must be rotated")
log.Info("certificate near expiration, must be rotated")

s.Channel <- event.GenericEvent{Object: &kamajiv1alpha1.TenantControlPlane{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -85,14 +86,14 @@ func (s *CertificateLifecycle) Reconcile(ctx context.Context, request reconcile.
},
}}

logger.Info("certificate rotation triggered")
log.Info("certificate rotation triggered")

return reconcile.Result{}, nil
}

after := crt.NotAfter.Sub(deadline)

logger.Info("certificate is still valid, enqueuing back", "after", after.String())
log.Info("certificate is still valid, enqueuing back", "after", after.String())

return reconcile.Result{Requeue: true, RequeueAfter: after}, nil
}
Expand Down
14 changes: 7 additions & 7 deletions controllers/datastore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func (r *DataStore) Reconcile(ctx context.Context, request reconcile.Request) (r
log := log.FromContext(ctx)

ds := &kamajiv1alpha1.DataStore{}
if err := r.Client.Get(ctx, request.NamespacedName, ds); err != nil {
if k8serrors.IsNotFound(err) {
return reconcile.Result{}, nil
}

log.Error(err, "unable to retrieve the request")

err := r.Client.Get(ctx, request.NamespacedName, ds)
if k8serrors.IsNotFound(err) {
log.Info("resource have been deleted, skipping")
return reconcile.Result{}, nil

Check failure on line 45 in controllers/datastore_controller.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}
if err != nil {
log.Error(err, "cannot retrieve the required resource")
return reconcile.Result{}, err
}

Expand Down
17 changes: 7 additions & 10 deletions controllers/tenantcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
apimachineryerrors "k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/clock"
Expand Down Expand Up @@ -84,16 +84,13 @@ func (r *TenantControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R
defer cancelFn()

tenantControlPlane, err := r.getTenantControlPlane(ctx, req.NamespacedName)()
if k8serrors.IsNotFound(err) {
log.Info("resource have been deleted, skipping")
return reconcile.Result{}, nil
}
if err != nil {
if apimachineryerrors.IsNotFound(err) {
log.Info("resource may have been deleted, skipping")

return ctrl.Result{}, nil
}

log.Error(err, "cannot retrieve the required instance")

return ctrl.Result{}, err
log.Error(err, "cannot retrieve the required resource")
return reconcile.Result{}, err
}

releaser, err := mutex.Acquire(r.mutexSpec(tenantControlPlane))
Expand Down

0 comments on commit fbb977a

Please sign in to comment.