From ea3de5092a1f91725240778b4ae254a53534136b Mon Sep 17 00:00:00 2001 From: ubombar Date: Fri, 22 Mar 2024 11:19:28 +0100 Subject: [PATCH] Remove owner reference in subnamespace creation --- internal/multitenancy/v1/multitenancy.go | 8 +++----- internal/utils/utils.go | 8 ++++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/internal/multitenancy/v1/multitenancy.go b/internal/multitenancy/v1/multitenancy.go index 8f31bab..78c4e82 100644 --- a/internal/multitenancy/v1/multitenancy.go +++ b/internal/multitenancy/v1/multitenancy.go @@ -389,7 +389,7 @@ func (m *multiTenancyManager) SubNamespaceCleanup(ctx context.Context, s *multit }, } - // Try to delete the namespace, ignore if not found + // Then finally try to delete the namespace if err := m.client.Delete(ctx, ns); err != nil && !errors.IsNotFound(err) { return err } @@ -410,10 +410,8 @@ func (m *multiTenancyManager) SetupSubNamespace(ctx context.Context, s *multiten "edge-net.io/kind": "sub", "edge-net.io/parent": s.GetNamespace(), }, - // The owner reference is required to ensure the namespace cannot be deleted before the subnamespace object. - OwnerReferences: []metav1.OwnerReference{ - *metav1.NewControllerRef(s, s.GroupVersionKind()), - }, + // We will use admission controller to prevent namespaces that are managed by the subnamespace controller from deleting. + // So we will not have finalizers and owners in the newly created object. }, } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 686551a..2bf7587 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -70,7 +70,7 @@ func ResolveSubNamespaceName(s *multitenancyv1.SubNamespace) string { } // Check a string exists in a list of strings -func containsFinalizer(finalizers []string, finalizer string) bool { +func ContainsFinalizer(finalizers []string, finalizer string) bool { for _, item := range finalizers { if item == finalizer { return true @@ -80,7 +80,7 @@ func containsFinalizer(finalizers []string, finalizer string) bool { } // Remove a string from a list of strings -func removeFinalizer(finalizers []string, finalizer string) []string { +func RemoveFinalizer(finalizers []string, finalizer string) []string { for i, item := range finalizers { if item == finalizer { // Remove the item at index i from slice. @@ -116,7 +116,7 @@ func GetResourceWithFinalizer(ctx context.Context, c client.Client, obj client.O } // If the object is not marked for deletion and doesn't contain the finalizer - if obj.GetDeletionTimestamp().IsZero() && !containsFinalizer(obj.GetFinalizers(), "edge-net.io/controller") { + if obj.GetDeletionTimestamp().IsZero() && !ContainsFinalizer(obj.GetFinalizers(), "edge-net.io/controller") { obj.SetFinalizers(append(obj.GetFinalizers(), "edge-net.io/controller")) if err := c.Update(ctx, obj); err != nil { @@ -145,7 +145,7 @@ func GetResource(ctx context.Context, c client.Client, obj client.Object, namesp // AllowObjectDeletion method removes the edge-net.io/controller finalizer. By this way the object can be completely removed // from the cluster. func AllowObjectDeletion(ctx context.Context, c client.Client, obj client.Object) (reconcile.Result, error) { - obj.SetFinalizers(removeFinalizer(obj.GetFinalizers(), "edge-net.io/controller")) + obj.SetFinalizers(RemoveFinalizer(obj.GetFinalizers(), "edge-net.io/controller")) if err := c.Update(ctx, obj); err != nil { return reconcile.Result{}, err