mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-04-10 12:48:02 +00:00
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
// Copyright 2020-2026 Project Capsule Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//nolint:dupl
|
|
package validation
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
|
|
"k8s.io/client-go/tools/events"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
|
|
|
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2"
|
|
"github.com/projectcapsule/capsule/pkg/runtime/handlers"
|
|
)
|
|
|
|
type containerRegistryRegexHandler struct{}
|
|
|
|
func ContainerRegistryRegexHandler() handlers.TypedHandler[*capsulev1beta2.Tenant] {
|
|
return &containerRegistryRegexHandler{}
|
|
}
|
|
|
|
func (h *containerRegistryRegexHandler) OnCreate(
|
|
_ client.Client,
|
|
tnt *capsulev1beta2.Tenant,
|
|
decoder admission.Decoder,
|
|
_ events.EventRecorder,
|
|
) handlers.Func {
|
|
return func(_ context.Context, req admission.Request) *admission.Response {
|
|
if err := h.validate(tnt, req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (h *containerRegistryRegexHandler) OnDelete(
|
|
client.Client,
|
|
*capsulev1beta2.Tenant,
|
|
admission.Decoder,
|
|
events.EventRecorder,
|
|
) handlers.Func {
|
|
return func(context.Context, admission.Request) *admission.Response {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (h *containerRegistryRegexHandler) OnUpdate(
|
|
_ client.Client,
|
|
tnt *capsulev1beta2.Tenant,
|
|
old *capsulev1beta2.Tenant,
|
|
decoder admission.Decoder,
|
|
_ events.EventRecorder,
|
|
) handlers.Func {
|
|
return func(_ context.Context, req admission.Request) *admission.Response {
|
|
if response := h.validate(tnt, req); response != nil {
|
|
return response
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
//nolint:staticcheck
|
|
func (h *containerRegistryRegexHandler) validate(
|
|
tnt *capsulev1beta2.Tenant,
|
|
req admission.Request,
|
|
) *admission.Response {
|
|
if tnt.Spec.ContainerRegistries != nil && len(tnt.Spec.ContainerRegistries.Regex) > 0 {
|
|
if _, err := regexp.Compile(tnt.Spec.ContainerRegistries.Regex); err != nil {
|
|
response := admission.Denied("unable to compile containerRegistries allowedRegex")
|
|
|
|
return &response
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|