@@ -26,6 +26,7 @@ import (
26
26
"k8s.io/apimachinery/pkg/types"
27
27
ctrl "sigs.k8s.io/controller-runtime"
28
28
"sigs.k8s.io/controller-runtime/pkg/client"
29
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
29
30
"sigs.k8s.io/controller-runtime/pkg/event"
30
31
"sigs.k8s.io/controller-runtime/pkg/handler"
31
32
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -81,9 +82,9 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
81
82
// Anchors in unmanaged namespace should be ignored. Make sure it
82
83
// doesn't have any finalizers, otherwise, leave it alone.
83
84
if why := config .WhyUnmanaged (pnm ); why != "" {
84
- if len (inst . ObjectMeta . Finalizers ) > 0 {
85
+ if controllerutil . ContainsFinalizer (inst , api . MetaGroup ) {
85
86
log .Info ("Removing finalizers from anchor in unmanaged namespace" , "reason" , why )
86
- inst . ObjectMeta . Finalizers = nil
87
+ controllerutil . RemoveFinalizer ( inst , api . MetaGroup )
87
88
return ctrl.Result {}, r .writeInstance (ctx , log , inst )
88
89
}
89
90
return ctrl.Result {}, nil
@@ -94,10 +95,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
94
95
// been bypassed and the anchor has been successfully created. Forbidden
95
96
// anchors won't have finalizers.
96
97
if why := config .WhyUnmanaged (nm ); why != "" {
97
- if inst .Status .State != api .Forbidden || len (inst . ObjectMeta . Finalizers ) > 0 {
98
+ if inst .Status .State != api .Forbidden || controllerutil . ContainsFinalizer (inst , api . MetaGroup ) {
98
99
log .Info ("Setting forbidden state on anchor with unmanaged name" , "reason" , why )
99
100
inst .Status .State = api .Forbidden
100
- inst . ObjectMeta . Finalizers = nil
101
+ controllerutil . RemoveFinalizer ( inst , api . MetaGroup )
101
102
return ctrl.Result {}, r .writeInstance (ctx , log , inst )
102
103
}
103
104
return ctrl.Result {}, nil
@@ -130,9 +131,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
130
131
}
131
132
}
132
133
133
- // Add finalizers on all non-forbidden anchors to ensure it's not deleted until
134
- // after the subnamespace is deleted.
135
- inst .ObjectMeta .Finalizers = []string {api .MetaGroup }
136
134
return ctrl.Result {}, r .writeInstance (ctx , log , inst )
137
135
}
138
136
@@ -146,8 +144,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
146
144
// if allowCascadingDeletion is disabled but the subnamespace has descendants (see
147
145
// shouldDeleteSubns for details). In such cases, we move straight to step 2.
148
146
func (r * Reconciler ) onDeleting (ctx context.Context , log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) (bool , error ) {
149
- // Early exit and continue reconciliation if the instance is not being deleted.
147
+ // Early exit, ensure finalizer is present and continue reconciliation if the instance is not being deleted.
150
148
if inst .DeletionTimestamp .IsZero () {
149
+ // Add finalizers on all non-forbidden anchors to ensure it's not deleted until
150
+ // after the subnamespace is deleted.
151
+ if ! controllerutil .ContainsFinalizer (inst , api .MetaGroup ) {
152
+ controllerutil .AddFinalizer (inst , api .MetaGroup )
153
+ }
151
154
return false , nil
152
155
}
153
156
@@ -168,13 +171,16 @@ func (r *Reconciler) onDeleting(ctx context.Context, log logr.Logger, inst *api.
168
171
log .Info ("Deleting subnamespace due to anchor being deleted" )
169
172
return true , r .deleteNamespace (ctx , log , snsInst )
170
173
case r .shouldFinalizeAnchor (log , inst , snsInst ):
171
- log .V (1 ).Info ("Unblocking deletion" ) // V(1) since we'll very shortly show an "anchor deleted" message
172
- inst .ObjectMeta .Finalizers = nil
173
- return true , r .writeInstance (ctx , log , inst )
174
+ if controllerutil .ContainsFinalizer (inst , api .MetaGroup ) {
175
+ log .V (1 ).Info ("Unblocking deletion" ) // V(1) since we'll very shortly show an "anchor deleted" message
176
+ controllerutil .RemoveFinalizer (inst , api .MetaGroup )
177
+ return true , r .writeInstance (ctx , log , inst )
178
+ }
179
+ return true , nil
174
180
default :
175
181
// There's nothing to do; we're just waiting for something to happen. Print out a log message
176
182
// indicating what we're waiting for.
177
- if len (inst . ObjectMeta . Finalizers ) > 0 {
183
+ if controllerutil . ContainsFinalizer (inst , api . MetaGroup ) {
178
184
log .Info ("Waiting for subnamespace to be fully purged before letting the anchor be deleted" )
179
185
} else {
180
186
// I doubt we'll ever get here but I suppose it's possible
@@ -213,7 +219,7 @@ func (r *Reconciler) shouldDeleteSubns(log logr.Logger, inst *api.SubnamespaceAn
213
219
log .V (1 ).Info ("The subnamespace is already being deleted; no need to delete again" )
214
220
return false
215
221
}
216
- if len (inst . ObjectMeta . Finalizers ) == 0 {
222
+ if ! controllerutil . ContainsFinalizer (inst , api . MetaGroup ) {
217
223
log .V (1 ).Info ("The anchor has already been finalized; do not reconsider deleting the namespace" )
218
224
return false
219
225
}
@@ -257,7 +263,7 @@ func (r *Reconciler) shouldDeleteSubns(log logr.Logger, inst *api.SubnamespaceAn
257
263
// deleted, it's in the process of being deleted, etc).
258
264
func (r * Reconciler ) shouldFinalizeAnchor (log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) bool {
259
265
// If the anchor is already finalized, there's no need to do it again.
260
- if len (inst . ObjectMeta . Finalizers ) == 0 {
266
+ if ! controllerutil . ContainsFinalizer (inst , api . MetaGroup ) {
261
267
return false
262
268
}
263
269
0 commit comments