|
| 1 | +// Copyright Red Hat, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the License); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an AS IS BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package finalizer |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 22 | + |
| 23 | + "github.com/openshift-service-mesh/federation/internal/controller" |
| 24 | +) |
| 25 | + |
| 26 | +// CleanupFn is a closure which can be used in the reconciler to define finalizer logic just before |
| 27 | +// the object is removed from kube-apiserver. |
| 28 | +type CleanupFn func() error |
| 29 | + |
| 30 | +// Handler encapsulates finalizer handling. It can: |
| 31 | +// - add finalizer to a given object and persist it |
| 32 | +// - perform cleanup defined as CleanupFn if the object is marked for deletion |
| 33 | +// |
| 34 | +// Example usage in the controller reconcile loop: |
| 35 | +// |
| 36 | +// finalizerHandler := finalizer.NewHandler(r.Client, "federation.openshift-service-mesh.io/mesh-federation") |
| 37 | +// if finalized, errFinalize := finalizerHandler.Finalize(ctx, meshFederation, func() error { |
| 38 | +// // finalizer logic |
| 39 | +// return nil |
| 40 | +// }); finalized { |
| 41 | +// return ctrl.Result{}, errFinalize |
| 42 | +// } |
| 43 | +// |
| 44 | +// if finalizerAlreadyExists, errAdd := finalizerHandler.Add(ctx, meshFederation); !finalizerAlreadyExists { |
| 45 | +// return ctrl.Result{}, errAdd |
| 46 | +// } |
| 47 | +type Handler struct { |
| 48 | + cl client.Client |
| 49 | + finalizerName string |
| 50 | +} |
| 51 | + |
| 52 | +func NewHandler(cl client.Client, finalizerName string) *Handler { |
| 53 | + return &Handler{ |
| 54 | + cl: cl, |
| 55 | + finalizerName: finalizerName, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Add adds the defined finalizer to the object and then persists it if the finalizer was not already present. |
| 60 | +// Returns true if the finalizer was already present. |
| 61 | +// Returns an error if updating the object failed. |
| 62 | +func (f *Handler) Add(ctx context.Context, obj client.Object) (bool, error) { |
| 63 | + if finalizersUpdated := controllerutil.AddFinalizer(obj, f.finalizerName); !finalizersUpdated { |
| 64 | + return true, nil // Finalizer already exists, no need to add it |
| 65 | + } |
| 66 | + |
| 67 | + _, errRetry := controller.RetryUpdate(ctx, f.cl, obj, func(saved client.Object) { |
| 68 | + controllerutil.AddFinalizer(saved, f.finalizerName) // in case of conflict retry adding finalizer on the obj fetched from cluster |
| 69 | + }) |
| 70 | + |
| 71 | + return false, errRetry |
| 72 | +} |
| 73 | + |
| 74 | +// Finalize executes cleanup logic defined in cleanupFn only if the object is marked for deletion. |
| 75 | +// Returns true if finalizer logic was attempted. |
| 76 | +// Returns an error if the cleanup function was unsuccessful or the removal of the finalizer failed. |
| 77 | +func (f *Handler) Finalize(ctx context.Context, obj client.Object, cleanupFn CleanupFn) (bool, error) { |
| 78 | + if finalizeNeeded := !obj.GetDeletionTimestamp().IsZero() && controllerutil.ContainsFinalizer(obj, f.finalizerName); !finalizeNeeded { |
| 79 | + return false, nil |
| 80 | + } |
| 81 | + |
| 82 | + if err := cleanupFn(); err != nil { |
| 83 | + return true, err |
| 84 | + } |
| 85 | + |
| 86 | + _, errRetry := controller.RetryUpdate(ctx, f.cl, obj, func(saved client.Object) { |
| 87 | + controllerutil.RemoveFinalizer(saved, f.finalizerName) // in case of conflict retry removing finalizer on the obj fetched from cluster |
| 88 | + }) |
| 89 | + |
| 90 | + return true, errRetry |
| 91 | +} |
0 commit comments