generated from cloudoperators/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
finalizer.go
35 lines (30 loc) · 1.02 KB
/
finalizer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0
package clientutil
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// EnsureFinalizer ensures a finalizer is present on the object. Returns an error on failure.
func EnsureFinalizer(ctx context.Context, c client.Client, o client.Object, finalizer string) error {
if controllerutil.ContainsFinalizer(o, finalizer) {
return nil
}
_, err := Patch(ctx, c, o, func() error {
controllerutil.AddFinalizer(o, finalizer)
return nil
})
return err
}
// RemoveFinalizer removes a finalizer from an object. Returns an error on failure.
func RemoveFinalizer(ctx context.Context, c client.Client, o client.Object, finalizer string) error {
if !controllerutil.ContainsFinalizer(o, finalizer) {
return nil
}
_, err := Patch(ctx, c, o, func() error {
controllerutil.RemoveFinalizer(o, finalizer)
return nil
})
return err
}