@@ -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
@@ -113,9 +114,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
113
114
r .updateState (log , inst , snsInst )
114
115
115
116
// Handle the case where the anchor is being deleted.
116
- if deleting , err := r .onDeleting (ctx , log , inst , snsInst ); deleting {
117
- return ctrl.Result {}, err
117
+ if ! inst .DeletionTimestamp .IsZero () {
118
+ // Stop reconciliation as anchor is being deleted
119
+ return ctrl.Result {}, r .onDeleting (ctx , log , inst , snsInst )
118
120
}
121
+ // Add finalizers on all non-forbidden anchors to ensure it's not deleted until
122
+ // after the subnamespace is deleted.
123
+ controllerutil .AddFinalizer (inst , api .MetaGroup )
119
124
120
125
// If the subnamespace doesn't exist, create it.
121
126
if inst .Status .State == api .Missing {
@@ -130,9 +135,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
130
135
}
131
136
}
132
137
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
138
return ctrl.Result {}, r .writeInstance (ctx , log , inst )
137
139
}
138
140
@@ -145,19 +147,14 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
145
147
// There are several conditions where we skip step 1 - for example, if we're uninstalling HNC, or
146
148
// if allowCascadingDeletion is disabled but the subnamespace has descendants (see
147
149
// shouldDeleteSubns for details). In such cases, we move straight to step 2.
148
- 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.
150
- if inst .DeletionTimestamp .IsZero () {
151
- return false , nil
152
- }
153
-
150
+ func (r * Reconciler ) onDeleting (ctx context.Context , log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) error {
154
151
// We handle deletions differently depending on whether _one_ anchor is being deleted (i.e., the
155
152
// user wants to delete the namespace) or whether the Anchor CRD is being deleted, which usually
156
153
// means HNC is being uninstalled and we shouldn't delete _any_ namespaces.
157
154
deletingCRD , err := crd .IsDeletingCRD (ctx , api .Anchors )
158
155
if err != nil {
159
156
log .Error (err , "Couldn't determine if CRD is being deleted" )
160
- return false , err
157
+ return err
161
158
}
162
159
log .V (1 ).Info ("Anchor is being deleted" , "deletingCRD" , deletingCRD )
163
160
@@ -166,21 +163,21 @@ func (r *Reconciler) onDeleting(ctx context.Context, log logr.Logger, inst *api.
166
163
switch {
167
164
case r .shouldDeleteSubns (log , inst , snsInst , deletingCRD ):
168
165
log .Info ("Deleting subnamespace due to anchor being deleted" )
169
- return true , r .deleteNamespace (ctx , log , snsInst )
166
+ return r .deleteNamespace (ctx , log , snsInst )
170
167
case r .shouldFinalizeAnchor (log , inst , snsInst ):
171
168
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 )
169
+ controllerutil . RemoveFinalizer ( inst , api . MetaGroup )
170
+ return r .writeInstance (ctx , log , inst )
174
171
default :
175
172
// There's nothing to do; we're just waiting for something to happen. Print out a log message
176
173
// indicating what we're waiting for.
177
- if len (inst . ObjectMeta . Finalizers ) > 0 {
174
+ if controllerutil . ContainsFinalizer (inst , api . MetaGroup ) {
178
175
log .Info ("Waiting for subnamespace to be fully purged before letting the anchor be deleted" )
179
176
} else {
180
177
// I doubt we'll ever get here but I suppose it's possible
181
178
log .Info ("Waiting for K8s to delete this anchor (all finalizers are removed)" )
182
179
}
183
- return true , nil
180
+ return nil
184
181
}
185
182
}
186
183
@@ -213,7 +210,7 @@ func (r *Reconciler) shouldDeleteSubns(log logr.Logger, inst *api.SubnamespaceAn
213
210
log .V (1 ).Info ("The subnamespace is already being deleted; no need to delete again" )
214
211
return false
215
212
}
216
- if len (inst . ObjectMeta . Finalizers ) == 0 {
213
+ if ! controllerutil . ContainsFinalizer (inst , api . MetaGroup ) {
217
214
log .V (1 ).Info ("The anchor has already been finalized; do not reconsider deleting the namespace" )
218
215
return false
219
216
}
@@ -257,7 +254,7 @@ func (r *Reconciler) shouldDeleteSubns(log logr.Logger, inst *api.SubnamespaceAn
257
254
// deleted, it's in the process of being deleted, etc).
258
255
func (r * Reconciler ) shouldFinalizeAnchor (log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) bool {
259
256
// If the anchor is already finalized, there's no need to do it again.
260
- if len (inst . ObjectMeta . Finalizers ) == 0 {
257
+ if ! controllerutil . ContainsFinalizer (inst , api . MetaGroup ) {
261
258
return false
262
259
}
263
260
0 commit comments