diff --git a/bootstrap/api/v1alpha1/rke2config_webhook.go b/bootstrap/api/v1alpha1/rke2config_webhook.go index 4dec2a40..a4f324ea 100644 --- a/bootstrap/api/v1alpha1/rke2config_webhook.go +++ b/bootstrap/api/v1alpha1/rke2config_webhook.go @@ -42,10 +42,11 @@ var _ webhook.Defaulter = &RKE2Config{} // Default implements webhook.Defaulter so a webhook will be registered for the type. func (r *RKE2Config) Default() { - defaultRKE2ConfigSpec(&r.Spec) + DefaultRKE2ConfigSpec(&r.Spec) } -func defaultRKE2ConfigSpec(spec *RKE2ConfigSpec) { +// DefaultRKE2ConfigSpec defaults the RKE2ConfigSpec. +func DefaultRKE2ConfigSpec(spec *RKE2ConfigSpec) { if spec.AgentConfig.Format == "" { spec.AgentConfig.Format = CloudConfig } @@ -57,12 +58,12 @@ var _ webhook.Validator = &RKE2Config{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. func (r *RKE2Config) ValidateCreate() error { - return validateRKE2ConfigSpec(r.Name, &r.Spec) + return ValidateRKE2ConfigSpec(r.Name, &r.Spec) } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. func (r *RKE2Config) ValidateUpdate(_ runtime.Object) error { - return validateRKE2ConfigSpec(r.Name, &r.Spec) + return ValidateRKE2ConfigSpec(r.Name, &r.Spec) } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. @@ -70,7 +71,8 @@ func (r *RKE2Config) ValidateDelete() error { return nil } -func validateRKE2ConfigSpec(name string, spec *RKE2ConfigSpec) error { +// ValidateRKE2ConfigSpec validates the RKE2ConfigSpec. +func ValidateRKE2ConfigSpec(name string, spec *RKE2ConfigSpec) error { allErrs := spec.validate(field.NewPath("spec")) if len(allErrs) == 0 { diff --git a/controlplane/api/v1alpha1/rke2controlplane_webhook.go b/controlplane/api/v1alpha1/rke2controlplane_webhook.go index 42af13f1..db53b814 100644 --- a/controlplane/api/v1alpha1/rke2controlplane_webhook.go +++ b/controlplane/api/v1alpha1/rke2controlplane_webhook.go @@ -21,6 +21,8 @@ import ( ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" + + bootstrapv1 "github.com/rancher-sandbox/cluster-api-provider-rke2/bootstrap/api/v1alpha1" ) // log is for logging in this package. @@ -39,7 +41,7 @@ var _ webhook.Defaulter = &RKE2ControlPlane{} // Default implements webhook.Defaulter so a webhook will be registered for the type. func (r *RKE2ControlPlane) Default() { - rke2controlplanelog.Info("default", "name", r.Name) + bootstrapv1.DefaultRKE2ConfigSpec(&r.Spec.RKE2ConfigSpec) } //+kubebuilder:webhook:path=/validate-controlplane-cluster-x-k8s-io-v1alpha1-rke2controlplane,mutating=false,failurePolicy=fail,sideEffects=None,groups=controlplane.cluster.x-k8s.io,resources=rke2controlplanes,verbs=create;update,versions=v1alpha1,name=vrke2controlplane.kb.io,admissionReviewVersions=v1 @@ -48,16 +50,12 @@ var _ webhook.Validator = &RKE2ControlPlane{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. func (r *RKE2ControlPlane) ValidateCreate() error { - rke2controlplanelog.Info("validate create", "name", r.Name) - - return nil + return bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec) } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. func (r *RKE2ControlPlane) ValidateUpdate(old runtime.Object) error { - rke2controlplanelog.Info("validate update", "name", r.Name) - - return nil + return bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec) } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.