Skip to content

Commit b3cd56e

Browse files
committed
machine: set v1beta2 Paused condition
1 parent 5511d65 commit b3cd56e

File tree

2 files changed

+5
-48
lines changed

2 files changed

+5
-48
lines changed

internal/controllers/machine/machine_controller.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
"sigs.k8s.io/cluster-api/util/finalizers"
5656
clog "sigs.k8s.io/cluster-api/util/log"
5757
"sigs.k8s.io/cluster-api/util/patch"
58+
"sigs.k8s.io/cluster-api/util/paused"
5859
"sigs.k8s.io/cluster-api/util/predicates"
5960
)
6061

@@ -121,15 +122,13 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
121122
c, err := ctrl.NewControllerManagedBy(mgr).
122123
For(&clusterv1.Machine{}).
123124
WithOptions(options).
124-
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue)).
125125
Watches(
126126
&clusterv1.Cluster{},
127127
handler.EnqueueRequestsFromMapFunc(clusterToMachines),
128128
builder.WithPredicates(
129129
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
130130
predicates.All(mgr.GetScheme(), predicateLog,
131131
predicates.Any(mgr.GetScheme(), predicateLog,
132-
predicates.ClusterUnpaused(mgr.GetScheme(), predicateLog),
133132
predicates.ClusterControlPlaneInitialized(mgr.GetScheme(), predicateLog),
134133
),
135134
predicates.ResourceHasFilterLabel(mgr.GetScheme(), predicateLog, r.WatchFilterValue),
@@ -195,17 +194,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
195194
m.Spec.ClusterName, m.Name, m.Namespace)
196195
}
197196

197+
if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, r.Client, cluster, m); err != nil || isPaused || conditionChanged {
198+
return ctrl.Result{}, err
199+
}
200+
198201
s := &scope{
199202
cluster: cluster,
200203
machine: m,
201204
}
202205

203-
// Return early if the object or Cluster is paused.
204-
if annotations.IsPaused(cluster, m) {
205-
log.Info("Reconciliation is paused for this object")
206-
return ctrl.Result{}, setPausedCondition(ctx, r.Client, s)
207-
}
208-
209206
// Initialize the patch helper
210207
patchHelper, err := patch.NewHelper(m, r.Client)
211208
if err != nil {
@@ -304,7 +301,6 @@ func patchMachine(ctx context.Context, patchHelper *patch.Helper, machine *clust
304301
clusterv1.MachineNodeReadyV1Beta2Condition,
305302
clusterv1.MachineNodeHealthyV1Beta2Condition,
306303
clusterv1.MachineDeletingV1Beta2Condition,
307-
clusterv1.MachinePausedV1Beta2Condition,
308304
}},
309305
)
310306

internal/controllers/machine/machine_controller_status.go

-39
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@ import (
2727
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2828
"k8s.io/utils/ptr"
2929
ctrl "sigs.k8s.io/controller-runtime"
30-
"sigs.k8s.io/controller-runtime/pkg/client"
3130

3231
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3332
"sigs.k8s.io/cluster-api/internal/contract"
34-
"sigs.k8s.io/cluster-api/util/annotations"
3533
v1beta2conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
36-
"sigs.k8s.io/cluster-api/util/patch"
3734
)
3835

3936
// reconcileStatus reconciles Machine's status during the entire lifecycle of the machine.
@@ -72,8 +69,6 @@ func (r *Reconciler) reconcileStatus(ctx context.Context, s *scope) {
7269

7370
// TODO: Update the Deleting condition.
7471

75-
unsetPausedCondition(s)
76-
7772
setMachinePhaseAndLastUpdated(ctx, s.machine)
7873
}
7974

@@ -539,40 +534,6 @@ func setAvailableCondition(_ context.Context, machine *clusterv1.Machine) {
539534
})
540535
}
541536

542-
func setPausedCondition(ctx context.Context, c client.Client, s *scope) error {
543-
patchHelper, err := patch.NewHelper(s.machine, c)
544-
if err != nil {
545-
return err
546-
}
547-
548-
var messages []string
549-
if s.cluster.Spec.Paused {
550-
messages = append(messages, "Cluster spec.paused is set to true")
551-
}
552-
if annotations.HasPaused(s.machine) {
553-
messages = append(messages, "Machine has the cluster.x-k8s.io/paused annotation")
554-
}
555-
v1beta2conditions.Set(s.machine, metav1.Condition{
556-
Type: clusterv1.MachinePausedV1Beta2Condition,
557-
Status: metav1.ConditionTrue,
558-
Reason: clusterv1.PausedV1Beta2Reason,
559-
Message: strings.Join(messages, ", "),
560-
})
561-
562-
return patchHelper.Patch(ctx, s.machine, patch.WithOwnedV1Beta2Conditions{Conditions: []string{
563-
clusterv1.MachinePausedV1Beta2Condition,
564-
}})
565-
}
566-
567-
func unsetPausedCondition(s *scope) {
568-
// Note: If we hit this code, the controller is reconciling and this Paused condition must be set to false.
569-
v1beta2conditions.Set(s.machine, metav1.Condition{
570-
Type: clusterv1.MachinePausedV1Beta2Condition,
571-
Status: metav1.ConditionFalse,
572-
Reason: clusterv1.NotPausedV1Beta2Reason,
573-
})
574-
}
575-
576537
func setMachinePhaseAndLastUpdated(_ context.Context, m *clusterv1.Machine) {
577538
originalPhase := m.Status.Phase
578539

0 commit comments

Comments
 (0)