Skip to content

Commit

Permalink
Remove owner reference in subnamespace creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ubombar committed Mar 22, 2024
1 parent 870f6d2 commit ea3de50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
8 changes: 3 additions & 5 deletions internal/multitenancy/v1/multitenancy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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.
},
}

Expand Down
8 changes: 4 additions & 4 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ea3de50

Please sign in to comment.