|
| 1 | +package webhook |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/go-logr/logr" |
| 7 | + kmmv1beta2 "github.com/kubernetes-sigs/kernel-module-management/api/v1beta2" |
| 8 | + "github.com/kubernetes-sigs/kernel-module-management/internal/utils" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + ctrl "sigs.k8s.io/controller-runtime" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 12 | +) |
| 13 | + |
| 14 | +// PreflightValidationValidator validates PreflightValidation resources. |
| 15 | +type PreflightValidationValidator struct { |
| 16 | + logger logr.Logger |
| 17 | +} |
| 18 | + |
| 19 | +func NewPreflightValidationValidator(logger logr.Logger) *PreflightValidationValidator { |
| 20 | + return &PreflightValidationValidator{logger: logger} |
| 21 | +} |
| 22 | + |
| 23 | +func (v *PreflightValidationValidator) SetupWebhookWithManager(mgr ctrl.Manager, pf *kmmv1beta2.PreflightValidation) error { |
| 24 | + return ctrl.NewWebhookManagedBy(mgr). |
| 25 | + For(pf). |
| 26 | + WithValidator(v). |
| 27 | + Complete() |
| 28 | +} |
| 29 | + |
| 30 | +//+kubebuilder:webhook:path=/validate-kmm-sigs-x-k8s-io-v1beta2-preflightvalidation,mutating=false,failurePolicy=fail,sideEffects=None,groups=kmm.sigs.x-k8s.io,resources=preflightvalidations,verbs=create;update,versions=v1beta2,name=vpreflightvalidation.kb.io,admissionReviewVersions=v1 |
| 31 | + |
| 32 | +func (v *PreflightValidationValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 33 | + pv, ok := obj.(*kmmv1beta2.PreflightValidation) |
| 34 | + if !ok { |
| 35 | + return nil, fmt.Errorf("bad type for the object; expected %v, got %v", pv, obj) |
| 36 | + } |
| 37 | + |
| 38 | + v.logger.Info("Validating PreflightValidation creation", "name", pv.Name) |
| 39 | + return validatePreflight(pv) |
| 40 | +} |
| 41 | + |
| 42 | +func (v *PreflightValidationValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { |
| 43 | + oldPV, ok := oldObj.(*kmmv1beta2.PreflightValidation) |
| 44 | + if !ok { |
| 45 | + return nil, fmt.Errorf("bad type for the old object; expected %v, got %v", oldPV, oldObj) |
| 46 | + } |
| 47 | + |
| 48 | + newPV, ok := newObj.(*kmmv1beta2.PreflightValidation) |
| 49 | + if !ok { |
| 50 | + return nil, fmt.Errorf("bad type for the new object; expected %v, got %v", newPV, newObj) |
| 51 | + } |
| 52 | + |
| 53 | + v.logger.Info("Validating PreflightValidation update", "name", oldPV.Name) |
| 54 | + return validatePreflight(newPV) |
| 55 | +} |
| 56 | + |
| 57 | +func (v *PreflightValidationValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 58 | + return nil, NotImplemented |
| 59 | +} |
| 60 | + |
| 61 | +func validatePreflight(pv *kmmv1beta2.PreflightValidation) (admission.Warnings, error) { |
| 62 | + if pv.Spec.KernelVersion == "" { |
| 63 | + return nil, fmt.Errorf("kernelVersion cannot be empty") |
| 64 | + } |
| 65 | + |
| 66 | + fields := utils.KernelRegexp.Split(pv.Spec.KernelVersion, -1) |
| 67 | + if len(fields) < 3 { |
| 68 | + return nil, fmt.Errorf("invalid kernelVersion %s", pv.Spec.KernelVersion) |
| 69 | + } |
| 70 | + |
| 71 | + return nil, nil |
| 72 | +} |
0 commit comments