@@ -35,7 +35,6 @@ 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/patch"
3938 "sigs.k8s.io/cluster-api/util/paused"
4039 "sigs.k8s.io/cluster-api/util/predicates"
4140 ctrl "sigs.k8s.io/controller-runtime"
@@ -191,26 +190,21 @@ func (r *LinodeMachineReconciler) pauseReferencedFirewall(ctx context.Context, l
191190 if annotations == nil {
192191 annotations = map [string ]string {}
193192 }
194-
193+ if ! conditionChanged {
194+ logger .Info ("CAPI cluster condition has not changed, not updating firewall annotations." )
195+ return nil
196+ }
195197 if isPaused {
196- logger .Info ("CAPI cluster is paused, pausing Firewall too" )
197198 // if we're paused, we should slap the pause annotation on our children
198199 // get the firewall & add the annotation
199- annotations [clusterv1 .PausedAnnotation ] = pauseAnnotationValue
200- } else if conditionChanged {
200+ annotations [clusterv1 .PausedAnnotation ] = "true"
201+ } else {
201202 // we are not paused here, but were previously paused
202203 logger .Info ("CAPI cluster is no longer paused, removing pause annotation from Firewall" )
203204 delete (annotations , clusterv1 .PausedAnnotation )
204205 }
205206 linodeFW .SetAnnotations (annotations )
206- fwPatchHelper , err := patch .NewHelper (& linodeFW , machineScope .Client )
207- if err != nil {
208- return fmt .Errorf ("failed to create patch helper for firewalls: %w" , err )
209- }
210- if err := fwPatchHelper .Patch (ctx , & linodeFW ); err != nil {
211- return fmt .Errorf ("failed to patch firewall: %w" , err )
212- }
213- return nil
207+ return machineScope .Client .Update (ctx , & linodeFW )
214208}
215209
216210func (r * LinodeMachineReconciler ) pauseReferencedPlacementGroup (ctx context.Context , logger logr.Logger , machineScope * scope.MachineScope , isPaused , conditionChanged bool ) error {
@@ -235,23 +229,83 @@ func (r *LinodeMachineReconciler) pauseReferencedPlacementGroup(ctx context.Cont
235229 }
236230
237231 if isPaused {
238- logger .Info ("CAPI cluster is paused, pausing Placement Group too" )
239232 // if we're paused, we should slap the pause annotation on our children
240233 // get the firewall & add the annotation
241- annotations [clusterv1 .PausedAnnotation ] = pauseAnnotationValue
234+ annotations [clusterv1 .PausedAnnotation ] = "true"
242235 } else if conditionChanged {
243236 // we are not paused here, but were previously paused
244237 logger .Info ("CAPI cluster is no longer paused, removing pause annotation from Placement Group " )
245238 delete (annotations , clusterv1 .PausedAnnotation )
246239 }
247240
248241 linodePG .SetAnnotations (annotations )
249- fwPatchHelper , err := patch .NewHelper (& linodePG , machineScope .Client )
250- if err != nil {
251- return fmt .Errorf ("failed to create patch helper for firewalls: %w" , err )
242+
243+ return machineScope .Client .Update (ctx , & linodePG )
244+ }
245+
246+ // reconcileFirewallLabel adds the cluster-name label to placement group referenced in the machineScope if it does not already exist
247+ func (r * LinodeMachineReconciler ) reconcileFirewallLabel (ctx context.Context , logger logr.Logger , machineScope * scope.MachineScope ) error {
248+ if machineScope .LinodeMachine .Spec .FirewallRef == nil {
249+ logger .Info ("Paused reconciliation is skipped due to missing Firewall ref" )
250+ return nil
251+ }
252+
253+ linodeFW := infrav1alpha2.LinodeFirewall {
254+ ObjectMeta : metav1.ObjectMeta {
255+ Namespace : machineScope .LinodeMachine .Spec .FirewallRef .Namespace ,
256+ Name : machineScope .LinodeMachine .Spec .FirewallRef .Name ,
257+ },
252258 }
253- if err := fwPatchHelper .Patch (ctx , & linodePG ); err != nil {
254- return fmt .Errorf ("failed to patch firewall: %w" , err )
259+ if err := machineScope .Client .Get (ctx , client .ObjectKeyFromObject (& linodeFW ), & linodeFW ); err != nil {
260+ return err
261+ }
262+ labels := linodeFW .ObjectMeta .GetLabels ()
263+ if labels == nil {
264+ labels = map [string ]string {}
265+ }
266+ if _ , ok := labels [clusterv1 .ClusterNameLabel ]; ! ok {
267+ labels [clusterv1 .ClusterNameLabel ] = machineScope .Machine .Labels [clusterv1 .ClusterNameLabel ]
268+ }
269+ linodeFW .SetLabels (labels )
270+ return machineScope .Client .Update (ctx , & linodeFW )
271+ }
272+
273+ // reconcilePGLabel adds the cluster-name label to placement group referenced in the machineScope if it does not already exist
274+ func (r * LinodeMachineReconciler ) reconcilePGLabel (ctx context.Context , logger logr.Logger , machineScope * scope.MachineScope ) error {
275+
276+ if machineScope .LinodeMachine .Spec .PlacementGroupRef == nil {
277+ logger .Info ("Paused reconciliation is skipped due to missing placement group ref" )
278+ return nil
279+ }
280+ linodePG := infrav1alpha2.LinodePlacementGroup {
281+ ObjectMeta : metav1.ObjectMeta {
282+ Namespace : machineScope .LinodeMachine .Spec .PlacementGroupRef .Namespace ,
283+ Name : machineScope .LinodeMachine .Spec .PlacementGroupRef .Name ,
284+ },
285+ }
286+ if err := machineScope .Client .Get (ctx , client .ObjectKeyFromObject (& linodePG ), & linodePG ); err != nil {
287+ return err
288+ }
289+ labels := linodePG .ObjectMeta .GetLabels ()
290+ if labels == nil {
291+ labels = map [string ]string {}
292+ }
293+ if _ , ok := labels [clusterv1 .ClusterNameLabel ]; ok {
294+ return nil
295+ }
296+ labels [clusterv1 .ClusterNameLabel ] = machineScope .Machine .Labels [clusterv1 .ClusterNameLabel ]
297+ linodePG .ObjectMeta .SetLabels (labels )
298+ return machineScope .Client .Update (ctx , & linodePG )
299+ }
300+
301+ // reconcileLabel adds the cluster-name label to placement groups and firewalls that are difference in the machine spec
302+ // to ensure that they are represented as part of the cluster
303+ func (r * LinodeMachineReconciler ) reconcileLabels (ctx context.Context , logger logr.Logger , machineScope * scope.MachineScope ) error {
304+ if err := r .reconcilePGLabel (ctx , logger , machineScope ); err != nil {
305+ return fmt .Errorf ("failed to label referenced placement group: %w" , err )
306+ }
307+ if err := r .reconcileFirewallLabel (ctx , logger , machineScope ); err != nil {
308+ return fmt .Errorf ("failed to label referenced firewall: %w" , err )
255309 }
256310 return nil
257311}
@@ -322,11 +376,21 @@ func (r *LinodeMachineReconciler) reconcile(ctx context.Context, logger logr.Log
322376 }
323377 }
324378
379+ // Add labels to referenced resources
380+ if err := r .reconcileLabels (ctx , logger , machineScope ); err != nil {
381+ return ctrl.Result {}, err
382+ }
383+
325384 // Pause
326385 if err := r .reconcilePause (ctx , logger , machineScope ); err != nil {
327386 return ctrl.Result {}, err
328387 }
329388
389+ // Stop if paused
390+ if reconciler .IsPaused (machineScope .Cluster , machineScope .Machine ) {
391+ logger .Info ("LinodeMachine or linked cluster is marked as paused, won't reconcile." )
392+ return ctrl.Result {}, nil
393+ }
330394 // Delete
331395 if ! machineScope .LinodeMachine .ObjectMeta .DeletionTimestamp .IsZero () {
332396 failureReason = util .DeleteError
0 commit comments