|
| 1 | +// SPDX-FileCopyrightText: 2026 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 | + |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + |
| 11 | + ctrl "sigs.k8s.io/controller-runtime" |
| 12 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 14 | + |
| 15 | + metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1" |
| 16 | +) |
| 17 | + |
| 18 | +// log is for logging in this package. |
| 19 | +var bmclog = logf.Log.WithName("bmc-resource") |
| 20 | + |
| 21 | +// SetupBMCWebhookWithManager registers the webhook for BMC in the manager. |
| 22 | +func SetupBMCWebhookWithManager(mgr ctrl.Manager) error { |
| 23 | + return ctrl.NewWebhookManagedBy(mgr, &metalv1alpha1.BMC{}). |
| 24 | + WithValidator(&BMCCustomValidator{Client: mgr.GetClient()}). |
| 25 | + Complete() |
| 26 | +} |
| 27 | + |
| 28 | +// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here. |
| 29 | +// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook. |
| 30 | +// +kubebuilder:webhook:path=/validate-metal-ironcore-dev-v1alpha1-bmc,mutating=false,failurePolicy=fail,sideEffects=None,groups=metal.ironcore.dev,resources=bmcs,verbs=create;update,versions=v1alpha1,name=vbmc-v1alpha1.kb.io,admissionReviewVersions=v1 |
| 31 | + |
| 32 | +// BMCCustomValidator struct is responsible for validating the BMC resource |
| 33 | +// when it is created, updated, or deleted. |
| 34 | +// |
| 35 | +// NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods, |
| 36 | +// as this struct is used only for temporary operations and does not need to be deeply copied. |
| 37 | +// +kubebuilder:object:generate=false |
| 38 | +type BMCCustomValidator struct { |
| 39 | + Client client.Client |
| 40 | +} |
| 41 | + |
| 42 | +// bmcEndpointRefDeprecationWarning is the admission warning surfaced on BMC create/update |
| 43 | +// when spec.endpointRef is set, to direct users toward the inlined BMC.spec.access path. |
| 44 | +const bmcEndpointRefDeprecationWarning = "spec.endpointRef is deprecated; specify the network access inline via spec.access (an InlineEndpoint with macAddress and ip) instead" |
| 45 | + |
| 46 | +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type BMC. |
| 47 | +func (v *BMCCustomValidator) ValidateCreate(ctx context.Context, obj *metalv1alpha1.BMC) (admission.Warnings, error) { |
| 48 | + bmclog.Info("Validating BMC upon creation", "name", obj.GetName()) |
| 49 | + |
| 50 | + return bmcEndpointRefWarnings(obj), nil |
| 51 | +} |
| 52 | + |
| 53 | +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type BMC. |
| 54 | +func (v *BMCCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj *metalv1alpha1.BMC) (admission.Warnings, error) { |
| 55 | + bmclog.Info("Validating BMC upon update", "name", newObj.GetName()) |
| 56 | + |
| 57 | + return bmcEndpointRefWarnings(newObj), nil |
| 58 | +} |
| 59 | + |
| 60 | +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type BMC. |
| 61 | +func (v *BMCCustomValidator) ValidateDelete(ctx context.Context, obj *metalv1alpha1.BMC) (admission.Warnings, error) { |
| 62 | + bmclog.Info("Validating BMC upon deletion", "name", obj.GetName()) |
| 63 | + |
| 64 | + return nil, nil |
| 65 | +} |
| 66 | + |
| 67 | +// bmcEndpointRefWarnings returns the deprecation warning when the BMC references an Endpoint |
| 68 | +// via spec.endpointRef. It never rejects the object; the access/endpointRef exactly-one-of and |
| 69 | +// immutability rules are enforced by CEL XValidation markers on the BMCSpec type. |
| 70 | +func bmcEndpointRefWarnings(bmc *metalv1alpha1.BMC) admission.Warnings { |
| 71 | + if bmc.Spec.EndpointRef != nil { |
| 72 | + return admission.Warnings{bmcEndpointRefDeprecationWarning} |
| 73 | + } |
| 74 | + return nil |
| 75 | +} |
0 commit comments