Skip to content

Commit 660227d

Browse files
Iconditiosnmplement 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 cac790e commit 660227d

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
@@ -88,14 +88,14 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
8888
handler.EnqueueRequestsFromMapFunc(r.MachineSetToDeployments),
8989
).
9090
WithOptions(options).
91-
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
91+
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
9292
Watches(
9393
&clusterv1.Cluster{},
9494
handler.EnqueueRequestsFromMapFunc(clusterToMachineDeployments),
9595
builder.WithPredicates(
9696
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
9797
predicates.All(ctrl.LoggerFrom(ctx),
98-
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
98+
predicates.ClusterCreateUpdateEvent(ctrl.LoggerFrom(ctx)),
9999
),
100100
),
101101
).Complete(r)
@@ -131,12 +131,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
131131
return ctrl.Result{}, err
132132
}
133133

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

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

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

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)