Skip to content

Commit 7e7531a

Browse files
anmazzottinasusoba
andauthored
Implement CustomValidator and CustomDefaulter webhook interfaces. (#111)
* Implement CustomValidator and CustomDefaulter webhook interfaces. Signed-off-by: Andrea Mazzotti <[email protected]> * add default webhook Signed-off-by: nasusoba <[email protected]> fix webhook Signed-off-by: nasusoba <[email protected]> --------- Signed-off-by: Andrea Mazzotti <[email protected]> Signed-off-by: nasusoba <[email protected]> Co-authored-by: nasusoba <[email protected]>
1 parent 2a14b6e commit 7e7531a

File tree

5 files changed

+74
-43
lines changed

5 files changed

+74
-43
lines changed

bootstrap/api/v1beta2/kthreesconfig_webhook.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,44 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/apimachinery/pkg/runtime"
2123
ctrl "sigs.k8s.io/controller-runtime"
22-
"sigs.k8s.io/controller-runtime/pkg/webhook"
2324
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2425
)
2526

2627
// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane.
2728
func (c *KThreesConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
2829
return ctrl.NewWebhookManagedBy(mgr).
2930
For(c).
31+
WithDefaulter(&KThreesConfig{}).
32+
WithValidator(&KThreesConfig{}).
3033
Complete()
3134
}
3235

33-
// +kubebuilder:webhook:verbs=create;update,path=/validate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfig,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kthreesconfig,versions=v1beta2,name=validation.kthreesconfig.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta2
34-
// +kubebuilder:webhook:verbs=create;update,path=/mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfig,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kthreesconfig,versions=v1beta2,name=default.kthreesconfig.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta2
36+
// +kubebuilder:webhook:verbs=create;update,path=/validate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfig,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kthreesconfigs,versions=v1beta2,name=validation.kthreesconfig.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
37+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfig,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kthreesconfigs,versions=v1beta2,name=default.kthreesconfig.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3538

36-
var _ webhook.Defaulter = &KThreesConfig{}
37-
var _ webhook.Validator = &KThreesConfig{}
39+
var _ admission.CustomDefaulter = &KThreesConfig{}
40+
var _ admission.CustomValidator = &KThreesConfig{}
3841

3942
// ValidateCreate will do any extra validation when creating a KThreesControlPlane.
40-
func (c *KThreesConfig) ValidateCreate() (admission.Warnings, error) {
43+
func (c *KThreesConfig) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
4144
return []string{}, nil
4245
}
4346

4447
// ValidateUpdate will do any extra validation when updating a KThreesControlPlane.
45-
func (c *KThreesConfig) ValidateUpdate(runtime.Object) (admission.Warnings, error) {
48+
func (c *KThreesConfig) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
4649
return []string{}, nil
4750
}
4851

4952
// ValidateDelete allows you to add any extra validation when deleting.
50-
func (c *KThreesConfig) ValidateDelete() (admission.Warnings, error) {
53+
func (c *KThreesConfig) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5154
return []string{}, nil
5255
}
5356

5457
// Default will set default values for the KThreesControlPlane.
55-
func (c *KThreesConfig) Default() {
58+
func (c *KThreesConfig) Default(_ context.Context, _ runtime.Object) error {
59+
return nil
5660
}

bootstrap/api/v1beta2/kthreesconfigtemplate_webhook.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,44 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/apimachinery/pkg/runtime"
2123
ctrl "sigs.k8s.io/controller-runtime"
22-
"sigs.k8s.io/controller-runtime/pkg/webhook"
2324
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2425
)
2526

2627
// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane.
2728
func (c *KThreesConfigTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
2829
return ctrl.NewWebhookManagedBy(mgr).
2930
For(c).
31+
WithDefaulter(&KThreesConfigTemplate{}).
32+
WithValidator(&KThreesConfigTemplate{}).
3033
Complete()
3134
}
3235

33-
// +kubebuilder:webhook:verbs=create;update,path=/validate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kthreesconfigtemplate,versions=v1beta2,name=validation.kthreesconfigtemplate.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta2
34-
// +kubebuilder:webhook:verbs=create;update,path=/mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kthreesconfigtemplate,versions=v1beta2,name=default.kthreesconfigtemplate.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta2
36+
// +kubebuilder:webhook:verbs=create;update,path=/validate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kthreesconfigtemplates,versions=v1beta2,name=validation.kthreesconfigtemplate.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
37+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kthreesconfigtemplates,versions=v1beta2,name=default.kthreesconfigtemplate.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3538

