This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
finalizer.go
180 lines (134 loc) · 4.36 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright 2019, EnMasse authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package finalizer
import (
"context"
"fmt"
"github.com/pkg/errors"
"k8s.io/client-go/tools/record"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
var log = logf.Log.WithName("finalizer")
type DeconstructorContext struct {
Context context.Context
Client client.Client
Reader client.Reader
Object runtime.Object
Recorder record.EventRecorder
}
type Finalizer struct {
// the name of the finalizer
Name string
// The deconstructor function, which will be called when the finalizer
// is still present on the object, and the deletion timestamp is set.
// When the deconstructor is finished, or had nothing to do, it must return
// a nil error, and a result which does not request a requeue.
//
// If the error is non-nil, or a requeue was requested, the finalizer will
// *not* be removed from the list.
Deconstruct func(ctx DeconstructorContext) (reconcile.Result, error)
}
func hasFinalizer(name string, list []string) bool {
for _, e := range list {
if name == e {
return true
}
}
return false
}
func removeFinalizer(name string, list []string) []string {
if list == nil {
return list
}
// removing an entry from an array in go is tricky ...
for i := len(list) - 1; i >= 0; i-- {
v := list[i]
if v == name {
list = append(
list[:i],
list[i+1:]...,
)
}
}
return list
}
// The function will process finalizers of the object.
//
// If the deletion timestamp is nil, then it will add all names of all missing finalizers, and update the object if
// this caused a change.
//
// If the deletion timestamp is non-nil, it will iterate over the finalizers, and call the destructor for the first
// finalizer it still finds in the list (in any order). If the deconstructor returns as "finished", then will remove
// the finalizer from the list, and request to be re-queued.
//
// **Note:** When the function requests to be re-queue, the object's finalizer list was changed, and this change
// must be persisted by the caller!
func ProcessFinalizers(ctx context.Context, client client.Client, reader client.Reader, recorder record.EventRecorder, obj runtime.Object, finalizers []Finalizer) (reconcile.Result, error) {
object, ok := obj.(v1.Object)
if !ok {
return reconcile.Result{}, fmt.Errorf("unable to process an object, which is no a v1.Object")
}
if object.GetDeletionTimestamp() == nil {
// the object it _not_ scheduled for deletion
changed := false
current := object.GetFinalizers()
// iterate over the list of expected finalizers
for _, f := range finalizers {
if !hasFinalizer(f.Name, current) {
// if the finalizer is missing, add it
changed = true
current = append(current, f.Name)
}
}
if changed {
// the list of finalizers has changed, update and return
object.SetFinalizers(current)
log.Info("Re-queue: added finalizer")
return reconcile.Result{Requeue: true}, nil
}
} else {
// the object _is_ scheduled for deletion
current := object.GetFinalizers()
for _, f := range finalizers {
if hasFinalizer(f.Name, current) {
// if the finalizer is still present ...
// ... run the deconstructor
var result = reconcile.Result{}
var err error = nil
if f.Deconstruct != nil {
result, err = f.Deconstruct(DeconstructorContext{
Context: ctx,
Client: client,
Reader: reader,
Object: obj,
Recorder: recorder,
})
}
// process the result
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "Failed processing finalizers")
}
if !result.Requeue && result.RequeueAfter <= 0 {
// we had no error, and do not need to check again, so remove the finalizer
// remove it from the list, and set the update
c := current
object.SetFinalizers(removeFinalizer(f.Name, c))
// persist, and re-schedule for the next finalizer
log.Info("Re-queue: removed finalizer")
return reconcile.Result{Requeue: true}, nil
} else {
// return the re-queue request
return result, nil
}
}
}
}
// nothing had to be done
return reconcile.Result{}, nil
}