@@ -17,10 +17,11 @@ package reconcilers
17
17
18
18
import (
19
19
"context"
20
+ "errors"
20
21
21
22
"github.com/go-logr/logr"
22
23
corev1 "k8s.io/api/core/v1"
23
- "k8s.io/apimachinery/pkg/api/errors"
24
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
24
25
"k8s.io/apimachinery/pkg/types"
25
26
ctrl "sigs.k8s.io/controller-runtime"
26
27
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -65,7 +66,7 @@ func (r *AnchorReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
65
66
// purged.
66
67
inst , err := r .getInstance (ctx , pnm , nm )
67
68
if err != nil {
68
- if errors .IsNotFound (err ) {
69
+ if apierrors .IsNotFound (err ) {
69
70
return ctrl.Result {}, nil
70
71
}
71
72
return ctrl.Result {}, err
@@ -118,13 +119,19 @@ func (r *AnchorReconciler) onDeleting(ctx context.Context, log logr.Logger, inst
118
119
return false , err
119
120
}
120
121
122
+ // Update the state of the anchor
123
+ //
124
+ // TODO: call this in the parent function after v0.5 so there's no special code path for the
125
+ // onDeleting case.
126
+ r .updateState (log , inst , snsInst )
127
+
121
128
log .Info ("The anchor is being deleted" , "deletingCRD" , deletingCRD )
122
129
switch {
123
130
case len (inst .ObjectMeta .Finalizers ) == 0 :
124
131
// We've finished processing this, nothing to do.
125
132
log .Info ("Do nothing since the finalizers are already gone." )
126
133
return true , nil
127
- case r .shouldDeleteSubns (inst , snsInst , deletingCRD ):
134
+ case r .shouldDeleteSubns (log , inst , snsInst , deletingCRD ):
128
135
// The subnamespace is not already being deleted but it allows cascadingDelete or it's a leaf.
129
136
// Delete the subnamespace, unless the CRD is being deleted, in which case, we want to leave the
130
137
// namespaces alone.
@@ -143,7 +150,9 @@ func (r *AnchorReconciler) onDeleting(ctx context.Context, log logr.Logger, inst
143
150
144
151
// shouldDeleteSubns returns true if the namespace still exists and it is a leaf
145
152
// subnamespace or it allows cascading delete unless the CRD is being deleted.
146
- func (r * AnchorReconciler ) shouldDeleteSubns (inst * api.SubnamespaceAnchor , nsInst * corev1.Namespace , deletingCRD bool ) bool {
153
+ //
154
+ // TODO: fix comment post-v0.5
155
+ func (r * AnchorReconciler ) shouldDeleteSubns (log logr.Logger , inst * api.SubnamespaceAnchor , nsInst * corev1.Namespace , deletingCRD bool ) bool {
147
156
r .forest .Lock ()
148
157
defer r .forest .Unlock ()
149
158
@@ -152,6 +161,27 @@ func (r *AnchorReconciler) shouldDeleteSubns(inst *api.SubnamespaceAnchor, nsIns
152
161
return false
153
162
}
154
163
164
+ // If the anchor isn't in the "ok" state, don't delete the namespace. If it's "conflict," we
165
+ // *definitely* don't want to delete it; if it's missing (and possibly being created), then there
166
+ // isn't really a good option but we'll eventually put a condition on the namespace so it's not a
167
+ // big deal.
168
+ //
169
+ // TODO: much of the remaining portion of this function can probably be replaced by this switch
170
+ // statement. In v0.5, we're playing it safe so I won't modify the rest of the function, but in
171
+ // v0.6 we should restructure.
172
+ switch inst .Status .State {
173
+ case api .Missing :
174
+ return false
175
+ case api .Conflict :
176
+ return false
177
+ case api .Ok :
178
+ // keep going...
179
+ default :
180
+ log .Error (errors .New ("Illegal state" ), "Unknown state" , "state" , inst .Status .State )
181
+ // Stay on the safe side - don't delete
182
+ return false
183
+ }
184
+
155
185
cnm := inst .Name
156
186
pnm := inst .Namespace
157
187
cns := r .forest .Get (cnm )
@@ -259,7 +289,7 @@ func (r *AnchorReconciler) getNamespace(ctx context.Context, nm string) (*corev1
259
289
ns := & corev1.Namespace {}
260
290
nnm := types.NamespacedName {Name : nm }
261
291
if err := r .Get (ctx , nnm , ns ); err != nil {
262
- if ! errors .IsNotFound (err ) {
292
+ if ! apierrors .IsNotFound (err ) {
263
293
return nil , err
264
294
}
265
295
return & corev1.Namespace {}, nil
0 commit comments