Files

77 lines
2.7 KiB
Go
Raw Permalink Normal View History

2026-05-05 12:59:36 +02:00
// SPDX-License-Identifier: BSD-3-Clause
2025-03-06 10:57:45 +02:00
package v1
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2026-04-30 13:39:11 +02:00
nbv1 "github.com/netbirdio/kubernetes-operator/api/v1"
2025-03-06 10:57:45 +02:00
)
// nolint:unused
// log is for logging in this package.
var nbgrouplog = logf.Log.WithName("nbgroup-resource")
// SetupNBGroupWebhookWithManager registers the webhook for NBGroup in the manager.
func SetupNBGroupWebhookWithManager(mgr ctrl.Manager) error {
2026-04-30 13:39:11 +02:00
return ctrl.NewWebhookManagedBy(mgr, &nbv1.NBGroup{}).
WithValidator(&NBGroupCustomValidator{client: mgr.GetClient()}).
2025-03-06 10:57:45 +02:00
Complete()
}
// NBGroupCustomValidator struct is responsible for validating the NBGroup resource
// when it is created, updated, or deleted.
type NBGroupCustomValidator struct {
client client.Client
2025-03-06 10:57:45 +02:00
}
2026-04-30 13:39:11 +02:00
var _ admission.Validator[*nbv1.NBGroup] = &NBGroupCustomValidator{}
2025-03-06 10:57:45 +02:00
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type NBGroup.
2026-04-30 13:39:11 +02:00
func (v *NBGroupCustomValidator) ValidateCreate(ctx context.Context, group *nbv1.NBGroup) (admission.Warnings, error) {
2025-03-06 10:57:45 +02:00
return nil, nil
}
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type NBGroup.
2026-04-30 13:39:11 +02:00
func (v *NBGroupCustomValidator) ValidateUpdate(ctx context.Context, old, new *nbv1.NBGroup) (admission.Warnings, error) {
2025-03-06 10:57:45 +02:00
return nil, nil
}
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type NBGroup.
2026-04-30 13:39:11 +02:00
func (v *NBGroupCustomValidator) ValidateDelete(ctx context.Context, nbgroup *nbv1.NBGroup) (admission.Warnings, error) {
2025-03-06 10:57:45 +02:00
nbgrouplog.Info("Validation for NBGroup upon deletion", "name", nbgroup.GetName())
for _, o := range nbgroup.OwnerReferences {
2026-04-30 13:39:11 +02:00
if o.Kind == (&nbv1.NBResource{}).Kind {
var nbResource nbv1.NBResource
2025-03-06 10:57:45 +02:00
err := v.client.Get(ctx, types.NamespacedName{Namespace: nbgroup.Namespace, Name: o.Name}, &nbResource)
if err != nil && !errors.IsNotFound(err) {
return nil, err
}
if err == nil && nbResource.DeletionTimestamp == nil {
return nil, fmt.Errorf("group attached to NBResource %s/%s", nbgroup.Namespace, o.Name)
}
}
2026-04-30 13:39:11 +02:00
if o.Kind == (&nbv1.NBRoutingPeer{}).Kind {
var nbResource nbv1.NBRoutingPeer
2025-03-06 10:57:45 +02:00
err := v.client.Get(ctx, types.NamespacedName{Namespace: nbgroup.Namespace, Name: o.Name}, &nbResource)
if err != nil && !errors.IsNotFound(err) {
return nil, err
}
if err == nil && nbResource.DeletionTimestamp == nil {
return nil, fmt.Errorf("group attached to NBRoutingPeer %s/%s", nbgroup.Namespace, o.Name)
}
}
}
return nil, nil
}