@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
13
13
limitations under the License.
14
14
*/
15
15
16
- package reconcilers
16
+ package anchor
17
17
18
18
import (
19
19
"context"
@@ -33,17 +33,19 @@ import (
33
33
34
34
api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2"
35
35
"sigs.k8s.io/hierarchical-namespaces/internal/config"
36
+ "sigs.k8s.io/hierarchical-namespaces/internal/crd"
36
37
"sigs.k8s.io/hierarchical-namespaces/internal/forest"
38
+ "sigs.k8s.io/hierarchical-namespaces/internal/logutils"
37
39
"sigs.k8s.io/hierarchical-namespaces/internal/metadata"
38
40
)
39
41
40
- // AnchorReconciler reconciles SubnamespaceAnchor CRs to make sure all the subnamespaces are
42
+ // Reconciler reconciles SubnamespaceAnchor CRs to make sure all the subnamespaces are
41
43
// properly maintained.
42
- type AnchorReconciler struct {
44
+ type Reconciler struct {
43
45
client.Client
44
46
Log logr.Logger
45
47
46
- forest * forest.Forest
48
+ Forest * forest.Forest
47
49
48
50
// Affected is a channel of event.GenericEvent (see "Watching Channels" in
49
51
// https://book-v1.book.kubebuilder.io/beyond_basics/controller_watches.html) that is used to
@@ -53,8 +55,8 @@ type AnchorReconciler struct {
53
55
54
56
// Reconcile sets up some basic variables and then calls the business logic. It currently
55
57
// only handles the creation of the namespaces but no deletion or state reporting yet.
56
- func (r * AnchorReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
57
- log := loggerWithRID (r .Log ).WithValues ("trigger" , req .NamespacedName )
58
+ func (r * Reconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
59
+ log := logutils . WithRID (r .Log ).WithValues ("trigger" , req .NamespacedName )
58
60
log .V (1 ).Info ("Reconciling anchor" )
59
61
60
62
// Get names of the hierarchical namespace and the current namespace.
@@ -134,7 +136,7 @@ func (r *AnchorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
134
136
// There are several conditions where we skip step 1 - for example, if we're uninstalling HNC, or
135
137
// if allowCascadingDeletion is disabled but the subnamespace has descendants (see
136
138
// shouldDeleteSubns for details). In such cases, we move straight to step 2.
137
- func (r * AnchorReconciler ) onDeleting (ctx context.Context , log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) (bool , error ) {
139
+ func (r * Reconciler ) onDeleting (ctx context.Context , log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) (bool , error ) {
138
140
// Early exit and continue reconciliation if the instance is not being deleted.
139
141
if inst .DeletionTimestamp .IsZero () {
140
142
return false , nil
@@ -143,7 +145,7 @@ func (r *AnchorReconciler) onDeleting(ctx context.Context, log logr.Logger, inst
143
145
// We handle deletions differently depending on whether _one_ anchor is being deleted (i.e., the
144
146
// user wants to delete the namespace) or whether the Anchor CRD is being deleted, which usually
145
147
// means HNC is being uninstalled and we shouldn't delete _any_ namespaces.
146
- deletingCRD , err := isDeletingCRD (ctx , api .Anchors )
148
+ deletingCRD , err := crd . IsDeletingCRD (ctx , api .Anchors )
147
149
if err != nil {
148
150
log .Error (err , "Couldn't determine if CRD is being deleted" )
149
151
return false , err
@@ -180,9 +182,9 @@ func (r *AnchorReconciler) onDeleting(ctx context.Context, log logr.Logger, inst
180
182
//
181
183
// This is the only part of the deletion process that needs to access the forest, in order to check
182
184
// the recursive AllowCascadingDeletion setting.
183
- func (r * AnchorReconciler ) shouldDeleteSubns (log logr.Logger , inst * api.SubnamespaceAnchor , nsInst * corev1.Namespace , deletingCRD bool ) bool {
184
- r .forest .Lock ()
185
- defer r .forest .Unlock ()
185
+ func (r * Reconciler ) shouldDeleteSubns (log logr.Logger , inst * api.SubnamespaceAnchor , nsInst * corev1.Namespace , deletingCRD bool ) bool {
186
+ r .Forest .Lock ()
187
+ defer r .Forest .Unlock ()
186
188
187
189
// If the CRD is being deleted, we want to leave the subnamespace alone.
188
190
if deletingCRD {
@@ -211,7 +213,7 @@ func (r *AnchorReconciler) shouldDeleteSubns(log logr.Logger, inst *api.Subnames
211
213
// if ACD=true on one of their ancestors; the webhooks *should* ensure that this is always true
212
214
// before we get here, but if something slips by, we'll just leave the old subnamespace alone
213
215
// with a missing-anchor condition.
214
- cns := r .forest .Get (inst .Name )
216
+ cns := r .Forest .Get (inst .Name )
215
217
if cns .ChildNames () != nil && ! cns .AllowsCascadingDeletion () {
216
218
log .Info ("This subnamespace has descendants and allowCascadingDeletion is disabled; will not delete" )
217
219
return false
@@ -244,7 +246,7 @@ func (r *AnchorReconciler) shouldDeleteSubns(log logr.Logger, inst *api.Subnames
244
246
// shouldFinalizeAnchor determines whether the anchor is safe to delete. It should only be called once
245
247
// we know that we don't need to delete the subnamespace itself (e.g. it's already gone, it can't be
246
248
// deleted, it's in the process of being deleted, etc).
247
- func (r * AnchorReconciler ) shouldFinalizeAnchor (log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) bool {
249
+ func (r * Reconciler ) shouldFinalizeAnchor (log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) bool {
248
250
// If the anchor is already finalized, there's no need to do it again.
249
251
if len (inst .ObjectMeta .Finalizers ) == 0 {
250
252
return false
@@ -289,7 +291,7 @@ func (r *AnchorReconciler) shouldFinalizeAnchor(log logr.Logger, inst *api.Subna
289
291
}
290
292
}
291
293
292
- func (r * AnchorReconciler ) updateState (log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) {
294
+ func (r * Reconciler ) updateState (log logr.Logger , inst * api.SubnamespaceAnchor , snsInst * corev1.Namespace ) {
293
295
pnm := inst .Namespace
294
296
sOf := snsInst .Annotations [api .SubnamespaceOf ]
295
297
switch {
@@ -308,9 +310,9 @@ func (r *AnchorReconciler) updateState(log logr.Logger, inst *api.SubnamespaceAn
308
310
}
309
311
}
310
312
311
- // It enqueues a subnamespace anchor for later reconciliation. This occurs in a goroutine
313
+ // Enqueue enqueues a subnamespace anchor for later reconciliation. This occurs in a goroutine
312
314
// so the caller doesn't block; since the reconciler is never garbage-collected, this is safe.
313
- func (r * AnchorReconciler ) enqueue (log logr.Logger , nm , pnm , reason string ) {
315
+ func (r * Reconciler ) Enqueue (log logr.Logger , nm , pnm , reason string ) {
314
316
go func () {
315
317
// The watch handler doesn't care about anything except the metadata.
316
318
inst := & api.SubnamespaceAnchor {}
@@ -321,7 +323,7 @@ func (r *AnchorReconciler) enqueue(log logr.Logger, nm, pnm, reason string) {
321
323
}()
322
324
}
323
325
324
- func (r * AnchorReconciler ) getInstance (ctx context.Context , pnm , nm string ) (* api.SubnamespaceAnchor , error ) {
326
+ func (r * Reconciler ) getInstance (ctx context.Context , pnm , nm string ) (* api.SubnamespaceAnchor , error ) {
325
327
nsn := types.NamespacedName {Namespace : pnm , Name : nm }
326
328
inst := & api.SubnamespaceAnchor {}
327
329
if err := r .Get (ctx , nsn , inst ); err != nil {
@@ -330,7 +332,7 @@ func (r *AnchorReconciler) getInstance(ctx context.Context, pnm, nm string) (*ap
330
332
return inst , nil
331
333
}
332
334
333
- func (r * AnchorReconciler ) writeInstance (ctx context.Context , log logr.Logger , inst * api.SubnamespaceAnchor ) error {
335
+ func (r * Reconciler ) writeInstance (ctx context.Context , log logr.Logger , inst * api.SubnamespaceAnchor ) error {
334
336
if inst .CreationTimestamp .IsZero () {
335
337
if err := r .Create (ctx , inst ); err != nil {
336
338
log .Error (err , "while creating on apiserver" )
@@ -347,7 +349,7 @@ func (r *AnchorReconciler) writeInstance(ctx context.Context, log logr.Logger, i
347
349
348
350
// deleteInstance deletes the anchor instance. Note: Make sure there's no
349
351
// finalizers on the instance before calling this function.
350
- func (r * AnchorReconciler ) deleteInstance (ctx context.Context , log logr.Logger , inst * api.SubnamespaceAnchor ) error {
352
+ func (r * Reconciler ) deleteInstance (ctx context.Context , log logr.Logger , inst * api.SubnamespaceAnchor ) error {
351
353
if err := r .Delete (ctx , inst ); err != nil {
352
354
return fmt .Errorf ("while deleting on apiserver: %w" , err )
353
355
}
@@ -357,7 +359,7 @@ func (r *AnchorReconciler) deleteInstance(ctx context.Context, log logr.Logger,
357
359
// getNamespace returns the namespace if it exists, or returns an invalid, blank, unnamed one if it
358
360
// doesn't. This allows it to be trivially identified as a namespace that doesn't exist, and also
359
361
// allows us to easily modify it if we want to create it.
360
- func (r * AnchorReconciler ) getNamespace (ctx context.Context , nm string ) (* corev1.Namespace , error ) {
362
+ func (r * Reconciler ) getNamespace (ctx context.Context , nm string ) (* corev1.Namespace , error ) {
361
363
ns := & corev1.Namespace {}
362
364
nnm := types.NamespacedName {Name : nm }
363
365
if err := r .Get (ctx , nnm , ns ); err != nil {
@@ -369,7 +371,7 @@ func (r *AnchorReconciler) getNamespace(ctx context.Context, nm string) (*corev1
369
371
return ns , nil
370
372
}
371
373
372
- func (r * AnchorReconciler ) writeNamespace (ctx context.Context , log logr.Logger , nm , pnm string ) error {
374
+ func (r * Reconciler ) writeNamespace (ctx context.Context , log logr.Logger , nm , pnm string ) error {
373
375
inst := & corev1.Namespace {}
374
376
inst .ObjectMeta .Name = nm
375
377
metadata .SetAnnotation (inst , api .SubnamespaceOf , pnm )
@@ -386,15 +388,15 @@ func (r *AnchorReconciler) writeNamespace(ctx context.Context, log logr.Logger,
386
388
return nil
387
389
}
388
390
389
- func (r * AnchorReconciler ) deleteNamespace (ctx context.Context , log logr.Logger , inst * corev1.Namespace ) error {
391
+ func (r * Reconciler ) deleteNamespace (ctx context.Context , log logr.Logger , inst * corev1.Namespace ) error {
390
392
if err := r .Delete (ctx , inst ); err != nil {
391
393
log .Error (err , "While deleting subnamespace" )
392
394
return err
393
395
}
394
396
return nil
395
397
}
396
398
397
- func (r * AnchorReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
399
+ func (r * Reconciler ) SetupWithManager (mgr ctrl.Manager ) error {
398
400
// Maps an subnamespace to its anchor in the parent namespace.
399
401
nsMapFn := func (obj client.Object ) []reconcile.Request {
400
402
if obj .GetAnnotations ()[api .SubnamespaceOf ] == "" {
0 commit comments