Skip to content

Commit eaebb98

Browse files
committed
pause CAPI controllers
1 parent ebe23fe commit eaebb98

7 files changed

+139
-1
lines changed

internal/controller/linodecluster_controller.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3333
kutil "sigs.k8s.io/cluster-api/util"
3434
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
35+
"sigs.k8s.io/cluster-api/util/paused"
3536
"sigs.k8s.io/cluster-api/util/predicates"
3637
ctrl "sigs.k8s.io/controller-runtime"
3738
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -116,14 +117,59 @@ func (r *LinodeClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
116117
return r.reconcile(ctx, clusterScope, logger)
117118
}
118119

120+
func (r *LinodeClusterReconciler) reconcilePause(ctx context.Context, clusterScope *scope.ClusterScope, logger logr.Logger) error {
121+
// First thing to do is handle a paused Cluster. Paused clusters shouldn't be deleted.
122+
isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, clusterScope.Client, clusterScope.Cluster, clusterScope.LinodeCluster)
123+
if err == nil && !isPaused && !conditionChanged {
124+
return nil
125+
}
126+
127+
if err != nil {
128+
return err
129+
}
130+
131+
if clusterScope.LinodeCluster.Spec.VPCRef == nil {
132+
logger.Info("Paused reconciliation is skipped due to missing VPC ref")
133+
return nil
134+
}
135+
136+
linodeVPC := infrav1alpha2.LinodeVPC{
137+
ObjectMeta: metav1.ObjectMeta{
138+
Namespace: clusterScope.LinodeCluster.Spec.VPCRef.Namespace,
139+
Name: clusterScope.LinodeCluster.Spec.VPCRef.Name,
140+
},
141+
}
142+
143+
if err := clusterScope.Client.Get(ctx, client.ObjectKeyFromObject(&linodeVPC), &linodeVPC); err != nil {
144+
return err
145+
}
146+
147+
annotations := linodeVPC.ObjectMeta.GetAnnotations()
148+
if annotations == nil {
149+
annotations = map[string]string{}
150+
}
151+
152+
if isPaused {
153+
logger.Info("CAPI cluster is paused, pausing VPC")
154+
// if we're paused, we should slap the pause annotation on our children
155+
// get the vpc & add the annotation
156+
annotations[clusterv1.PausedAnnotation] = "true"
157+
} else {
158+
// we are not paused here, but were previously paused (we can get here only if conditionChanged is true.
159+
logger.Info("CAPI cluster is no longer paused, removing pause annotation from VPC")
160+
delete(annotations, clusterv1.PausedAnnotation)
161+
}
162+
linodeVPC.SetAnnotations(annotations)
163+
return clusterScope.PatchHelper.Patch(ctx, &linodeVPC)
164+
}
165+
119166
//nolint:cyclop // can't make it simpler with existing API
120167
func (r *LinodeClusterReconciler) reconcile(
121168
ctx context.Context,
122169
clusterScope *scope.ClusterScope,
123170
logger logr.Logger,
124171
) (res ctrl.Result, reterr error) {
125172
res = ctrl.Result{}
126-
127173
clusterScope.LinodeCluster.Status.Ready = false
128174
clusterScope.LinodeCluster.Status.FailureReason = nil
129175
clusterScope.LinodeCluster.Status.FailureMessage = util.Pointer("")
@@ -150,6 +196,10 @@ func (r *LinodeClusterReconciler) reconcile(
150196
return res, err
151197
}
152198

199+
if err := r.reconcilePause(ctx, clusterScope, logger); err != nil {
200+
return res, err
201+
}
202+
153203
// Handle deleted clusters
154204
if !clusterScope.LinodeCluster.DeletionTimestamp.IsZero() {
155205
if err := r.reconcileDelete(ctx, logger, clusterScope); err != nil {

internal/controller/linodefirewall_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3333
kutil "sigs.k8s.io/cluster-api/util"
3434
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
35+
"sigs.k8s.io/cluster-api/util/paused"
3536
"sigs.k8s.io/cluster-api/util/predicates"
3637
ctrl "sigs.k8s.io/controller-runtime"
3738
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -96,6 +97,7 @@ func (r *LinodeFirewallReconciler) Reconcile(ctx context.Context, req ctrl.Reque
9697
return r.reconcile(ctx, log, fwScope)
9798
}
9899

100+
//nolint:cyclop // simplying this further makes it much harder to read
99101
func (r *LinodeFirewallReconciler) reconcile(
100102
ctx context.Context,
101103
logger logr.Logger,
@@ -136,6 +138,12 @@ func (r *LinodeFirewallReconciler) reconcile(
136138
return ctrl.Result{}, err
137139
}
138140

141+
// Pause
142+
// We don't have much to do, but simply requeue without an error if we are paused or if we were recently unpaused
143+
if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, fwScope.Client, nil, fwScope.LinodeFirewall); err != nil || isPaused || conditionChanged {
144+
return ctrl.Result{}, err
145+
}
146+
139147
// Delete
140148
if !fwScope.LinodeFirewall.ObjectMeta.DeletionTimestamp.IsZero() {
141149
failureReason = infrav1alpha2.DeleteFirewallError

internal/controller/linodemachine_controller.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3636
kutil "sigs.k8s.io/cluster-api/util"
3737
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
38+
"sigs.k8s.io/cluster-api/util/paused"
3839
"sigs.k8s.io/cluster-api/util/predicates"
3940
ctrl "sigs.k8s.io/controller-runtime"
4041
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -169,6 +170,51 @@ func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques
169170
return r.reconcile(ctx, log, machineScope)
170171
}
171172

173+
func (r *LinodeMachineReconciler) reconcilePause(ctx context.Context, logger logr.Logger, machineScope *scope.MachineScope) error {
174+
// Pause
175+
isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, machineScope.Client, machineScope.Cluster, machineScope.LinodeMachine)
176+
177+
if err == nil && !isPaused && !conditionChanged {
178+
return nil
179+
}
180+
if err != nil {
181+
return err
182+
}
183+
if machineScope.LinodeMachine.Spec.FirewallRef == nil {
184+
logger.Info("Paused reconciliation is skipped due to missing Firewall ref")
185+
return nil
186+
}
187+
188+
linodeFW := infrav1alpha2.LinodeFirewall{
189+
ObjectMeta: metav1.ObjectMeta{
190+
Namespace: machineScope.LinodeMachine.Spec.FirewallRef.Namespace,
191+
Name: machineScope.LinodeMachine.Spec.FirewallRef.Name,
192+
},
193+
}
194+
195+
if err := machineScope.Client.Get(ctx, client.ObjectKeyFromObject(&linodeFW), &linodeFW); err != nil {
196+
return err
197+
}
198+
199+
annotations := linodeFW.ObjectMeta.GetAnnotations()
200+
if annotations == nil {
201+
annotations = map[string]string{}
202+
}
203+
204+
if isPaused {
205+
logger.Info("CAPI cluster is paused, pausing Firewall too")
206+
// if we're paused, we should slap the pause annotation on our children
207+
// get the firewall & add the annotation
208+
annotations[clusterv1.PausedAnnotation] = "true"
209+
} else {
210+
// we are not paused here, but were previously paused (we can get here only if conditionChanged is true.
211+
logger.Info("CAPI cluster is no longer paused, removing pause annotation from Firewall")
212+
delete(annotations, clusterv1.PausedAnnotation)
213+
}
214+
linodeFW.SetAnnotations(annotations)
215+
return machineScope.PatchHelper.Patch(ctx, &linodeFW)
216+
}
217+
172218
func (r *LinodeMachineReconciler) reconcile(ctx context.Context, logger logr.Logger, machineScope *scope.MachineScope) (res ctrl.Result, err error) {
173219
failureReason := util.UnknownError
174220
//nolint:dupl // Code duplication is simplicity in this case.
@@ -216,6 +262,11 @@ func (r *LinodeMachineReconciler) reconcile(ctx context.Context, logger logr.Log
216262
}
217263
}
218264

265+
// Pause
266+
if err := r.reconcilePause(ctx, logger, machineScope); err != nil {
267+
return ctrl.Result{}, err
268+
}
269+
219270
// Delete
220271
if !machineScope.LinodeMachine.ObjectMeta.DeletionTimestamp.IsZero() {
221272
failureReason = util.DeleteError

internal/controller/linodeobjectstoragebucket_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3131
kutil "sigs.k8s.io/cluster-api/util"
3232
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
33+
"sigs.k8s.io/cluster-api/util/paused"
3334
"sigs.k8s.io/cluster-api/util/predicates"
3435
ctrl "sigs.k8s.io/controller-runtime"
3536
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -115,6 +116,13 @@ func (r *LinodeObjectStorageBucketReconciler) reconcile(ctx context.Context, bSc
115116
reterr = err
116117
}
117118
}()
119+
120+
// Pause
121+
// We don't have much to do, but simply requeue without an error if we are paused or if we were recently unpaused
122+
if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, bScope.Client, nil, bScope.Bucket); err != nil || isPaused || conditionChanged {
123+
return ctrl.Result{}, err
124+
}
125+
118126
if err := r.reconcileApply(ctx, bScope); err != nil {
119127
return res, err
120128
}

internal/controller/linodeobjectstoragekey_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3434
kutil "sigs.k8s.io/cluster-api/util"
3535
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
36+
"sigs.k8s.io/cluster-api/util/paused"
3637
"sigs.k8s.io/cluster-api/util/predicates"
3738
ctrl "sigs.k8s.io/controller-runtime"
3839
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -129,6 +130,12 @@ func (r *LinodeObjectStorageKeyReconciler) reconcile(ctx context.Context, keySco
129130
return res, err
130131
}
131132

133+
// Pause
134+
// We don't have much to do, but simply requeue without an error if we are paused or if we were recently unpaused
135+
if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, keyScope.Client, nil, keyScope.Key); err != nil || isPaused || conditionChanged {
136+
return ctrl.Result{}, err
137+
}
138+
132139
if !keyScope.Key.DeletionTimestamp.IsZero() {
133140
return res, r.reconcileDelete(ctx, keyScope)
134141
}

