Skip to content

Commit

Permalink
Implement consistent pause behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Dec 15, 2023
1 parent 919c03f commit e6ad0a2
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 31 deletions.
32 changes: 17 additions & 15 deletions controllers/serviceaccount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, req reconcile.
return reconcile.Result{}, err
}

cluster, err := clusterutilv1.GetClusterFromMetadata(ctx, r.Client, vsphereCluster.ObjectMeta)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to get Cluster from VSphereCluster")
}
log = log.WithValues("Cluster", klog.KObj(cluster))
ctx = ctrl.LoggerInto(ctx, log)

// Pause reconciliation if entire VSphereCluster or Cluster is paused
// Note: Pause on the ProviderServiceAccount level is handled in ensureProviderServiceAccounts.
if annotations.IsPaused(cluster, vsphereCluster) {
log.Info("Reconciliation is paused for this object")
return reconcile.Result{}, nil
}

Check warning on line 162 in controllers/serviceaccount_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/serviceaccount_controller.go#L160-L162

Added lines #L160 - L162 were not covered by tests

// Create the patch helper.
patchHelper, err := patch.NewHelper(vsphereCluster, r.Client)
if err != nil {
Expand All @@ -155,6 +169,7 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, req reconcile.

// Create the cluster context for this request.
clusterContext := &vmwarecontext.ClusterContext{
Cluster: cluster,
VSphereCluster: vsphereCluster,
PatchHelper: patchHelper,
}
Expand All @@ -171,19 +186,6 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, req reconcile.
return ctrl.Result{}, r.reconcileDelete(ctx, clusterContext)
}

cluster, err := clusterutilv1.GetClusterFromMetadata(ctx, r.Client, vsphereCluster.ObjectMeta)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to get Cluster from VSphereCluster")
}
log = log.WithValues("Cluster", klog.KObj(cluster))
ctx = ctrl.LoggerInto(ctx, log)

// Pause reconciliation if entire VSphereCluster or Cluster is paused
if annotations.IsPaused(cluster, vsphereCluster) {
log.Info("Reconciliation is paused for this object")
return reconcile.Result{}, nil
}

