Skip to content

Commit

Permalink
add default webhook
Browse files Browse the repository at this point in the history
Signed-off-by: nasusoba <[email protected]>

fix webhook

Signed-off-by: nasusoba <[email protected]>
  • Loading branch information
nasusoba authored and anmazzotti committed May 15, 2024
1 parent 2b24d34 commit 0c1a43b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
6 changes: 4 additions & 2 deletions bootstrap/api/v1beta2/kthreesconfig_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import (
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 _ admission.CustomDefaulter = &KThreesConfig{}
var _ admission.CustomValidator = &KThreesConfig{}
Expand Down
6 changes: 4 additions & 2 deletions bootstrap/api/v1beta2/kthreesconfigtemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import (
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 _ admission.CustomDefaulter = &KThreesConfigTemplate{}
var _ admission.CustomValidator = &KThreesConfigTemplate{}
Expand Down
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
27 changes: 24 additions & 3 deletions controlplane/api/v1beta2/kthreescontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ 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/admission"
Expand All @@ -28,11 +30,13 @@ import (
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 _ admission.CustomDefaulter = &KThreesControlPlane{}
var _ admission.CustomValidator = &KThreesControlPlane{}
Expand All @@ -53,6 +57,23 @@ func (in *KThreesControlPlane) ValidateDelete(_ context.Context, _ runtime.Objec
}

// Default will set default values for the KThreesControlPlane.
func (in *KThreesControlPlane) Default(_ context.Context, _ runtime.Object) error {
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 0c1a43b

Please sign in to comment.