Skip to content

Commit

Permalink
cloud/scope: fix: only patch objects missing finalizer (#90)
Browse files Browse the repository at this point in the history
Fixes the `AddFinalizers` scope methods to only patch their objects when the finalizer is
missing. This prevents unncessary `resourceVersion` changes to the object.

Co-authored-by: Ashley Dumaine <[email protected]>
  • Loading branch information
cbzzz and AshleyDumaine authored Feb 8, 2024
1 parent 13c3739 commit fdb9474
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
9 changes: 6 additions & 3 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ func (s *ClusterScope) Close(ctx context.Context) error {
return s.PatchObject(ctx)
}

// AddFinalizer adds a finalizer and immediately patches the object to avoid any race conditions
// AddFinalizer adds a finalizer if not present and immediately patches the
// object to avoid any race conditions.
func (s *ClusterScope) AddFinalizer(ctx context.Context) error {
controllerutil.AddFinalizer(s.LinodeCluster, infrav1alpha1.GroupVersion.String())
if controllerutil.AddFinalizer(s.LinodeCluster, infrav1alpha1.GroupVersion.String()) {
return s.Close(ctx)
}

return s.Close(ctx)
return nil
}
9 changes: 6 additions & 3 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ func (s *MachineScope) Close(ctx context.Context) error {
return s.PatchObject(ctx)
}

// AddFinalizer adds a finalizer and immediately patches the object to avoid any race conditions
// AddFinalizer adds a finalizer if not present and immediately patches the
// object to avoid any race conditions.
func (s *MachineScope) AddFinalizer(ctx context.Context) error {
controllerutil.AddFinalizer(s.LinodeMachine, infrav1alpha1.GroupVersion.String())
if controllerutil.AddFinalizer(s.LinodeMachine, infrav1alpha1.GroupVersion.String()) {
return s.Close(ctx)
}

return s.Close(ctx)
return nil
}

// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
Expand Down
9 changes: 6 additions & 3 deletions cloud/scope/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ func (s *VPCScope) Close(ctx context.Context) error {
return s.PatchObject(ctx)
}

// AddFinalizer adds a finalizer and immediately patches the object to avoid any race conditions
// AddFinalizer adds a finalizer if not present and immediately patches the
// object to avoid any race conditions.
func (s *VPCScope) AddFinalizer(ctx context.Context) error {
controllerutil.AddFinalizer(s.LinodeVPC, infrav1alpha1.GroupVersion.String())
if controllerutil.AddFinalizer(s.LinodeVPC, infrav1alpha1.GroupVersion.String()) {
return s.Close(ctx)
}

return s.Close(ctx)
return nil
}

0 comments on commit fdb9474

Please sign in to comment.