Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Improve logging for cluster identity controller #2426

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions controllers/vsphereclusteridentity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ import (
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/record"
)

const (
// VSphereClusterIdentityControllerName is the name of the vSphere cluster identity controller.
VSphereClusterIdentityControllerName = "vsphereclusteridentity-controller"
)

var (
identityControlledType = &infrav1.VSphereClusterIdentity{}
identityControlledTypeName = reflect.TypeOf(identityControlledType).Elem().Name()
Expand All @@ -61,34 +66,37 @@ func AddVsphereClusterIdentityControllerToManager(ctx context.Context, controlle
controllerNameLong = fmt.Sprintf("%s/%s/%s", controllerManagerCtx.Namespace, controllerManagerCtx.Name, controllerNameShort)
)

// Build the controller context.
controllerContext := &capvcontext.ControllerContext{
ControllerManagerContext: controllerManagerCtx,
Name: controllerNameShort,
Recorder: record.New(mgr.GetEventRecorderFor(controllerNameLong)),
Logger: controllerManagerCtx.Logger.WithName(controllerNameShort),
reconciler := clusterIdentityReconciler{
ControllerManagerCtx: controllerManagerCtx,
Client: controllerManagerCtx.Client,
Recorder: record.New(mgr.GetEventRecorderFor(controllerNameLong)),
}

reconciler := clusterIdentityReconciler{ControllerContext: controllerContext}

return ctrl.NewControllerManagedBy(mgr).
Named(VSphereClusterIdentityControllerName).
For(identityControlledType).
WithOptions(options).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), controllerManagerCtx.WatchFilterValue)).
Complete(reconciler)
}

type clusterIdentityReconciler struct {
*capvcontext.ControllerContext
ControllerManagerCtx *capvcontext.ControllerManagerContext
Client client.Client
Recorder record.Recorder
}

func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.Request) (_ reconcile.Result, reterr error) {
// TODO(gab-satchi) consider creating a context for the clusterIdentity
// Get VSphereClusterIdentity

log := ctrl.LoggerFrom(ctx)
log.V(4).Info("Starting Reconcile")

identity := &infrav1.VSphereClusterIdentity{}
if err := r.Client.Get(ctx, req.NamespacedName, identity); err != nil {
if apierrors.IsNotFound(err) {
r.Logger.V(4).Info("VSphereClusterIdentity not found, won't reconcile", "key", req.NamespacedName)
log.V(4).Info("VSphereClusterIdentity not found, won't reconcile", "key", req.NamespacedName)
return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -127,7 +135,7 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
// fetch secret
secret := &corev1.Secret{}
secretKey := client.ObjectKey{
Namespace: r.Namespace,
Namespace: r.ControllerManagerCtx.Namespace,
Name: identity.Spec.SecretName,
}
if err := r.Client.Get(ctx, secretKey, secret); err != nil {
Expand Down Expand Up @@ -167,10 +175,11 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
}

func (r clusterIdentityReconciler) reconcileDelete(ctx context.Context, identity *infrav1.VSphereClusterIdentity) error {
r.Logger.Info("Reconciling VSphereClusterIdentity delete")
log := ctrl.LoggerFrom(ctx)
log.Info("Reconciling VSphereClusterIdentity delete")
secret := &corev1.Secret{}
secretKey := client.ObjectKey{
Namespace: r.Namespace,
Namespace: r.ControllerManagerCtx.Namespace,
Name: identity.Spec.SecretName,
}
err := r.Client.Get(ctx, secretKey, secret)
Expand All @@ -182,7 +191,7 @@ func (r clusterIdentityReconciler) reconcileDelete(ctx context.Context, identity
}
return err
}
r.Logger.Info(fmt.Sprintf("Removing finalizer from Secret %s/%s", secret.Namespace, secret.Name))
log.Info(fmt.Sprintf("Removing finalizer from Secret %s/%s", secret.Namespace, secret.Name))
// Check if the old finalizer(from v0.7) is present, if yes, delete it
// For more context, please refer: https://github.com/kubernetes-sigs/cluster-api-provider-vsphere/issues/1482
if ctrlutil.ContainsFinalizer(secret, legacyIdentityFinalizer) {
Expand Down