internal/controller/linodeplacementgroup_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3535
kutil "sigs.k8s.io/cluster-api/util"
3636
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
37+
"sigs.k8s.io/cluster-api/util/paused"
3738
"sigs.k8s.io/cluster-api/util/predicates"
3839
ctrl "sigs.k8s.io/controller-runtime"
3940
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -147,6 +148,12 @@ func (r *LinodePlacementGroupReconciler) reconcile(
147148
return res, err
148149
}
149150

151+
// Pause
152+
// We don't have much to do, but simply requeue without an error if we are paused or if we were recently unpaused
153+
if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, pgScope.Client, nil, pgScope.LinodePlacementGroup); err != nil || isPaused || conditionChanged {
154+
return ctrl.Result{}, err
155+
}
156+
150157
// Delete
151158
if !pgScope.LinodePlacementGroup.ObjectMeta.DeletionTimestamp.IsZero() {
152159
failureReason = infrav1alpha2.DeletePlacementGroupError

internal/controller/linodevpc_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3434
kutil "sigs.k8s.io/cluster-api/util"
3535
conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
36+
"sigs.k8s.io/cluster-api/util/paused"
3637
"sigs.k8s.io/cluster-api/util/predicates"
3738
ctrl "sigs.k8s.io/controller-runtime"
3839
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -153,6 +154,12 @@ func (r *LinodeVPCReconciler) reconcile(
153154
return res, err
154155
}
155156

157+
// Pause
158+
// We don't have much to do, but simply requeue without an error if we are paused or if we were recently unpaused
159+
if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, vpcScope.Client, nil, vpcScope.LinodeVPC); err != nil || isPaused || conditionChanged {
160+
return ctrl.Result{}, err
161+
}
162+
156163
// Delete
157164
if !vpcScope.LinodeVPC.ObjectMeta.DeletionTimestamp.IsZero() {
158165
failureReason = infrav1alpha2.DeleteVPCError

0 commit comments

Comments
 (0)