Skip to content

Commit 3548866

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. If the machine deployment is paused a different reason is selected: MachineDeploymentPausedReason
1 parent 02b2739 commit 3548866

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

api/v1beta1/condition_consts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const (
5050

5151
// AnnotationPausedReason (Severity=Info) documents a CAPI object that is paused due to the paused annotation being present.
5252
AnnotationPausedReason = "PausedAnnotationSet"
53+
54+
// MachineDeploymentPausedReason (Severity=Info) documents a CAPI object that is paused due to the machine deployment being paused.
55+
MachineDeploymentPausedReason = "MachineDeploymentPaused"
5356
)
5457

5558
const (

internal/controllers/machinedeployment/machinedeployment_controller.go

Lines changed: 34 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,29 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
154148
}
155149
}()
156150

151+
// Return early and set the paused condition to True if the object or Cluster
152+
// is paused.
153+
if annotations.IsPaused(cluster, deployment) {
154+
log.Info("Reconciliation is paused for this object")
155+
156+
newPausedCondition := &clusterv1.Condition{
157+
Type: clusterv1.PausedCondition,
158+
Status: corev1.ConditionTrue,
159+
Severity: clusterv1.ConditionSeverityInfo,
160+
}
161+
162+
if cluster.Spec.Paused {
163+
newPausedCondition.Reason = clusterv1.ClusterPausedReason
164+
} else {
165+
newPausedCondition.Reason = clusterv1.AnnotationPausedReason
166+
}
167+
168+
conditions.Set(deployment, newPausedCondition)
169+
return ctrl.Result{}, nil
170+
}
171+
172+
conditions.MarkFalseWithNegativePolarity(deployment, clusterv1.PausedCondition)
173+
157174
// Ignore deleted MachineDeployments, this can happen when foregroundDeletion
158175
// is enabled
159176
if !deployment.DeletionTimestamp.IsZero() {
@@ -261,6 +278,15 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
261278
}
262279

263280
if md.Spec.Paused {
281+
log.Info("This machine deployment is paused. Sync and status updates will still be reconciled")
282+
newPausedCondition := &clusterv1.Condition{
283+
Type: clusterv1.PausedCondition,
284+
Status: corev1.ConditionTrue,
285+
Severity: clusterv1.ConditionSeverityInfo,
286+
Reason: clusterv1.MachineDeploymentPausedReason,
287+
}
288+
289+
conditions.Set(md, newPausedCondition)
264290
return r.sync(ctx, md, msList)
265291
}
266292

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)