From fdb9474c30a6f90fe28dc6407f5e87757361e4d5 Mon Sep 17 00:00:00 2001 From: cbzzz <69888673+cbzzz@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:07:00 -0500 Subject: [PATCH] cloud/scope: fix: only patch objects missing finalizer (#90) 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 <5779804+AshleyDumaine@users.noreply.github.com> --- cloud/scope/cluster.go | 9 ++++++--- cloud/scope/machine.go | 9 ++++++--- cloud/scope/vpc.go | 9 ++++++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cloud/scope/cluster.go b/cloud/scope/cluster.go index a99557394..90d102882 100644 --- a/cloud/scope/cluster.go +++ b/cloud/scope/cluster.go @@ -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 } diff --git a/cloud/scope/machine.go b/cloud/scope/machine.go index 1857400d0..6a3bcd47d 100644 --- a/cloud/scope/machine.go +++ b/cloud/scope/machine.go @@ -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. diff --git a/cloud/scope/vpc.go b/cloud/scope/vpc.go index a5a68c208..0ffa39108 100644 --- a/cloud/scope/vpc.go +++ b/cloud/scope/vpc.go @@ -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 }