Files
capsule/pkg/webhook/tenant/protected.go
Oliver Bähler 3682283352 chore: add license headers (#1504)
* chore: add nwa

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>

* chore: update helm-schema version

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>

* chore: update helm-schema version

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>

---------

Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>
2025-06-13 07:31:04 +02:00

54 lines
1.5 KiB
Go

// Copyright 2020-2025 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0
package tenant
import (
"context"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
capsulewebhook "github.com/projectcapsule/capsule/pkg/webhook"
"github.com/projectcapsule/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(clt client.Client, _ admission.Decoder, _ record.EventRecorder) capsulewebhook.Func {
return func(ctx context.Context, req admission.Request) *admission.Response {
tenant := &capsulev1beta2.Tenant{}
if err := clt.Get(ctx, types.NamespacedName{Name: req.Name}, tenant); err != nil {
return utils.ErroredResponse(err)
}
if tenant.Spec.PreventDeletion {
response := admission.Denied("tenant is protected and cannot be deleted")
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
}
}