sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/internal/webhooks/kubeadmconfigtemplate.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package webhooks
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    24  	runtime "k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/util/validation/field"
    26  	ctrl "sigs.k8s.io/controller-runtime"
    27  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    28  
    29  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
    30  )
    31  
    32  func (webhook *KubeadmConfigTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
    33  	return ctrl.NewWebhookManagedBy(mgr).
    34  		For(&bootstrapv1.KubeadmConfigTemplate{}).
    35  		WithDefaulter(webhook).
    36  		WithValidator(webhook).
    37  		Complete()
    38  }
    39  
    40  // +kubebuilder:webhook:verbs=create;update,path=/mutate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfigtemplate,mutating=true,failurePolicy=fail,groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigtemplates,versions=v1beta1,name=default.kubeadmconfigtemplate.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
    41  // +kubebuilder:webhook:verbs=create;update,path=/validate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfigtemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigtemplates,versions=v1beta1,name=validation.kubeadmconfigtemplate.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
    42  
    43  // KubeadmConfigTemplate implements a validation and defaulting webhook for KubeadmConfigTemplate.
    44  type KubeadmConfigTemplate struct{}
    45  
    46  // Default implements webhook.Defaulter so a webhook will be registered for the type.
    47  func (webhook *KubeadmConfigTemplate) Default(_ context.Context, obj runtime.Object) error {
    48  	c, ok := obj.(*bootstrapv1.KubeadmConfigTemplate)
    49  	if !ok {
    50  		return apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfigTemplate but got a %T", obj))
    51  	}
    52  
    53  	c.Spec.Template.Spec.Default()
    54  
    55  	return nil
    56  }
    57  
    58  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
    59  func (webhook *KubeadmConfigTemplate) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
    60  	c, ok := obj.(*bootstrapv1.KubeadmConfigTemplate)
    61  	if !ok {
    62  		return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfigTemplate but got a %T", obj))
    63  	}
    64  
    65  	return nil, webhook.validate(&c.Spec, c.Name)
    66  }
    67  
    68  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
    69  func (webhook *KubeadmConfigTemplate) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
    70  	newC, ok := newObj.(*bootstrapv1.KubeadmConfigTemplate)
    71  	if !ok {
    72  		return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfigTemplate but got a %T", newObj))
    73  	}
    74  
    75  	return nil, webhook.validate(&newC.Spec, newC.Name)
    76  }
    77  
    78  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
    79  func (webhook *KubeadmConfigTemplate) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
    80  	return nil, nil
    81  }
    82  
    83  func (webhook *KubeadmConfigTemplate) validate(r *bootstrapv1.KubeadmConfigTemplateSpec, name string) error {
    84  	var allErrs field.ErrorList
    85  
    86  	allErrs = append(allErrs, r.Template.Spec.Validate(field.NewPath("spec", "template", "spec"))...)
    87  	// Validate the metadata of the template.
    88  	allErrs = append(allErrs, r.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))...)
    89  
    90  	if len(allErrs) == 0 {
    91  		return nil
    92  	}
    93  
    94  	return apierrors.NewInvalid(bootstrapv1.GroupVersion.WithKind("KubeadmConfigTemplate").GroupKind(), name, allErrs)
    95  }