|
| 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 | + "fmt" |
| 9 | + "math" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 15 | + ctrl "sigs.k8s.io/controller-runtime" |
| 16 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 19 | + |
| 20 | + "github.com/ironcore-dev/network-operator/api/core/v1alpha1" |
| 21 | +) |
| 22 | + |
| 23 | +// log is for logging in this package. |
| 24 | +var bgplog = logf.Log.WithName("bgp-resource") |
| 25 | + |
| 26 | +// SetupBGPWebhookWithManager registers the webhook for BGP in the manager. |
| 27 | +func SetupBGPWebhookWithManager(mgr ctrl.Manager) error { |
| 28 | + return ctrl.NewWebhookManagedBy(mgr).For(&v1alpha1.BGP{}). |
| 29 | + WithValidator(&BGPCustomValidator{}). |
| 30 | + Complete() |
| 31 | +} |
| 32 | + |
| 33 | +// +kubebuilder:webhook:path=/validate-networking-metal-ironcore-dev-v1alpha1-bgp,mutating=false,failurePolicy=Fail,sideEffects=None,groups=networking.metal.ironcore.dev,resources=bgp,verbs=create;update,versions=v1alpha1,name=bgp-v1alpha1.kb.io,admissionReviewVersions=v1 |
| 34 | + |
| 35 | +// BGPCustomValidator struct is responsible for validating the BGP resource |
| 36 | +// when it is created, updated, or deleted. |
| 37 | +type BGPCustomValidator struct{} |
| 38 | + |
| 39 | +var _ webhook.CustomValidator = &BGPCustomValidator{} |
| 40 | + |
| 41 | +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type BGP. |
| 42 | +func (v *BGPCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 43 | + bgp, ok := obj.(*v1alpha1.BGP) |
| 44 | + if !ok { |
| 45 | + return nil, fmt.Errorf("expected a BGP object but got %T", obj) |
| 46 | + } |
| 47 | + |
| 48 | + bgplog.Info("Validation for BGP upon creation", "name", bgp.GetName()) |
| 49 | + |
| 50 | + return nil, validateASNumber(bgp.Spec.ASNumber) |
| 51 | +} |
| 52 | + |
| 53 | +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type BGP. |
| 54 | +func (v *BGPCustomValidator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 55 | + bgp, ok := newObj.(*v1alpha1.BGP) |
| 56 | + if !ok { |
| 57 | + return nil, fmt.Errorf("expected a BGP object for the newObj but got %T", newObj) |
| 58 | + } |
| 59 | + |
| 60 | + bgplog.Info("Validation for BGP upon update", "name", bgp.GetName()) |
| 61 | + |
| 62 | + return nil, validateASNumber(bgp.Spec.ASNumber) |
| 63 | +} |
| 64 | + |
| 65 | +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type BGP. |
| 66 | +func (v *BGPCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 67 | + return nil, nil |
| 68 | +} |
| 69 | + |
| 70 | +// validateASNumber performs validation on the autonomous system number (ASN). |
| 71 | +// It ensures the ASN is within valid ranges for both plain and dotted notation as per [RFC 5396]. |
| 72 | +// [RFC 5396](https://datatracker.ietf.org/doc/html/rfc5396) |
| 73 | +func validateASNumber(asn intstr.IntOrString) error { |
| 74 | + // If the value is an integer, validate plain format only |
| 75 | + if asn.Type == intstr.Int { |
| 76 | + asnValue := asn.IntVal |
| 77 | + if asnValue < 1 { |
| 78 | + return fmt.Errorf("AS number %d must be >=1 (for larger values use string format to support full range 1-4294967295)", asnValue) |
| 79 | + } |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + // If the value is a string, it can be either plain or dotted notation |
| 84 | + asnStr := asn.StrVal |
| 85 | + |
| 86 | + // Try to parse as plain format first |
| 87 | + if !strings.Contains(asnStr, ".") { |
| 88 | + asnValue, err := strconv.ParseInt(asnStr, 10, 64) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("invalid AS number format %q: %w", asnStr, err) |
| 91 | + } |
| 92 | + if asnValue < 1 || asnValue > math.MaxUint32 { |
| 93 | + return fmt.Errorf("AS number %d is out of valid range (1-4294967295)", asnValue) |
| 94 | + } |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + // Parse as dotted notation (high.low) |
| 99 | + parts := strings.Split(asnStr, ".") |
| 100 | + if len(parts) != 2 { |
| 101 | + return fmt.Errorf("invalid AS number dotted notation %q: must be in format high.low", asnStr) |
| 102 | + } |
| 103 | + |
| 104 | + high, err := strconv.ParseInt(parts[0], 10, 64) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("invalid AS number dotted notation %q: high part is not a valid number: %w", asnStr, err) |
| 107 | + } |
| 108 | + if high < 1 || high > math.MaxUint16 { |
| 109 | + return fmt.Errorf("invalid AS number dotted notation %q: high part %d is out of valid range (1-65535)", asnStr, high) |
| 110 | + } |
| 111 | + |
| 112 | + low, err := strconv.ParseInt(parts[1], 10, 64) |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("invalid AS number dotted notation %q: low part is not a valid number: %w", asnStr, err) |
| 115 | + } |
| 116 | + if low < 0 || low > math.MaxUint16 { |
| 117 | + return fmt.Errorf("invalid AS number dotted notation %q: low part %d is out of valid range (0-65535)", asnStr, low) |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
0 commit comments