// We cannot proceed until we are able to access the target cluster. Until
// then just return a no-op and wait for the next sync. This will occur when
// the Cluster's status is updated with a reference to the secret that has
Expand Down Expand Up @@ -257,8 +259,8 @@ func (r *ServiceAccountReconciler) ensureProviderServiceAccounts(ctx context.Con
log := log.WithValues("EnsureProviderServiceAccount", klog.KRef(pSvcAccount.Namespace, pSvcAccount.Name))
ctx := ctrl.LoggerInto(ctx, log)

if guestClusterCtx.Cluster != nil && annotations.IsPaused(guestClusterCtx.Cluster, &(pSvcAccounts[i])) {
log.V(4).Info("Skipping ensure ProviderServiceAccount as ProviderServiceAccount is paused or belongs to a cluster that is paused ")
if annotations.HasPaused(&(pSvcAccounts[i])) {
log.V(4).Info("Skipping ensure ProviderServiceAccount as ProviderServiceAccount is paused")

Check warning on line 263 in controllers/serviceaccount_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/serviceaccount_controller.go#L263

Added line #L263 was not covered by tests
continue
}

Expand Down
18 changes: 13 additions & 5 deletions controllers/servicediscovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func (r *serviceDiscoveryReconciler) Reconcile(ctx context.Context, req reconcil
return reconcile.Result{}, err
}

cluster, err := clusterutilv1.GetClusterFromMetadata(ctx, r.Client, vsphereCluster.ObjectMeta)
if err != nil {
return reconcile.Result{RequeueAfter: clusterNotReadyRequeueTime}, errors.Wrapf(err, "failed to get Cluster from VSphereCluster")
}
log = log.WithValues("Cluster", klog.KObj(cluster))
ctx = ctrl.LoggerInto(ctx, log)

if annotations.IsPaused(cluster, vsphereCluster) {
log.Info("Reconciliation is paused for this object")
return ctrl.Result{}, nil
}

Check warning on line 171 in controllers/servicediscovery_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/servicediscovery_controller.go#L169-L171

Added lines #L169 - L171 were not covered by tests

// Create the patch helper.
patchHelper, err := patch.NewHelper(vsphereCluster, r.Client)
if err != nil {
Expand All @@ -166,6 +178,7 @@ func (r *serviceDiscoveryReconciler) Reconcile(ctx context.Context, req reconcil

// Create the cluster context for this request.
clusterContext := &vmwarecontext.ClusterContext{
Cluster: cluster,
VSphereCluster: vsphereCluster,
PatchHelper: patchHelper,
}
Expand All @@ -183,11 +196,6 @@ func (r *serviceDiscoveryReconciler) Reconcile(ctx context.Context, req reconcil
return reconcile.Result{}, nil
}

cluster, err := clusterutilv1.GetClusterFromMetadata(ctx, r.Client, vsphereCluster.ObjectMeta)
if err != nil {
return reconcile.Result{RequeueAfter: clusterNotReadyRequeueTime}, errors.Wrapf(err, "failed to get Cluster from VSphereCluster")
}

// We cannot proceed until we are able to access the target cluster. Until
// then just return a no-op and wait for the next sync.
guestClient, err := r.remoteClusterCacheTracker.GetClient(ctx, client.ObjectKeyFromObject(cluster))
Expand Down
9 changes: 9 additions & 0 deletions controllers/vmware/vspherecluster_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/klog/v2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterutilv1 "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -89,6 +90,14 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
if cluster != nil {
log = log.WithValues("Cluster", klog.KObj(cluster))
ctx = ctrl.LoggerInto(ctx, log)

if annotations.IsPaused(cluster, vsphereCluster) {
log.Info("Reconciliation is paused for this object")
return ctrl.Result{}, nil
}

Check warning on line 97 in controllers/vmware/vspherecluster_reconciler.go

View check run for this annotation

Codecov / codecov/patch

controllers/vmware/vspherecluster_reconciler.go#L95-L97

Added lines #L95 - L97 were not covered by tests
} else if annotations.HasPaused(vsphereCluster) {
log.Info("Reconciliation is paused for this object")
return ctrl.Result{}, nil

Check warning on line 100 in controllers/vmware/vspherecluster_reconciler.go

View check run for this annotation

Codecov / codecov/patch

controllers/vmware/vspherecluster_reconciler.go#L99-L100

Added lines #L99 - L100 were not covered by tests
}

// Build the patch helper.
Expand Down
8 changes: 8 additions & 0 deletions controllers/vsphereclusteridentity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/klog/v2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterutilv1 "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
Expand Down Expand Up @@ -70,6 +71,8 @@ type clusterIdentityReconciler struct {
}

func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.Request) (_ reconcile.Result, reterr error) {
log := ctrl.LoggerFrom(ctx)

identity := &infrav1.VSphereClusterIdentity{}
if err := r.Client.Get(ctx, req.NamespacedName, identity); err != nil {
if apierrors.IsNotFound(err) {
Expand All @@ -79,6 +82,11 @@ func (r clusterIdentityReconciler) Reconcile(ctx context.Context, req reconcile.
return reconcile.Result{}, err
}

if annotations.HasPaused(identity) {
log.Info("Reconciliation is paused for this object")
return reconcile.Result{}, nil
}

Check warning on line 88 in controllers/vsphereclusteridentity_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vsphereclusteridentity_controller.go#L86-L88

Added lines #L86 - L88 were not covered by tests

// Create the patch helper.
patchHelper, err := patch.NewHelper(identity, r.Client)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions controllers/vspheredeploymentzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterutilv1 "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -97,6 +98,11 @@ func (r vsphereDeploymentZoneReconciler) Reconcile(ctx context.Context, request
log = log.WithValues("VSphereFailureDomain", klog.KRef("", vsphereDeploymentZone.Spec.FailureDomain))
ctx = ctrl.LoggerInto(ctx, log)

if annotations.HasPaused(vsphereDeploymentZone) {
log.Info("Reconciliation is paused for this object")
return reconcile.Result{}, nil
}

Check warning on line 104 in controllers/vspheredeploymentzone_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspheredeploymentzone_controller.go#L102-L104

Added lines #L102 - L104 were not covered by tests

patchHelper, err := patch.NewHelper(vsphereDeploymentZone, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to initialize patch helper")
Expand Down
6 changes: 4 additions & 2 deletions controllers/vspheremachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,11 @@ func (r *machineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_

if annotations.IsPaused(cluster, machineContext.GetVSphereMachine()) {
log.Info("Reconciliation is paused for this object")
// Cluster is set to nil to continue for the delete case and return in the regular case.
cluster = nil
return reconcile.Result{}, nil

Check warning on line 224 in controllers/vspheremachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspheremachine_controller.go#L224

Added line #L224 was not covered by tests
}
} else if annotations.HasPaused(machineContext.GetVSphereMachine()) {
log.Info("Reconciliation is paused for this object")
return reconcile.Result{}, nil

Check warning on line 228 in controllers/vspheremachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspheremachine_controller.go#L227-L228

Added lines #L227 - L228 were not covered by tests
}

// Create the patch helper.
Expand Down
27 changes: 18 additions & 9 deletions controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ func (r vmReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.R
return reconcile.Result{}, err
}

cluster, err := clusterutilv1.GetClusterFromMetadata(ctx, r.Client, vsphereVM.ObjectMeta)
if err != nil {
log.Error(err, "Failed to get Cluster from VSphereVM: Machine is missing cluster label or cluster does not exist")
}
if cluster != nil {
log = log.WithValues("Cluster", klog.KObj(cluster))
ctx = ctrl.LoggerInto(ctx, log)

if annotations.IsPaused(cluster, vsphereVM) {
log.Info("Reconciliation is paused for this object")
return reconcile.Result{}, nil
}

Check warning on line 165 in controllers/vspherevm_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspherevm_controller.go#L163-L165

Added lines #L163 - L165 were not covered by tests
} else if annotations.HasPaused(vsphereVM) {
log.Info("Reconciliation is paused for this object")
return reconcile.Result{}, nil
}

Check warning on line 169 in controllers/vspherevm_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/vspherevm_controller.go#L167-L169

Added lines #L167 - L169 were not covered by tests

// Create the patch helper.
patchHelper, err := patch.NewHelper(vsphereVM, r.Client)
if err != nil {
Expand Down Expand Up @@ -178,7 +195,7 @@ func (r vmReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.R
return reconcile.Result{}, nil
}

log = log.WithValues("VSphereMachine", klog.KObj(vsphereMachine), "Cluster", klog.KRef(vsphereMachine.Namespace, vsphereMachine.Labels[clusterv1.ClusterNameLabel]))
log = log.WithValues("VSphereMachine", klog.KObj(vsphereMachine))
ctx = ctrl.LoggerInto(ctx, log)

vsphereCluster, err := util.GetVSphereClusterFromVSphereMachine(ctx, r.Client, vsphereMachine)
Expand Down Expand Up @@ -254,14 +271,6 @@ func (r vmReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.R
}
}()

cluster, err := clusterutilv1.GetClusterFromMetadata(ctx, r.Client, vsphereVM.ObjectMeta)
if err == nil {
if annotations.IsPaused(cluster, vsphereVM) {
log.Info("Reconciliation is paused for this object")
return reconcile.Result{}, nil
}
}

if vsphereVM.ObjectMeta.DeletionTimestamp.IsZero() {
// If the VSphereVM doesn't have our finalizer, add it.
// Requeue immediately to avoid the race condition between init and delete
Expand Down

0 comments on commit e6ad0a2

Please sign in to comment.