mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-05-06 09:26:42 +00:00
* chore(deps): update dependency golangci/golangci-lint to v2.8.0 * chore(deps): update dependency golangci/golangci-lint to v2.8.0 Signed-off-by: Hristo Hristov <me@hhristov.info> * chore(deps): update dependency golangci/golangci-lint to v2.8.0 Signed-off-by: Hristo Hristov <me@hhristov.info> * chore(deps): update dependency golangci/golangci-lint to v2.8.0 Signed-off-by: Hristo Hristov <me@hhristov.info> --------- Signed-off-by: Hristo Hristov <me@hhristov.info> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Hristo Hristov <me@hhristov.info>
74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
// Copyright 2020-2026 Project Capsule Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package validation
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
"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/internal/webhook"
|
|
"github.com/projectcapsule/capsule/pkg/configuration"
|
|
"github.com/projectcapsule/capsule/pkg/utils/users"
|
|
)
|
|
|
|
type patchHandler struct {
|
|
cfg configuration.Configuration
|
|
}
|
|
|
|
func PatchHandler(configuration configuration.Configuration) capsulewebhook.TypedHandlerWithTenant[*corev1.Namespace] {
|
|
return &patchHandler{cfg: configuration}
|
|
}
|
|
|
|
func (h *patchHandler) OnCreate(
|
|
client.Client,
|
|
*corev1.Namespace,
|
|
admission.Decoder,
|
|
record.EventRecorder,
|
|
*capsulev1beta2.Tenant,
|
|
) capsulewebhook.Func {
|
|
return func(context.Context, admission.Request) *admission.Response {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (h *patchHandler) OnDelete(
|
|
client.Client,
|
|
*corev1.Namespace,
|
|
admission.Decoder,
|
|
record.EventRecorder,
|
|
*capsulev1beta2.Tenant,
|
|
) capsulewebhook.Func {
|
|
return func(context.Context, admission.Request) *admission.Response {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (h *patchHandler) OnUpdate(
|
|
c client.Client,
|
|
ns *corev1.Namespace,
|
|
old *corev1.Namespace,
|
|
decoder admission.Decoder,
|
|
recorder record.EventRecorder,
|
|
tnt *capsulev1beta2.Tenant,
|
|
) capsulewebhook.Func {
|
|
return func(ctx context.Context, req admission.Request) *admission.Response {
|
|
e := fmt.Sprintf("namespace/%s can not be patched", ns.Name)
|
|
|
|
if ok := users.IsTenantOwnerByStatus(ctx, c, h.cfg, tnt, req.UserInfo); ok {
|
|
return nil
|
|
}
|
|
|
|
recorder.Eventf(ns, corev1.EventTypeWarning, "NamespacePatch", e)
|
|
response := admission.Denied(e)
|
|
|
|
return &response
|
|
}
|
|
}
|