Skip to content

Commit

Permalink
Merge pull request #2672 from justinsb/eager_finalizers
Browse files Browse the repository at this point in the history
fix: write finalizers eagerly in direct controller
  • Loading branch information
google-oss-prow[bot] committed Sep 11, 2024
2 parents b7089ad + b0bb993 commit c5b8b87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/controller/direct/directbase/directbase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ func (r *reconcileContext) doReconcile(ctx context.Context, u *unstructured.Unst

// add finalizers for deletion defender to make sure we don't delete cloud provider resources when uninstalling
if u.GetDeletionTimestamp().IsZero() {
k8s.EnsureFinalizers(u, k8s.ControllerFinalizerName, k8s.DeletionDefenderFinalizerName)
if err := r.ensureFinalizers(ctx, u); err != nil {
return false, nil
}
}

return false, nil
Expand Down Expand Up @@ -282,7 +284,9 @@ func (r *reconcileContext) doReconcile(ctx context.Context, u *unstructured.Unst
return false, r.handleDeleted(ctx, u)
}

k8s.EnsureFinalizers(u, k8s.ControllerFinalizerName, k8s.DeletionDefenderFinalizerName)
if err := r.ensureFinalizers(ctx, u); err != nil {
return false, err
}

// set the etag to an empty string, since IAMPolicy is the authoritative intent, KCC wants to overwrite the underlying policy regardless
//policy.Spec.Etag = ""
Expand Down Expand Up @@ -312,6 +316,21 @@ func (r *reconcileContext) doReconcile(ctx context.Context, u *unstructured.Unst
return false, nil
}

// ensureFinalizers will apply our finalizers to the object if they are not present.
// We update the kube-apiserver immediately if any changes are needed.
func (r *reconcileContext) ensureFinalizers(ctx context.Context, u *unstructured.Unstructured) error {
if k8s.EnsureFinalizers(u, k8s.ControllerFinalizerName, k8s.DeletionDefenderFinalizerName) {
// No change
return nil
}

if err := r.Reconciler.Client.Update(ctx, u); err != nil {
return fmt.Errorf("updating finalizers: %w", err)
}

return nil
}

func (r *reconcileContext) handleUpToDate(ctx context.Context, u *unstructured.Unstructured) error {
resource, err := toK8sResource(u)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/k8s/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func EnsureFinalizer(o metav1.Object, finalizer string) (found bool) {
return false
}

// EnsureFinalizers adds the specified finalizers, returning true if the finalizers were already present (i.e. no changes)
func EnsureFinalizers(o metav1.Object, finalizers ...string) (found bool) {
found = true
for _, f := range finalizers {
Expand Down

0 comments on commit c5b8b87

Please sign in to comment.