Skip to content

Commit e369071

Browse files
Machine controller: drop ClusterUnpaused predicate
This change drops the ClusterUnpaused predicate, in favour of one that will reconcile both paused and unpaused clusters. This is to enable the setting of the Paused condition.
1 parent 2a7a2a2 commit e369071

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

internal/controllers/machine/machine_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
116116
c, err := ctrl.NewControllerManagedBy(mgr).
117117
For(&clusterv1.Machine{}).
118118
WithOptions(options).
119-
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
119+
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
120120
Watches(
121121
&clusterv1.Cluster{},
122122
handler.EnqueueRequestsFromMapFunc(clusterToMachines),
123123
builder.WithPredicates(
124124
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
125125
predicates.All(ctrl.LoggerFrom(ctx),
126126
predicates.Any(ctrl.LoggerFrom(ctx),
127-
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
127+
predicates.ClusterCreateUpdateEvent(ctrl.LoggerFrom(ctx)),
128128
predicates.ClusterControlPlaneInitialized(ctrl.LoggerFrom(ctx)),
129129
),
130130
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
@@ -206,7 +206,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
206206
// Return early and set the paused condition to True if the object or Cluster
207207
// is paused.
208208
if annotations.IsPaused(cluster, m) {
209-
log.Info("Reconciliation is paused for this object")
209+
log.Info("Reconciliation is paused for this object, setting Paused condition")
210210
conditions.MarkTrue(m, clusterv1.PausedCondition)
211211
return ctrl.Result{}, nil
212212
}

util/predicates/cluster_predicates.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,49 @@ func ClusterUnpaused(logger logr.Logger) predicate.Funcs {
168168
return Any(log, ClusterCreateNotPaused(log), ClusterUpdateUnpaused(log))
169169
}
170170

171+
func ClusterCreateUpdateEvent(logger logr.Logger) predicate.Funcs {
172+
return predicate.Funcs{
173+
UpdateFunc: func(e event.UpdateEvent) bool {
174+
log := logger.WithValues("predicate", "ClusterUpdatePauseUnpause", "eventType", "update")
175+
176+
oldCluster, ok := e.ObjectOld.(*clusterv1.Cluster)
177+
if !ok {
178+
log.V(4).Info("Expected Cluster", "type", fmt.Sprintf("%T", e.ObjectOld))
179+
return false
180+
}
181+
log = log.WithValues("Cluster", klog.KObj(oldCluster))
182+
183+
newCluster := e.ObjectNew.(*clusterv1.Cluster)
184+
185+
if oldCluster.Spec.Paused && !newCluster.Spec.Paused {
186+
log.V(4).Info("Cluster was unpaused, allowing further processing")
187+
return true
188+
}
189+
190+
if !oldCluster.Spec.Paused && newCluster.Spec.Paused {
191+
log.V(4).Info("Cluster was paused, allowing further processing to update condition")
192+
return true
193+
}
194+
195+
return false
196+
},
197+
CreateFunc: func(e event.CreateEvent) bool {
198+
log := logger.WithValues("predicate", "ClusterCreate", "eventType", "create")
199+
200+
c, ok := e.Object.(*clusterv1.Cluster)
201+
if !ok {
202+
log.V(4).Info("Expected Cluster", "type", fmt.Sprintf("%T", e.Object))
203+
return false
204+
}
205+
log = log.WithValues("Cluster", klog.KObj(c))
206+
207+
return true
208+
},
209+
DeleteFunc: func(event.DeleteEvent) bool { return false },
210+
GenericFunc: func(event.GenericEvent) bool { return false },
211+
}
212+
}
213+
171214
// ClusterControlPlaneInitialized returns a Predicate that returns true on Update events
172215
// when ControlPlaneInitializedCondition on a Cluster changes to true.
173216
// Example use:

0 commit comments

Comments
 (0)