-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8b84c8
commit 23564f8
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//go:build e2e | ||
|
||
// Copyright 2020-2021 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package e2e | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
capsulev1beta1 "github.com/clastix/capsule/api/v1beta1" | ||
) | ||
|
||
var _ = Describe("Deleting a tenant with protected annotation", func() { | ||
tnt := &capsulev1beta1.Tenant{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "protected_tenant", | ||
Annotations: map[string]string{ | ||
capsulev1beta1.ProtectedTenantAnnotation: "", | ||
}, | ||
}, | ||
Spec: capsulev1beta1.TenantSpec{ | ||
Owners: capsulev1beta1.OwnerListSpec{ | ||
{ | ||
Name: "john", | ||
Kind: "User", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
It("should fail", func() { | ||
Expect(k8sClient.Create(context.TODO(), tnt)).Should(Succeed()) | ||
Expect(k8sClient.Delete(context.TODO(), tnt)).ShouldNot(Succeed()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2020-2022 Clastix Labs | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tenant | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
capsulev1beta1 "github.com/clastix/capsule/api/v1beta1" | ||
capsulewebhook "github.com/clastix/capsule/pkg/webhook" | ||
"github.com/clastix/capsule/pkg/webhook/utils" | ||
) | ||
|
||
type protectedHandler struct{} | ||
|
||
func ProtectedHandler() capsulewebhook.Handler { | ||
return &protectedHandler{} | ||
} | ||
|
||
func (h *protectedHandler) OnCreate(client.Client, *admission.Decoder, record.EventRecorder) capsulewebhook.Func { | ||
return func(context.Context, admission.Request) *admission.Response { | ||
return nil | ||
} | ||
} | ||
|
||
func (h *protectedHandler) OnDelete(_ client.Client, decoder *admission.Decoder, _ record.EventRecorder) capsulewebhook.Func { | ||
return func(ctx context.Context, req admission.Request) *admission.Response { | ||
tenant := &capsulev1beta1.Tenant{} | ||
if err := decoder.Decode(req, tenant); err != nil { | ||
return utils.ErroredResponse(err) | ||
} | ||
|
||
if _, protected := tenant.Annotations[capsulev1beta1.ProtectedTenantAnnotation]; protected { | ||
response := admission.Denied(fmt.Sprintf("tenant is protected and cannot be deleted, remove %s annotation before proceeding", capsulev1beta1.ProtectedTenantAnnotation)) | ||
|
||
return &response | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func (h *protectedHandler) OnUpdate(client.Client, *admission.Decoder, record.EventRecorder) capsulewebhook.Func { | ||
return func(context.Context, admission.Request) *admission.Response { | ||
return nil | ||
} | ||
} |