36-
var _ webhook.Defaulter = &KThreesConfigTemplate{}
37-
var _ webhook.Validator = &KThreesConfigTemplate{}
39+
var _ admission.CustomDefaulter = &KThreesConfigTemplate{}
40+
var _ admission.CustomValidator = &KThreesConfigTemplate{}
3841

3942
// ValidateCreate will do any extra validation when creating a KThreesControlPlane.
40-
func (c *KThreesConfigTemplate) ValidateCreate() (admission.Warnings, error) {
43+
func (c *KThreesConfigTemplate) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
4144
return []string{}, nil
4245
}
4346

4447
// ValidateUpdate will do any extra validation when updating a KThreesControlPlane.
45-
func (c *KThreesConfigTemplate) ValidateUpdate(runtime.Object) (admission.Warnings, error) {
48+
func (c *KThreesConfigTemplate) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
4649
return []string{}, nil
4750
}
4851

4952
// ValidateDelete allows you to add any extra validation when deleting.
50-
func (c *KThreesConfigTemplate) ValidateDelete() (admission.Warnings, error) {
53+
func (c *KThreesConfigTemplate) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5154
return []string{}, nil
5255
}
5356

5457
// Default will set default values for the KThreesControlPlane.
55-
func (c *KThreesConfigTemplate) Default() {
58+
func (c *KThreesConfigTemplate) Default(_ context.Context, _ runtime.Object) error {
59+
return nil
5660
}

bootstrap/config/webhook/manifests.yaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ metadata:
66
webhooks:
77
- admissionReviewVersions:
88
- v1
9-
- v1beta2
9+
- v1beta1
1010
clientConfig:
1111
service:
1212
name: webhook-service
1313
namespace: system
14-
path: /mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfig
14+
path: /mutate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfig
1515
failurePolicy: Fail
1616
matchPolicy: Equivalent
1717
name: default.kthreesconfig.bootstrap.cluster.x-k8s.io
@@ -24,16 +24,16 @@ webhooks:
2424
- CREATE
2525
- UPDATE
2626
resources:
27-
- kthreesconfig
27+
- kthreesconfigs
2828
sideEffects: None
2929
- admissionReviewVersions:
3030
- v1
31-
- v1beta2
31+
- v1beta1
3232
clientConfig:
3333
service:
3434
name: webhook-service
3535
namespace: system
36-
path: /mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate
36+
path: /mutate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate
3737
failurePolicy: Fail
3838
matchPolicy: Equivalent
3939
name: default.kthreesconfigtemplate.bootstrap.cluster.x-k8s.io
@@ -46,7 +46,7 @@ webhooks:
4646
- CREATE
4747
- UPDATE
4848
resources:
49-
- kthreesconfigtemplate
49+
- kthreesconfigtemplates
5050
sideEffects: None
5151
---
5252
apiVersion: admissionregistration.k8s.io/v1
@@ -56,12 +56,12 @@ metadata:
5656
webhooks:
5757
- admissionReviewVersions:
5858
- v1
59-
- v1beta2
59+
- v1beta1
6060
clientConfig:
6161
service:
6262
name: webhook-service
6363
namespace: system
64-
path: /validate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfig
64+
path: /validate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfig
6565
failurePolicy: Fail
6666
matchPolicy: Equivalent
6767
name: validation.kthreesconfig.bootstrap.cluster.x-k8s.io
@@ -74,16 +74,16 @@ webhooks:
7474
- CREATE
7575
- UPDATE
7676
resources:
77-
- kthreesconfig
77+
- kthreesconfigs
7878
sideEffects: None
7979
- admissionReviewVersions:
8080
- v1
81-
- v1beta2
81+
- v1beta1
8282
clientConfig:
8383
service:
8484
name: webhook-service
8585
namespace: system
86-
path: /validate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate
86+
path: /validate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate
8787
failurePolicy: Fail
8888
matchPolicy: Equivalent
8989
name: validation.kthreesconfigtemplate.bootstrap.cluster.x-k8s.io
@@ -96,5 +96,5 @@ webhooks:
9696
- CREATE
9797
- UPDATE
9898
resources:
99-
- kthreesconfigtemplate
99+
- kthreesconfigtemplates
100100
sideEffects: None

controlplane/api/v1beta2/kthreescontrolplane_webhook.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,63 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
21+
"fmt"
22+
23+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2024
"k8s.io/apimachinery/pkg/runtime"
2125
ctrl "sigs.k8s.io/controller-runtime"
22-
"sigs.k8s.io/controller-runtime/pkg/webhook"
2326
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2427
)
2528

