Skip to content

Commit e67a7ac

Browse files
Implement paused condition for machine deployment controller
This change updates the machine deployment controller to set the paused condition based on if the cluster or machine deployment has the paused annotations.
1 parent dbad38a commit e67a7ac

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

internal/controllers/machinedeployment/machinedeployment_controller.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
8787
handler.EnqueueRequestsFromMapFunc(r.MachineSetToDeployments),
8888
).
8989
WithOptions(options).
90-
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
90+
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
9191
Watches(
9292
&clusterv1.Cluster{},
9393
handler.EnqueueRequestsFromMapFunc(clusterToMachineDeployments),
9494
builder.WithPredicates(
9595
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
9696
predicates.All(ctrl.LoggerFrom(ctx),
97-
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
97+
predicates.ClusterCreateUpdateEvent(ctrl.LoggerFrom(ctx)),
9898
),
9999
),
100100
).Complete(r)
@@ -130,12 +130,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
130130
return ctrl.Result{}, err
131131
}
132132

133-
// Return early if the object or Cluster is paused.
134-
if annotations.IsPaused(cluster, deployment) {
135-
log.Info("Reconciliation is paused for this object")
136-
return ctrl.Result{}, nil
137-
}
138-
139133
// Initialize the patch helper
140134
patchHelper, err := patch.NewHelper(deployment, r.Client)
141135
if err != nil {
@@ -154,6 +148,14 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
154148
}
155149
}()
156150

151+
// Return early if the object or Cluster is paused.
152+
if annotations.IsPaused(cluster, deployment) {
153+
log.Info("Reconciliation is paused for this object")
154+
conditions.MarkTrue(deployment, clusterv1.PausedCondition)
155+
return ctrl.Result{}, nil
156+
}
157+
conditions.MarkFalse(deployment, clusterv1.PausedCondition, clusterv1.ResourceNotPausedReason, clusterv1.ConditionSeverityInfo, "Resource is operating as expected")
158+
157159
// Ignore deleted MachineDeployments, this can happen when foregroundDeletion
158160
// is enabled
159161
if !deployment.DeletionTimestamp.IsZero() {
@@ -261,6 +263,10 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
261263
}
262264

263265
if md.Spec.Paused {
266+
log.Info("This machine deployment is paused. Sync and status updates will still be reconciled")
267+
// TODO: Make this condition specific, with a different reason to the other
268+
// paused = true conditions
269+
conditions.MarkTrue(md, clusterv1.PausedCondition)
264270
return r.sync(ctx, md, msList)
265271
}
266272

internal/controllers/machinedeployment/machinedeployment_controller_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,25 @@ func TestMachineDeploymentReconciler(t *testing.T) {
465465
return conditions.IsTrue(deployment, clusterv1.MachineDeploymentAvailableCondition)
466466
}, timeout).Should(BeTrue())
467467

468+
// By default, the machine deployment is not paused
469+
470+
g.Eventually(func() bool {
471+
key := client.ObjectKey{Name: deployment.Name, Namespace: deployment.Namespace}
472+
g.Expect(env.Get(ctx, key, deployment)).To(Succeed())
473+
return conditions.IsFalse(deployment, clusterv1.PausedCondition)
474+
}, timeout).Should(BeTrue())
475+
476+
// Pause the cluster, expect the paused condition to be set
477+
g.Expect(env.Get(ctx, util.ObjectKey(testCluster), testCluster)).To(Succeed())
478+
testCluster.Spec.Paused = true
479+
g.Expect(env.Update(ctx, testCluster)).To(Succeed())
480+
481+
g.Eventually(func() bool {
482+
key := client.ObjectKey{Name: deployment.Name, Namespace: deployment.Namespace}
483+
g.Expect(env.Get(ctx, key, deployment)).To(Succeed())
484+
return conditions.IsTrue(deployment, clusterv1.PausedCondition)
485+
}, timeout).Should(BeTrue())
486+
468487
// Validate that the controller set the cluster name label in selector.
469488
g.Expect(deployment.Status.Selector).To(ContainSubstring(testCluster.Name))
470489
})

0 commit comments

Comments
 (0)