Skip to content

Commit

Permalink
Implement CustomValidator and CustomDefaulter webhook interfaces. (#111)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
anmazzotti and nasusoba authored May 16, 2024
1 parent 2a14b6e commit 7e7531a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 43 deletions.
22 changes: 13 additions & 9 deletions bootstrap/api/v1beta2/kthreesconfig_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,44 @@ limitations under the License.
package v1beta2

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane.
func (c *KThreesConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(c).
WithDefaulter(&KThreesConfig{}).
WithValidator(&KThreesConfig{}).
Complete()
}

// +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
// +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
// +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
// +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

var _ webhook.Defaulter = &KThreesConfig{}
var _ webhook.Validator = &KThreesConfig{}
var _ admission.CustomDefaulter = &KThreesConfig{}
var _ admission.CustomValidator = &KThreesConfig{}

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

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

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

// Default will set default values for the KThreesControlPlane.
func (c *KThreesConfig) Default() {
func (c *KThreesConfig) Default(_ context.Context, _ runtime.Object) error {
return nil
}
22 changes: 13 additions & 9 deletions bootstrap/api/v1beta2/kthreesconfigtemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,44 @@ limitations under the License.
package v1beta2

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane.
func (c *KThreesConfigTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(c).
WithDefaulter(&KThreesConfigTemplate{}).
WithValidator(&KThreesConfigTemplate{}).
Complete()
}

// +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
// +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
// +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
// +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

var _ webhook.Defaulter = &KThreesConfigTemplate{}
var _ webhook.Validator = &KThreesConfigTemplate{}
var _ admission.CustomDefaulter = &KThreesConfigTemplate{}
var _ admission.CustomValidator = &KThreesConfigTemplate{}

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

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

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

// Default will set default values for the KThreesControlPlane.
func (c *KThreesConfigTemplate) Default() {
func (c *KThreesConfigTemplate) Default(_ context.Context, _ runtime.Object) error {
return nil
}
24 changes: 12 additions & 12 deletions bootstrap/config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ metadata:
webhooks:
- admissionReviewVersions:
- v1
- v1beta2
- v1beta1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfig
path: /mutate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfig
failurePolicy: Fail
matchPolicy: Equivalent
name: default.kthreesconfig.bootstrap.cluster.x-k8s.io
Expand All @@ -24,16 +24,16 @@ webhooks:
- CREATE
- UPDATE
resources:
- kthreesconfig
- kthreesconfigs
sideEffects: None
- admissionReviewVersions:
- v1
- v1beta2
- v1beta1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate
path: /mutate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate
failurePolicy: Fail
matchPolicy: Equivalent
name: default.kthreesconfigtemplate.bootstrap.cluster.x-k8s.io
Expand All @@ -46,7 +46,7 @@ webhooks:
- CREATE
- UPDATE
resources:
- kthreesconfigtemplate
- kthreesconfigtemplates
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
Expand All @@ -56,12 +56,12 @@ metadata:
webhooks:
- admissionReviewVersions:
- v1
- v1beta2
- v1beta1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfig
path: /validate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfig
failurePolicy: Fail
matchPolicy: Equivalent
name: validation.kthreesconfig.bootstrap.cluster.x-k8s.io
Expand All @@ -74,16 +74,16 @@ webhooks:
- CREATE
- UPDATE
resources:
- kthreesconfig
- kthreesconfigs
sideEffects: None
- admissionReviewVersions:
- v1
- v1beta2
- v1beta1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-controlplane-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate
path: /validate-bootstrap-cluster-x-k8s-io-v1beta2-kthreesconfigtemplate
failurePolicy: Fail
matchPolicy: Equivalent
name: validation.kthreesconfigtemplate.bootstrap.cluster.x-k8s.io
Expand All @@ -96,5 +96,5 @@ webhooks:
- CREATE
- UPDATE
resources:
- kthreesconfigtemplate
- kthreesconfigtemplates
sideEffects: None
41 changes: 32 additions & 9 deletions controlplane/api/v1beta2/kthreescontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,63 @@ limitations under the License.
package v1beta2

import (
"context"
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane.
func (in *KThreesControlPlane) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(in).
WithDefaulter(&KThreesControlPlane{}).
WithValidator(&KThreesControlPlane{}).
Complete()
}

// +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
// +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
// +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
// +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

var _ webhook.Defaulter = &KThreesControlPlane{}
var _ webhook.Validator = &KThreesControlPlane{}
var _ admission.CustomDefaulter = &KThreesControlPlane{}
var _ admission.CustomValidator = &KThreesControlPlane{}

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

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

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

// Default will set default values for the KThreesControlPlane.
func (in *KThreesControlPlane) Default() {
func (in *KThreesControlPlane) Default(_ context.Context, obj runtime.Object) error {
c, ok := obj.(*KThreesControlPlane)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfig but got a %T", obj))
}

defaultKThreesControlPlaneSpec(&c.Spec, c.Namespace)
return nil
}

func defaultKThreesControlPlaneSpec(s *KThreesControlPlaneSpec, namespace string) {
if s.Replicas == nil {
replicas := int32(1)
s.Replicas = &replicas
}

if s.MachineTemplate.InfrastructureRef.Namespace == "" {
s.MachineTemplate.InfrastructureRef.Namespace = namespace
}
}
8 changes: 4 additions & 4 deletions controlplane/config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
webhooks:
- admissionReviewVersions:
- v1
- v1beta2
- v1beta1
clientConfig:
service:
name: webhook-service
Expand All @@ -24,7 +24,7 @@ webhooks:
- CREATE
- UPDATE
resources:
- kthreescontrolplane
- kthreescontrolplanes
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
Expand All @@ -34,7 +34,7 @@ metadata:
webhooks:
- admissionReviewVersions:
- v1
- v1beta2
- v1beta1
clientConfig:
service:
name: webhook-service
Expand All @@ -52,5 +52,5 @@ webhooks:
- CREATE
- UPDATE
resources:
- kthreescontrolplane
- kthreescontrolplanes
sideEffects: None

0 comments on commit 7e7531a

Please sign in to comment.