Skip to content

Commit

Permalink
Validate/default control plane object with webhooks (#123)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandr Demicev <[email protected]>
  • Loading branch information
alexander-demicev committed Mar 28, 2023
1 parent ed217e7 commit 2189204
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 7 additions & 5 deletions bootstrap/api/v1alpha1/rke2config_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -57,20 +58,21 @@ 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.
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 {
Expand Down
12 changes: 5 additions & 7 deletions controlplane/api/v1alpha1/rke2controlplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 2189204

Please sign in to comment.