Skip to content

Commit 4544633

Browse files
Implement paused condition for machine set controller
This change updates the machine set controller to set the paused condition based on if the cluster or machine set resource is paused.
1 parent b530b11 commit 4544633

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

internal/controllers/machineset/machineset_controller.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
105105
handler.EnqueueRequestsFromMapFunc(r.MachineToMachineSets),
106106
).
107107
WithOptions(options).
108-
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
108+
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
109109
Watches(
110110
&clusterv1.Cluster{},
111111
handler.EnqueueRequestsFromMapFunc(clusterToMachineSets),
112112
builder.WithPredicates(
113113
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
114114
predicates.All(ctrl.LoggerFrom(ctx),
115-
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
115+
predicates.ClusterCreateUpdateEvent(ctrl.LoggerFrom(ctx)),
116116
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
117117
),
118118
),
@@ -153,12 +153,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
153153
return ctrl.Result{}, err
154154
}
155155

156-
// Return early if the object or Cluster is paused.
157-
if annotations.IsPaused(cluster, machineSet) {
158-
log.Info("Reconciliation is paused for this object")
159-
return ctrl.Result{}, nil
160-
}
161-
162156
// Initialize the patch helper
163157
patchHelper, err := patch.NewHelper(machineSet, r.Client)
164158
if err != nil {
@@ -172,6 +166,14 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
172166
}
173167
}()
174168

169+
// Return early if the object or Cluster is paused.
170+
if annotations.IsPaused(cluster, machineSet) {
171+
log.Info("Reconciliation is paused for this object")
172+
conditions.MarkTrue(machineSet, clusterv1.PausedCondition)
173+
return ctrl.Result{}, nil
174+
}
175+
conditions.MarkFalse(machineSet, clusterv1.PausedCondition, clusterv1.ResourceNotPausedReason, clusterv1.ConditionSeverityInfo, "Resource is operating as expected")
176+
175177
// Ignore deleted MachineSets, this can happen when foregroundDeletion
176178
// is enabled
177179
if !machineSet.DeletionTimestamp.IsZero() {

internal/controllers/machineset/machineset_controller_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,36 @@ func TestMachineSetReconciler(t *testing.T) {
400400
return conditions.IsTrue(instance, clusterv1.MachinesReadyCondition)
401401
}, timeout).Should(BeTrue())
402402

403+
t.Log("Verifying MachineSet has PausedCondition")
404+
// Start with Paused == False
405+
g.Eventually(func() bool {
406+
key := client.ObjectKey{Name: instance.Name, Namespace: instance.Namespace}
407+
if err := env.Get(ctx, key, instance); err != nil {
408+
return false
409+
}
410+
// Checks the condition is set
411+
if !conditions.Has(instance, clusterv1.PausedCondition) {
412+
return false
413+
}
414+
415+
// The condition is set to false
416+
return conditions.IsFalse(instance, clusterv1.PausedCondition)
417+
}, timeout).Should(BeTrue())
418+
419+
// Pause the cluster
420+
testCluster.Spec.Paused = true
421+
g.Expect(env.Update(ctx, testCluster)).To(Succeed())
422+
423+
// The paused condition should eventually be true
424+
g.Eventually(func() bool {
425+
key := client.ObjectKey{Name: instance.Name, Namespace: instance.Namespace}
426+
if err := env.Get(ctx, key, instance); err != nil {
427+
return false
428+
}
429+
430+
// The condition is set to false
431+
return conditions.IsTrue(instance, clusterv1.PausedCondition)
432+
}, timeout).Should(BeTrue())
403433
// Validate that the controller set the cluster name label in selector.
404434
g.Expect(instance.Status.Selector).To(ContainSubstring(testCluster.Name))
405435
})

0 commit comments

Comments
 (0)