Skip to content

Commit 2189204

Browse files
Validate/default control plane object with webhooks (#123)
Signed-off-by: Alexandr Demicev <alexandr.demicev@suse.com>
1 parent ed217e7 commit 2189204

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

bootstrap/api/v1alpha1/rke2config_webhook.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ var _ webhook.Defaulter = &RKE2Config{}
4242

4343
// Default implements webhook.Defaulter so a webhook will be registered for the type.
4444
func (r *RKE2Config) Default() {
45-
defaultRKE2ConfigSpec(&r.Spec)
45+
DefaultRKE2ConfigSpec(&r.Spec)
4646
}
4747

48-
func defaultRKE2ConfigSpec(spec *RKE2ConfigSpec) {
48+
// DefaultRKE2ConfigSpec defaults the RKE2ConfigSpec.
49+
func DefaultRKE2ConfigSpec(spec *RKE2ConfigSpec) {
4950
if spec.AgentConfig.Format == "" {
5051
spec.AgentConfig.Format = CloudConfig
5152
}
@@ -57,20 +58,21 @@ var _ webhook.Validator = &RKE2Config{}
5758

5859
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
5960
func (r *RKE2Config) ValidateCreate() error {
60-
return validateRKE2ConfigSpec(r.Name, &r.Spec)
61+
return ValidateRKE2ConfigSpec(r.Name, &r.Spec)
6162
}
6263

6364
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
6465
func (r *RKE2Config) ValidateUpdate(_ runtime.Object) error {
65-
return validateRKE2ConfigSpec(r.Name, &r.Spec)
66+
return ValidateRKE2ConfigSpec(r.Name, &r.Spec)
6667
}
6768

6869
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
6970
func (r *RKE2Config) ValidateDelete() error {
7071
return nil
7172
}
7273

73-
func validateRKE2ConfigSpec(name string, spec *RKE2ConfigSpec) error {
74+
// ValidateRKE2ConfigSpec validates the RKE2ConfigSpec.
75+
func ValidateRKE2ConfigSpec(name string, spec *RKE2ConfigSpec) error {
7476
allErrs := spec.validate(field.NewPath("spec"))
7577

7678
if len(allErrs) == 0 {

controlplane/api/v1alpha1/rke2controlplane_webhook.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
ctrl "sigs.k8s.io/controller-runtime"
2222
logf "sigs.k8s.io/controller-runtime/pkg/log"
2323
"sigs.k8s.io/controller-runtime/pkg/webhook"
24+
25+
bootstrapv1 "github.com/rancher-sandbox/cluster-api-provider-rke2/bootstrap/api/v1alpha1"
2426
)
2527

2628
// log is for logging in this package.
@@ -39,7 +41,7 @@ var _ webhook.Defaulter = &RKE2ControlPlane{}
3941

4042
// Default implements webhook.Defaulter so a webhook will be registered for the type.
4143
func (r *RKE2ControlPlane) Default() {
42-
rke2controlplanelog.Info("default", "name", r.Name)
44+
bootstrapv1.DefaultRKE2ConfigSpec(&r.Spec.RKE2ConfigSpec)
4345
}
4446

4547
//+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{}
4850

4951
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
5052
func (r *RKE2ControlPlane) ValidateCreate() error {
51-
rke2controlplanelog.Info("validate create", "name", r.Name)
52-
53-
return nil
53+
return bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec)
5454
}
5555

5656
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
5757
func (r *RKE2ControlPlane) ValidateUpdate(old runtime.Object) error {
58-
rke2controlplanelog.Info("validate update", "name", r.Name)
59-
60-
return nil
58+
return bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec)
6159
}
6260

6361
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.

0 commit comments

Comments
 (0)