@@ -99,8 +99,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
9999 log := loggerWithRID (r .Log ).WithValues ("ns" , ns )
100100
101101 // Early exit if it's an excluded namespace
102- if ! config .IsNamespaceIncluded (ns ) {
103- return ctrl.Result {}, r .handleExcludedNamespace (ctx , log , ns )
102+ if ! config .IsManagedNamespace (ns ) {
103+ return ctrl.Result {}, r .handleUnmanaged (ctx , log , ns )
104104 }
105105
106106 stats .StartHierConfigReconcile ()
@@ -109,7 +109,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
109109 return ctrl.Result {}, r .reconcile (ctx , log , ns )
110110}
111111
112- func (r * Reconciler ) handleExcludedNamespace (ctx context.Context , log logr.Logger , nm string ) error {
112+ func (r * Reconciler ) handleUnmanaged (ctx context.Context , log logr.Logger , nm string ) error {
113113 // Get the namespace. Early exist if the namespace doesn't exist or is purged.
114114 // If so, there must be no namespace label or HC instance to delete.
115115 nsInst , err := r .getNamespace (ctx , nm )
@@ -127,13 +127,23 @@ func (r *Reconciler) handleExcludedNamespace(ctx context.Context, log logr.Logge
127127 return err
128128 }
129129
130- // Always delete hierarchyconfiguration (and any other HNC CRs) in the
131- // excluded namespaces. Note: since singletons in the excluded namespaces are
132- // never synced by HNC, there are no finalizers on the singletons that we can
133- // delete them without removing the finalizers first.
134- if err := r . deleteSingletonIfExists ( ctx , log , nm ); err != nil {
130+ // Don't delete the hierarchy config, since the admin might be enabling and disabling the regex and
131+ // we don't want this to be destructive. Instead, just remove the finalizers if there are any so that
132+ // users can delete it if they like.
133+ inst , _ , err := r . getSingleton ( ctx , nm )
134+ if err != nil {
135135 return err
136136 }
137+ if len (inst .ObjectMeta .Finalizers ) > 0 || len (inst .Status .Children ) > 0 {
138+ log .Info ("Removing finalizers and children on unmanaged singleton" )
139+ inst .ObjectMeta .Finalizers = nil
140+ inst .Status .Children = nil
141+ stats .WriteHierConfig ()
142+ if err := r .Update (ctx , inst ); err != nil {
143+ log .Error (err , "while removing finalizers on unmanaged namespace" )
144+ return err
145+ }
146+ }
137147
138148 return nil
139149}
@@ -428,9 +438,9 @@ func (r *Reconciler) syncParent(log logr.Logger, inst *api.HierarchyConfiguratio
428438
429439 // Sync this namespace with its current parent.
430440 curParent := r .Forest .Get (inst .Spec .Parent )
431- if ! config .IsNamespaceIncluded (inst .Spec .Parent ) {
432- log .Info ("Setting ConditionActivitiesHalted: excluded namespace set as parent" , "parent" , inst .Spec .Parent )
433- ns .SetCondition (api .ConditionActivitiesHalted , api .ReasonIllegalParent , fmt .Sprintf ("Parent %q is an excluded namespace" , inst .Spec .Parent ))
441+ if ! config .IsManagedNamespace (inst .Spec .Parent ) {
442+ log .Info ("Setting ConditionActivitiesHalted: unmanaged namespace set as parent" , "parent" , inst .Spec .Parent )
443+ ns .SetCondition (api .ConditionActivitiesHalted , api .ReasonIllegalParent , fmt .Sprintf ("Parent %q is an unmanaged namespace" , inst .Spec .Parent ))
434444 } else if curParent != nil && ! curParent .Exists () {
435445 log .Info ("Setting ConditionActivitiesHalted: parent doesn't exist (or hasn't been synced yet)" , "parent" , inst .Spec .Parent )
436446 ns .SetCondition (api .ConditionActivitiesHalted , api .ReasonParentMissing , fmt .Sprintf ("Parent %q does not exist" , inst .Spec .Parent ))
@@ -644,37 +654,6 @@ func (r *Reconciler) writeHierarchy(ctx context.Context, log logr.Logger, orig,
644654 return true , nil
645655}
646656
647- // deleteSingletonIfExists deletes the singleton in the namespace if it exists.
648- // Note: Make sure there's no finalizers on the singleton before calling this
649- // function.
650- func (r * Reconciler ) deleteSingletonIfExists (ctx context.Context , log logr.Logger , nm string ) error {
651- inst , deletingCRD , err := r .getSingleton (ctx , nm )
652- if err != nil {
653- return err
654- }
655-
656- // Early exit if the singleton doesn't exist.
657- if inst .CreationTimestamp .IsZero () {
658- return nil
659- }
660-
661- // If the CRD is being deleted, we don't need to delete it separately. It will
662- // be deleted with the CRD.
663- if deletingCRD {
664- log .Info ("HC in excluded namespace is already being deleted" )
665- return nil
666- }
667- log .Info ("Deleting illegal HC in excluded namespace" )
668-
669- stats .WriteHierConfig ()
670- if err := r .Delete (ctx , inst ); err != nil {
671- log .Error (err , "while deleting on apiserver" )
672- return err
673- }
674-
675- return nil
676- }
677-
678657func (r * Reconciler ) writeNamespace (ctx context.Context , log logr.Logger , orig , inst * corev1.Namespace ) (bool , error ) {
679658 if reflect .DeepEqual (orig , inst ) {
680659 return false , nil
0 commit comments