Skip to content

Commit f310749

Browse files
authored
Merge pull request #4095 from luthermonson/cleanup-patches
cleaning up unnecessary object patches
2 parents 9d1f067 + c2aaf06 commit f310749

File tree

7 files changed

+30
-42
lines changed

7 files changed

+30
-42
lines changed

controllers/awscluster_controller.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import (
5555
"sigs.k8s.io/cluster-api/util"
5656
capiannotations "sigs.k8s.io/cluster-api/util/annotations"
5757
"sigs.k8s.io/cluster-api/util/conditions"
58-
"sigs.k8s.io/cluster-api/util/patch"
5958
"sigs.k8s.io/cluster-api/util/predicates"
6059
)
6160

@@ -166,24 +165,6 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
166165
}
167166

168167
log = log.WithValues("cluster", klog.KObj(cluster))
169-
helper, err := patch.NewHelper(awsCluster, r.Client)
170-
if err != nil {
171-
return reconcile.Result{}, errors.Wrap(err, "failed to init patch helper")
172-
}
173-
174-
defer func() {
175-
e := helper.Patch(
176-
context.TODO(),
177-
awsCluster,
178-
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
179-
infrav1.PrincipalCredentialRetrievedCondition,
180-
infrav1.PrincipalUsageAllowedCondition,
181-
infrav1.LoadBalancerReadyCondition,
182-
}})
183-
if e != nil {
184-
fmt.Println(e.Error())
185-
}
186-
}()
187168

188169
// Create the scope.
189170
clusterScope, err := scope.NewClusterScope(scope.ClusterScopeParams{
@@ -274,10 +255,11 @@ func (r *AWSClusterReconciler) reconcileNormal(clusterScope *scope.ClusterScope)
274255
awsCluster := clusterScope.AWSCluster
275256

276257
// If the AWSCluster doesn't have our finalizer, add it.
277-
controllerutil.AddFinalizer(awsCluster, infrav1.ClusterFinalizer)
278-
// Register the finalizer immediately to avoid orphaning AWS resources on delete
279-
if err := clusterScope.PatchObject(); err != nil {
280-
return reconcile.Result{}, err
258+
if controllerutil.AddFinalizer(awsCluster, infrav1.ClusterFinalizer) {
259+
// Register the finalizer immediately to avoid orphaning AWS resources on delete
260+
if err := clusterScope.PatchObject(); err != nil {
261+
return reconcile.Result{}, err
262+
}
281263
}
282264

283265
ec2Service := r.getEC2Service(clusterScope)

controllers/awsmachine_controller.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,14 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
470470
}
471471

472472
// If the AWSMachine doesn't have our finalizer, add it.
473-
controllerutil.AddFinalizer(machineScope.AWSMachine, infrav1.MachineFinalizer)
474-
// Register the finalizer after first read operation from AWS to avoid orphaning AWS resources on delete
475-
if err := machineScope.PatchObject(); err != nil {
476-
machineScope.Error(err, "unable to patch object")
477-
return ctrl.Result{}, err
473+
if controllerutil.AddFinalizer(machineScope.AWSMachine, infrav1.MachineFinalizer) {
474+
// Register the finalizer after first read operation from AWS to avoid orphaning AWS resources on delete
475+
if err := machineScope.PatchObject(); err != nil {
476+
machineScope.Error(err, "unable to patch object")
477+
return ctrl.Result{}, err
478+
}
478479
}
480+
479481
// Create new instance since providerId is nil and instance could not be found by tags.
480482
if instance == nil {
481483
// Avoid a flickering condition between InstanceProvisionStarted and InstanceProvisionFailed if there's a persistent failure with createInstance

controlplane/eks/controllers/awsmanagedcontrolplane_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ func (r *AWSManagedControlPlaneReconciler) reconcileNormal(ctx context.Context,
241241

242242
awsManagedControlPlane := managedScope.ControlPlane
243243

244-
controllerutil.AddFinalizer(managedScope.ControlPlane, ekscontrolplanev1.ManagedControlPlaneFinalizer)
245-
if err := managedScope.PatchObject(); err != nil {
246-
return ctrl.Result{}, err
244+
if controllerutil.AddFinalizer(managedScope.ControlPlane, ekscontrolplanev1.ManagedControlPlaneFinalizer) {
245+
if err := managedScope.PatchObject(); err != nil {
246+
return ctrl.Result{}, err
247+
}
247248
}
248249

249250
ec2Service := ec2.NewService(managedScope)

exp/controllers/awsfargatepool_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ func (r *AWSFargateProfileReconciler) reconcileNormal(
147147
) (ctrl.Result, error) {
148148
fargateProfileScope.Info("Reconciling AWSFargateProfile")
149149

150-
controllerutil.AddFinalizer(fargateProfileScope.FargateProfile, expinfrav1.FargateProfileFinalizer)
151-
if err := fargateProfileScope.PatchObject(); err != nil {
152-
return ctrl.Result{}, err
150+
if controllerutil.AddFinalizer(fargateProfileScope.FargateProfile, expinfrav1.FargateProfileFinalizer) {
151+
if err := fargateProfileScope.PatchObject(); err != nil {
152+
return ctrl.Result{}, err
153+
}
153154
}
154155

155156
ekssvc := eks.NewFargateService(fargateProfileScope)

exp/controllers/awsmachinepool_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, machineP
203203
}
204204

205205
// If the AWSMachinepool doesn't have our finalizer, add it
206-
controllerutil.AddFinalizer(machinePoolScope.AWSMachinePool, expinfrav1.MachinePoolFinalizer)
207-
208-
// Register finalizer immediately to avoid orphaning AWS resources
209-
if err := machinePoolScope.PatchObject(); err != nil {
210-
return ctrl.Result{}, err
206+
if controllerutil.AddFinalizer(machinePoolScope.AWSMachinePool, expinfrav1.MachinePoolFinalizer) {
207+
// Register finalizer immediately to avoid orphaning AWS resources
208+
if err := machinePoolScope.PatchObject(); err != nil {
209+
return ctrl.Result{}, err
210+
}
211211
}
212212

213213
if !machinePoolScope.Cluster.Status.InfrastructureReady {

exp/controllers/awsmanagedmachinepool_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ func (r *AWSManagedMachinePoolReconciler) reconcileNormal(
199199
) (ctrl.Result, error) {
200200
machinePoolScope.Info("Reconciling AWSManagedMachinePool")
201201

202-
controllerutil.AddFinalizer(machinePoolScope.ManagedMachinePool, expinfrav1.ManagedMachinePoolFinalizer)
203-
if err := machinePoolScope.PatchObject(); err != nil {
204-
return ctrl.Result{}, err
202+
if controllerutil.AddFinalizer(machinePoolScope.ManagedMachinePool, expinfrav1.ManagedMachinePoolFinalizer) {
203+
if err := machinePoolScope.PatchObject(); err != nil {
204+
return ctrl.Result{}, err
205+
}
205206
}
206207

207208
ekssvc := eks.NewNodegroupService(machinePoolScope)

pkg/cloud/scope/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ func (s *ClusterScope) PatchObject() error {
260260
infrav1.BastionHostReadyCondition,
261261
infrav1.LoadBalancerReadyCondition,
262262
infrav1.PrincipalUsageAllowedCondition,
263+
infrav1.PrincipalCredentialRetrievedCondition,
263264
}})
264265
}
265266

0 commit comments

Comments
 (0)