Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 11 additions & 34 deletions bootstrap/api/v1beta1/rke2config_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (
"gopkg.in/yaml.v3"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

Expand All @@ -57,22 +55,16 @@ type RKE2ConfigCustomValidator struct{}

// SetupRKE2ConfigWebhookWithManager sets up the Controller Manager for the Webhook for the RKE2ControlPlaneTemplate resource.
func SetupRKE2ConfigWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(&RKE2Config{}).
return ctrl.NewWebhookManagedBy(mgr, &RKE2Config{}).
WithValidator(&RKE2ConfigCustomValidator{}).
WithDefaulter(&RKE2ConfigCustomDefaulter{}, admission.DefaulterRemoveUnknownOrOmitableFields).
Complete()
}

var _ webhook.CustomDefaulter = &RKE2ConfigCustomDefaulter{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (r *RKE2ConfigCustomDefaulter) Default(_ context.Context, obj runtime.Object) error {
rc, ok := obj.(*RKE2Config)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a RKE2Config but got a %T", obj))
}
var _ admission.Defaulter[*RKE2Config] = &RKE2ConfigCustomDefaulter{}

// Default implements admission.Defaulter so a webhook will be registered for the type.
func (r *RKE2ConfigCustomDefaulter) Default(_ context.Context, rc *RKE2Config) error {
rke2ConfigLogger.Info("defaulting", "RKE2Config", klog.KObj(rc))

DefaultRKE2ConfigSpec(&rc.Spec)
Expand Down Expand Up @@ -135,15 +127,10 @@ func CorrectArbitraryData(arbitraryData map[string]string) error {
return nil
}

var _ webhook.CustomValidator = &RKE2ConfigCustomValidator{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
rc, ok := obj.(*RKE2Config)
if !ok {
return nil, fmt.Errorf("expected a RKE2Config object but got %T", obj)
}
var _ admission.Validator[*RKE2Config] = &RKE2ConfigCustomValidator{}

// ValidateCreate implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateCreate(_ context.Context, rc *RKE2Config) (admission.Warnings, error) {
rke2ConfigLogger.Info("validate create", "RKE2Config", klog.KObj(rc))

var allErrs field.ErrorList
Expand All @@ -157,13 +144,8 @@ func (r *RKE2ConfigCustomValidator) ValidateCreate(_ context.Context, obj runtim
return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2Config").GroupKind(), rc.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
newrc, ok := newObj.(*RKE2Config)
if !ok {
return nil, fmt.Errorf("expected a RKE2Config object but got %T", newObj)
}

// ValidateUpdate implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateUpdate(_ context.Context, _, newrc *RKE2Config) (admission.Warnings, error) {
rke2ConfigLogger.Info("validate update", "RKE2Config", klog.KObj(newrc))

var allErrs field.ErrorList
Expand All @@ -177,13 +159,8 @@ func (r *RKE2ConfigCustomValidator) ValidateUpdate(_ context.Context, _, newObj
return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2Config").GroupKind(), newrc.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
rc, ok := obj.(*RKE2Config)
if !ok {
return nil, fmt.Errorf("expected a RKE2Config object but got %T", obj)
}

// ValidateDelete implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateDelete(_ context.Context, rc *RKE2Config) (admission.Warnings, error) {
rke2ConfigLogger.Info("validate delete", "RKE2Config", klog.KObj(rc))

return nil, nil
Expand Down
49 changes: 12 additions & 37 deletions bootstrap/api/v1beta1/rke2configtemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ package v1beta1

import (
"context"
"fmt"

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

Expand All @@ -48,62 +44,41 @@ type RKE2ConfigTemplateCustomValidator struct{}

// SetupRKE2ConfigTemplateWebhookWithManager sets up the Controller Manager for the Webhook for the RKE2ControlPlaneTemplate resource.
func SetupRKE2ConfigTemplateWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(&RKE2ConfigTemplate{}).
return ctrl.NewWebhookManagedBy(mgr, &RKE2ConfigTemplate{}).
WithValidator(&RKE2ConfigTemplateCustomValidator{}).
WithDefaulter(&RKE2ConfigTemplateCustomDefaulter{}, admission.DefaulterRemoveUnknownOrOmitableFields).
Complete()
}

var _ webhook.CustomDefaulter = &RKE2ConfigTemplateCustomDefaulter{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomDefaulter) Default(_ context.Context, obj runtime.Object) error {
rct, ok := obj.(*RKE2ConfigTemplate)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a RKE2ConfigTemplate but got a %T", obj))
}
var _ admission.Defaulter[*RKE2ConfigTemplate] = &RKE2ConfigTemplateCustomDefaulter{}

// Default implements admission.Defaulter so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomDefaulter) Default(_ context.Context, rct *RKE2ConfigTemplate) error {
rke2ConfigTemplateLogger.Info("defaulting", "RKE2ConfigTemplate", klog.KObj(rct))

DefaultRKE2ConfigSpec(&rct.Spec.Template.Spec)

return nil
}

var _ webhook.CustomValidator = &RKE2ConfigTemplateCustomValidator{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
rct, ok := obj.(*RKE2ConfigTemplate)
if !ok {
return nil, fmt.Errorf("expected a RKE2ConfigTemplate object but got %T", obj)
}
var _ admission.Validator[*RKE2ConfigTemplate] = &RKE2ConfigTemplateCustomValidator{}

// ValidateCreate implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateCreate(_ context.Context, rct *RKE2ConfigTemplate) (admission.Warnings, error) {
rke2ConfigTemplateLogger.Info("validate create", "RKE2ConfigTemplate", klog.KObj(rct))

return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateUpdate(_ context.Context, oldObj, _ runtime.Object) (admission.Warnings, error) {
rct, ok := oldObj.(*RKE2ConfigTemplate)
if !ok {
return nil, fmt.Errorf("expected a RKE2ConfigTemplate object but got %T", oldObj)
}

rke2ConfigTemplateLogger.Info("validate update", "RKE2ConfigTemplate", klog.KObj(rct))
// ValidateUpdate implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateUpdate(_ context.Context, oldRct, _ *RKE2ConfigTemplate) (admission.Warnings, error) {
rke2ConfigTemplateLogger.Info("validate update", "RKE2ConfigTemplate", klog.KObj(oldRct))

return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
rct, ok := obj.(*RKE2ConfigTemplate)
if !ok {
return nil, fmt.Errorf("expected a RKE2ConfigTemplate object but got %T", obj)
}

// ValidateDelete implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateDelete(_ context.Context, rct *RKE2ConfigTemplate) (admission.Warnings, error) {
rke2ConfigTemplateLogger.Info("validate delete", "RKE2ConfigTemplate", klog.KObj(rct))

return nil, nil
Expand Down
45 changes: 11 additions & 34 deletions bootstrap/api/v1beta2/rke2config_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (
"gopkg.in/yaml.v3"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

Expand All @@ -57,24 +55,18 @@ type RKE2ConfigCustomValidator struct{}

// SetupRKE2ConfigWebhookWithManager sets up the Controller Manager for the Webhook for the RKE2ControlPlaneTemplate resource.
func SetupRKE2ConfigWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(&RKE2Config{}).
return ctrl.NewWebhookManagedBy(mgr, &RKE2Config{}).
WithValidator(&RKE2ConfigCustomValidator{}).
WithDefaulter(&RKE2ConfigCustomDefaulter{}, admission.DefaulterRemoveUnknownOrOmitableFields).
Complete()
}

//+kubebuilder:webhook:path=/mutate-bootstrap-cluster-x-k8s-io-v1beta2-rke2config,mutating=true,failurePolicy=fail,sideEffects=None,groups=bootstrap.cluster.x-k8s.io,resources=rke2configs,verbs=create;update,versions=v1beta2,name=mrke2config.kb.io,admissionReviewVersions=v1;v1beta1

var _ webhook.CustomDefaulter = &RKE2ConfigCustomDefaulter{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (r *RKE2ConfigCustomDefaulter) Default(_ context.Context, obj runtime.Object) error {
rc, ok := obj.(*RKE2Config)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a RKE2Config but got a %T", obj))
}
var _ admission.Defaulter[*RKE2Config] = &RKE2ConfigCustomDefaulter{}

// Default implements admission.Defaulter so a webhook will be registered for the type.
func (r *RKE2ConfigCustomDefaulter) Default(_ context.Context, rc *RKE2Config) error {
rke2ConfigLogger.Info("defaulting", "RKE2Config", klog.KObj(rc))

DefaultRKE2ConfigSpec(&rc.Spec)
Expand Down Expand Up @@ -139,15 +131,10 @@ func CorrectArbitraryData(arbitraryData map[string]string) error {

//+kubebuilder:webhook:path=/validate-bootstrap-cluster-x-k8s-io-v1beta2-rke2config,mutating=false,failurePolicy=fail,sideEffects=None,groups=bootstrap.cluster.x-k8s.io,resources=rke2configs,verbs=create;update,versions=v1beta2,name=vrke2config.kb.io,admissionReviewVersions=v1;v1beta1

var _ webhook.CustomValidator = &RKE2ConfigCustomValidator{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
rc, ok := obj.(*RKE2Config)
if !ok {
return nil, fmt.Errorf("expected a RKE2Config object but got %T", obj)
}
var _ admission.Validator[*RKE2Config] = &RKE2ConfigCustomValidator{}

// ValidateCreate implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateCreate(_ context.Context, rc *RKE2Config) (admission.Warnings, error) {
rke2ConfigLogger.Info("validate create", "RKE2Config", klog.KObj(rc))

var allErrs field.ErrorList
Expand All @@ -161,13 +148,8 @@ func (r *RKE2ConfigCustomValidator) ValidateCreate(_ context.Context, obj runtim
return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2Config").GroupKind(), rc.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
newrc, ok := newObj.(*RKE2Config)
if !ok {
return nil, fmt.Errorf("expected a RKE2Config object but got %T", newObj)
}

// ValidateUpdate implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateUpdate(_ context.Context, _, newrc *RKE2Config) (admission.Warnings, error) {
rke2ConfigLogger.Info("validate update", "RKE2Config", klog.KObj(newrc))

var allErrs field.ErrorList
Expand All @@ -181,13 +163,8 @@ func (r *RKE2ConfigCustomValidator) ValidateUpdate(_ context.Context, _, newObj
return nil, apierrors.NewInvalid(GroupVersion.WithKind("RKE2Config").GroupKind(), newrc.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
rc, ok := obj.(*RKE2Config)
if !ok {
return nil, fmt.Errorf("expected a RKE2Config object but got %T", obj)
}

// ValidateDelete implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigCustomValidator) ValidateDelete(_ context.Context, rc *RKE2Config) (admission.Warnings, error) {
rke2ConfigLogger.Info("validate delete", "RKE2Config", klog.KObj(rc))

return nil, nil
Expand Down
49 changes: 12 additions & 37 deletions bootstrap/api/v1beta2/rke2configtemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ package v1beta2

import (
"context"
"fmt"

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

Expand All @@ -48,24 +44,18 @@ type RKE2ConfigTemplateCustomValidator struct{}

// SetupRKE2ConfigTemplateWebhookWithManager sets up the Controller Manager for the Webhook for the RKE2ControlPlaneTemplate resource.
func SetupRKE2ConfigTemplateWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(&RKE2ConfigTemplate{}).
return ctrl.NewWebhookManagedBy(mgr, &RKE2ConfigTemplate{}).
WithValidator(&RKE2ConfigTemplateCustomValidator{}).
WithDefaulter(&RKE2ConfigTemplateCustomDefaulter{}, admission.DefaulterRemoveUnknownOrOmitableFields).
Complete()
}

//+kubebuilder:webhook:path=/mutate-bootstrap-cluster-x-k8s-io-v1beta2-rke2configtemplate,mutating=true,failurePolicy=fail,sideEffects=None,groups=bootstrap.cluster.x-k8s.io,resources=rke2configtemplates,verbs=create;update,versions=v1beta2,name=mrke2configtemplate.kb.io,admissionReviewVersions=v1;v1beta1

var _ webhook.CustomDefaulter = &RKE2ConfigTemplateCustomDefaulter{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomDefaulter) Default(_ context.Context, obj runtime.Object) error {
rct, ok := obj.(*RKE2ConfigTemplate)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a RKE2ConfigTemplate but got a %T", obj))
}
var _ admission.Defaulter[*RKE2ConfigTemplate] = &RKE2ConfigTemplateCustomDefaulter{}

// Default implements admission.Defaulter so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomDefaulter) Default(_ context.Context, rct *RKE2ConfigTemplate) error {
rke2ConfigTemplateLogger.Info("defaulting", "RKE2ConfigTemplate", klog.KObj(rct))

DefaultRKE2ConfigSpec(&rct.Spec.Template.Spec)
Expand All @@ -75,39 +65,24 @@ func (r *RKE2ConfigTemplateCustomDefaulter) Default(_ context.Context, obj runti

//+kubebuilder:webhook:path=/validate-bootstrap-cluster-x-k8s-io-v1beta2-rke2configtemplate,mutating=false,failurePolicy=fail,sideEffects=None,groups=bootstrap.cluster.x-k8s.io,resources=rke2configtemplates,verbs=create;update,versions=v1beta2,name=vrke2configtemplate.kb.io,admissionReviewVersions=v1;v1beta1

var _ webhook.CustomValidator = &RKE2ConfigTemplateCustomValidator{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
rct, ok := obj.(*RKE2ConfigTemplate)
if !ok {
return nil, fmt.Errorf("expected a RKE2ConfigTemplate object but got %T", obj)
}
var _ admission.Validator[*RKE2ConfigTemplate] = &RKE2ConfigTemplateCustomValidator{}

// ValidateCreate implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateCreate(_ context.Context, rct *RKE2ConfigTemplate) (admission.Warnings, error) {
rke2ConfigTemplateLogger.Info("validate create", "RKE2ConfigTemplate", klog.KObj(rct))

return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateUpdate(_ context.Context, oldObj, _ runtime.Object) (admission.Warnings, error) {
rct, ok := oldObj.(*RKE2ConfigTemplate)
if !ok {
return nil, fmt.Errorf("expected a RKE2ConfigTemplate object but got %T", oldObj)
}

rke2ConfigTemplateLogger.Info("validate update", "RKE2ConfigTemplate", klog.KObj(rct))
// ValidateUpdate implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateUpdate(_ context.Context, oldRct, _ *RKE2ConfigTemplate) (admission.Warnings, error) {
rke2ConfigTemplateLogger.Info("validate update", "RKE2ConfigTemplate", klog.KObj(oldRct))

return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
rct, ok := obj.(*RKE2ConfigTemplate)
if !ok {
return nil, fmt.Errorf("expected a RKE2ConfigTemplate object but got %T", obj)
}

// ValidateDelete implements admission.Validator so a webhook will be registered for the type.
func (r *RKE2ConfigTemplateCustomValidator) ValidateDelete(_ context.Context, rct *RKE2ConfigTemplate) (admission.Warnings, error) {
rke2ConfigTemplateLogger.Info("validate delete", "RKE2ConfigTemplate", klog.KObj(rct))

return nil, nil
Expand Down
Loading