2629
// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane.
2730
func (in *KThreesControlPlane) SetupWebhookWithManager(mgr ctrl.Manager) error {
2831
return ctrl.NewWebhookManagedBy(mgr).
2932
For(in).
33+
WithDefaulter(&KThreesControlPlane{}).
34+
WithValidator(&KThreesControlPlane{}).
3035
Complete()
3136
}
3237

33-
// +kubebuilder:webhook:verbs=create;update,path=/validate-controlplane-cluster-x-k8s-io-v1beta2-kthreescontrolplane,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=controlplane.cluster.x-k8s.io,resources=kthreescontrolplane,versions=v1beta2,name=validation.kthreescontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta2
34-
// +kubebuilder:webhook:verbs=create;update,path=/mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreescontrolplane,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=controlplane.cluster.x-k8s.io,resources=kthreescontrolplane,versions=v1beta2,name=default.kthreescontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta2
38+
// +kubebuilder:webhook:verbs=create;update,path=/validate-controlplane-cluster-x-k8s-io-v1beta2-kthreescontrolplane,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=controlplane.cluster.x-k8s.io,resources=kthreescontrolplanes,versions=v1beta2,name=validation.kthreescontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
39+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreescontrolplane,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=controlplane.cluster.x-k8s.io,resources=kthreescontrolplanes,versions=v1beta2,name=default.kthreescontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3540

36-
var _ webhook.Defaulter = &KThreesControlPlane{}
37-
var _ webhook.Validator = &KThreesControlPlane{}
41+
var _ admission.CustomDefaulter = &KThreesControlPlane{}
42+
var _ admission.CustomValidator = &KThreesControlPlane{}
3843

3944
// ValidateCreate will do any extra validation when creating a KThreesControlPlane.
40-
func (in *KThreesControlPlane) ValidateCreate() (admission.Warnings, error) {
45+
func (in *KThreesControlPlane) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
4146
return []string{}, nil
4247
}
4348

4449
// ValidateUpdate will do any extra validation when updating a KThreesControlPlane.
45-
func (in *KThreesControlPlane) ValidateUpdate(runtime.Object) (admission.Warnings, error) {
50+
func (in *KThreesControlPlane) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
4651
return []string{}, nil
4752
}
4853

4954
// ValidateDelete allows you to add any extra validation when deleting.
50-
func (in *KThreesControlPlane) ValidateDelete() (admission.Warnings, error) {
55+
func (in *KThreesControlPlane) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5156
return []string{}, nil
5257
}
5358

5459
// Default will set default values for the KThreesControlPlane.
55-
func (in *KThreesControlPlane) Default() {
60+
func (in *KThreesControlPlane) Default(_ context.Context, obj runtime.Object) error {
61+
c, ok := obj.(*KThreesControlPlane)
62+
if !ok {
63+
return apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfig but got a %T", obj))
64+
}
65+
66+
defaultKThreesControlPlaneSpec(&c.Spec, c.Namespace)
67+
return nil
68+
}
69+
70+
func defaultKThreesControlPlaneSpec(s *KThreesControlPlaneSpec, namespace string) {
71+
if s.Replicas == nil {
72+
replicas := int32(1)
73+
s.Replicas = &replicas
74+
}
75+
76+
if s.MachineTemplate.InfrastructureRef.Namespace == "" {
77+
s.MachineTemplate.InfrastructureRef.Namespace = namespace
78+
}
5679
}

controlplane/config/webhook/manifests.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ metadata:
66
webhooks:
77
- admissionReviewVersions:
88
- v1
9-
- v1beta2
9+
- v1beta1
1010
clientConfig:
1111
service:
1212
name: webhook-service
@@ -24,7 +24,7 @@ webhooks:
2424
- CREATE
2525
- UPDATE
2626
resources:
27-
- kthreescontrolplane
27+
- kthreescontrolplanes
2828
sideEffects: None
2929
---
3030
apiVersion: admissionregistration.k8s.io/v1
@@ -34,7 +34,7 @@ metadata:
3434
webhooks:
3535
- admissionReviewVersions:
3636
- v1
37-
- v1beta2
37+
- v1beta1
3838
clientConfig:
3939
service:
4040
name: webhook-service
@@ -52,5 +52,5 @@ webhooks:
5252
- CREATE
5353
- UPDATE
5454
resources:
55-
- kthreescontrolplane
55+
- kthreescontrolplanes
5656
sideEffects: None

0 commit comments

Comments
 (0)