Skip to content

Commit

Permalink
fix(controller): ensure iteration on capsule ownerreferences (#1059)
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Bähler <[email protected]>
  • Loading branch information
oliverbaehler authored May 2, 2024
1 parent c2f3694 commit e418f74
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions e2e/namespace_additional_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ var _ = Describe("creating a Namespace for a Tenant with additional metadata", f
tnt := &capsulev1beta2.Tenant{
ObjectMeta: metav1.ObjectMeta{
Name: "tenant-metadata",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "cap",
Kind: "dummy",
Name: "tenant-metadata",
UID: "tenant-metadata",
},
},
},
Spec: capsulev1beta2.TenantSpec{
Owners: capsulev1beta2.OwnerListSpec{
Expand Down
4 changes: 4 additions & 0 deletions pkg/webhook/namespace/freezed.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (r *freezedHandler) OnCreate(client client.Client, decoder *admission.Decod
}

for _, objectRef := range ns.ObjectMeta.OwnerReferences {
if !isTenantOwnerReference(objectRef) {
continue
}

// retrieving the selected Tenant
tnt := &capsulev1beta2.Tenant{}
if err := client.Get(ctx, types.NamespacedName{Name: objectRef.Name}, tnt); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/webhook/namespace/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (r *prefixHandler) OnCreate(clt client.Client, decoder *admission.Decoder,
tnt := &capsulev1beta2.Tenant{}

for _, or := range ns.ObjectMeta.OwnerReferences {
if !isTenantOwnerReference(or) {
continue
}

// retrieving the selected Tenant
if err := clt.Get(ctx, types.NamespacedName{Name: or.Name}, tnt); err != nil {
return utils.ErroredResponse(err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/webhook/namespace/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (r *quotaHandler) OnCreate(client client.Client, decoder *admission.Decoder
}

for _, objectRef := range ns.ObjectMeta.OwnerReferences {
if !isTenantOwnerReference(objectRef) {
continue
}

// retrieving the selected Tenant
tnt := &capsulev1beta2.Tenant{}
if err := client.Get(ctx, types.NamespacedName{Name: objectRef.Name}, tnt); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/webhook/namespace/user_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func (r *userMetadataHandler) OnCreate(client client.Client, decoder *admission.
}

tnt := &capsulev1beta2.Tenant{}

for _, objectRef := range ns.ObjectMeta.OwnerReferences {
if !isTenantOwnerReference(objectRef) {
continue
}

// retrieving the selected Tenant
if err := client.Get(ctx, types.NamespacedName{Name: objectRef.Name}, tnt); err != nil {
return utils.ErroredResponse(err)
Expand Down Expand Up @@ -83,7 +88,12 @@ func (r *userMetadataHandler) OnUpdate(client client.Client, decoder *admission.
}

tnt := &capsulev1beta2.Tenant{}

for _, objectRef := range newNs.ObjectMeta.OwnerReferences {
if !isTenantOwnerReference(objectRef) {
continue
}

// retrieving the selected Tenant
if err := client.Get(ctx, types.NamespacedName{Name: objectRef.Name}, tnt); err != nil {
return utils.ErroredResponse(err)
Expand Down
27 changes: 27 additions & 0 deletions pkg/webhook/namespace/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020-2023 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0

package namespace

import (
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
)

const (
ObjectReferenceTenantKind = "Tenant"
)

func isTenantOwnerReference(or metav1.OwnerReference) bool {
parts := strings.Split(or.APIVersion, "/")
if len(parts) != 2 {
return false
}

group := parts[0]

return group == capsulev1beta2.GroupVersion.Group && or.Kind == ObjectReferenceTenantKind
}

0 comments on commit e418f74

Please sign in to comment.