|
| 1 | +// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package v1alpha1 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + ctrl "sigs.k8s.io/controller-runtime" |
| 13 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 16 | + |
| 17 | + "github.com/ironcore-dev/network-operator/api/core/v1alpha1" |
| 18 | +) |
| 19 | + |
| 20 | +// log is for logging in this package. |
| 21 | +var prefixsetlog = logf.Log.WithName("prefixset-resource") |
| 22 | + |
| 23 | +// SetupPrefixSetWebhookWithManager registers the webhook for PrefixSets in the manager. |
| 24 | +func SetupPrefixSetWebhookWithManager(mgr ctrl.Manager) error { |
| 25 | + return ctrl.NewWebhookManagedBy(mgr). |
| 26 | + For(&v1alpha1.PrefixSet{}). |
| 27 | + WithValidator(&PrefixSetCustomValidator{}). |
| 28 | + Complete() |
| 29 | +} |
| 30 | + |
| 31 | +// +kubebuilder:webhook:path=/validate-networking-metal-ironcore-dev-v1alpha1-prefixset,mutating=false,failurePolicy=Fail,sideEffects=None,groups=networking.metal.ironcore.dev,resources=prefixsets,verbs=create;update,versions=v1alpha1,name=prefixset-v1alpha1.kb.io,admissionReviewVersions=v1 |
| 32 | + |
| 33 | +// PrefixSetCustomValidator struct is responsible for validating the PrefixSet resource |
| 34 | +// when it is created, updated, or deleted. |
| 35 | +type PrefixSetCustomValidator struct{} |
| 36 | + |
| 37 | +var _ webhook.CustomValidator = &PrefixSetCustomValidator{} |
| 38 | + |
| 39 | +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type PrefixSet. |
| 40 | +func (v *PrefixSetCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 41 | + ps, ok := obj.(*v1alpha1.PrefixSet) |
| 42 | + if !ok { |
| 43 | + return nil, fmt.Errorf("expected a PrefixSets object but got %T", obj) |
| 44 | + } |
| 45 | + |
| 46 | + prefixsetlog.Info("Validation for PrefixSets upon creation", "name", ps.GetName()) |
| 47 | + |
| 48 | + return nil, validatePrefixSetSpec(ps) |
| 49 | +} |
| 50 | + |
| 51 | +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type PrefixSet. |
| 52 | +func (v *PrefixSetCustomValidator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 53 | + prev, ok := oldObj.(*v1alpha1.PrefixSet) |
| 54 | + if !ok { |
| 55 | + return nil, fmt.Errorf("expected a PrefixSets object for the oldObj but got %T", oldObj) |
| 56 | + } |
| 57 | + |
| 58 | + curr, ok := newObj.(*v1alpha1.PrefixSet) |
| 59 | + if !ok { |
| 60 | + return nil, fmt.Errorf("expected a PrefixSets object for the newObj but got %T", newObj) |
| 61 | + } |
| 62 | + |
| 63 | + prefixsetlog.Info("Validation for PrefixSets upon update", "name", curr.GetName()) |
| 64 | + |
| 65 | + if err := validatePrefixSetSpec(curr); err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + if len(prev.Spec.Entries) > 0 && prev.Is6() != curr.Is6() { |
| 70 | + return nil, errors.New("cannot change IP family of a PrefixSet once created") |
| 71 | + } |
| 72 | + |
| 73 | + return nil, nil |
| 74 | +} |
| 75 | + |
| 76 | +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type PrefixSet. |
| 77 | +func (v *PrefixSetCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 78 | + return nil, nil |
| 79 | +} |
| 80 | + |
| 81 | +// validatePrefixSetSpec performs validation on the PrefixSet spec. |
| 82 | +func validatePrefixSetSpec(ps *v1alpha1.PrefixSet) error { |
| 83 | + var is6 bool |
| 84 | + var errAgg []error |
| 85 | + for i, ent := range ps.Spec.Entries { |
| 86 | + if i > 0 && ent.Prefix.Addr().Is6() != is6 { |
| 87 | + errAgg = append(errAgg, errors.New("all prefixes in a PrefixSet must be of the same IP family")) |
| 88 | + } |
| 89 | + is6 = ent.Prefix.Addr().Is6() |
| 90 | + if ent.MaskLengthRange != nil { |
| 91 | + bits := ent.Prefix.Bits() |
| 92 | + rmin := int(ent.MaskLengthRange.Min) |
| 93 | + rmax := int(ent.MaskLengthRange.Max) |
| 94 | + |
| 95 | + if rmin < bits { |
| 96 | + errAgg = append(errAgg, fmt.Errorf("entry %d: mask length range min %d is invalid for prefix %s", i, rmin, ent.Prefix.String())) |
| 97 | + } |
| 98 | + if rmax < bits { |
| 99 | + errAgg = append(errAgg, fmt.Errorf("entry %d: mask length range max %d is invalid for prefix %s", i, rmax, ent.Prefix.String())) |
| 100 | + } |
| 101 | + if rmin > rmax { |
| 102 | + errAgg = append(errAgg, fmt.Errorf("entry %d: mask length range min %d cannot be greater than max %d", i, rmin, rmax)) |
| 103 | + } |
| 104 | + const maxBits = 32 |
| 105 | + if !is6 && rmin > maxBits { |
| 106 | + errAgg = append(errAgg, fmt.Errorf("entry %d: mask length range min %d exceeds maximum %d bits for IPv4", i, rmin, maxBits)) |
| 107 | + } |
| 108 | + if !is6 && rmax > maxBits { |
| 109 | + errAgg = append(errAgg, fmt.Errorf("entry %d: mask length range max %d exceeds maximum %d bits for IPv4", i, rmax, maxBits)) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return errors.Join(errAgg...) |
| 114 | +} |
0 commit comments