Skip to content

Commit 0a3b455

Browse files
Initial commit for finalizer helper library
Signed-off-by: rashmigottipati <[email protected]>
1 parent fca94d5 commit 0a3b455

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

pkg/finalizer/finalizer.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
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+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package finalizer
15+
16+
import (
17+
"context"
18+
"fmt"
19+
20+
"sigs.k8s.io/controller-runtime/pkg/client"
21+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
22+
)
23+
24+
func NewFinalizers() (Finalizers, error) {
25+
return finalizers(map[string]Finalizer{}), nil
26+
}
27+
28+
func (f finalizers) Register(key string, finalizer Finalizer) error {
29+
if _, ok := f[key]; ok {
30+
return fmt.Errorf("finalizer for key %q already registered", key)
31+
}
32+
f[key] = finalizer
33+
return nil
34+
}
35+
36+
func (f finalizers) Finalize(ctx context.Context, obj client.Object) (bool, error) {
37+
needsUpdate := false
38+
for key, finalizer := range f {
39+
if obj.GetDeletionTimestamp() == nil && !controllerutil.ContainsFinalizer(obj, key) {
40+
controllerutil.AddFinalizer(obj, key)
41+
needsUpdate = true
42+
} else if obj.GetDeletionTimestamp() != nil && controllerutil.ContainsFinalizer(obj, key) {
43+
_, err := finalizer.Finalize(ctx, obj)
44+
if err != nil {
45+
return true, fmt.Errorf("finalize failed for key %q: %v", key, err)
46+
}
47+
controllerutil.RemoveFinalizer(obj, key)
48+
needsUpdate = true
49+
}
50+
}
51+
return needsUpdate, nil
52+
}

pkg/finalizer/types.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
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+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package finalizer
15+
16+
import (
17+
"context"
18+
19+
"sigs.k8s.io/controller-runtime/pkg/client"
20+
)
21+
22+
type Registerer interface {
23+
Register(key string, f Finalizer) error
24+
}
25+
26+
type Finalizer interface {
27+
Finalize(context.Context, client.Object) (needsUpdate bool, err error)
28+
}
29+
30+
type finalizers map[string]Finalizer
31+
32+
type Finalizers interface {
33+
// implements Registerer and Finalizer to finalize
34+
// all registered finalizers if the provided object
35+
// has a deletion timestamp or set all registered
36+
// finalizers if it doesn't
37+
Registerer
38+
Finalizer
39+
}

0 commit comments

Comments
 (0)