sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/internal/webhooks/kubeadmconfig.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  	"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"
    28  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    29  
    30  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
    31  )
    32  
    33  func (webhook *KubeadmConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
    34  	return ctrl.NewWebhookManagedBy(mgr).
    35  		For(&bootstrapv1.KubeadmConfig{}).
    36  		WithDefaulter(webhook).
    37  		WithValidator(webhook).
    38  		Complete()
    39  }
    40  
    41  // +kubebuilder:webhook:verbs=create;update,path=/mutate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfig,mutating=true,failurePolicy=fail,groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigs,versions=v1beta1,name=default.kubeadmconfig.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
    42  // +kubebuilder:webhook:verbs=create;update,path=/validate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfig,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigs,versions=v1beta1,name=validation.kubeadmconfig.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
    43  
    44  // KubeadmConfig implements a validation and defaulting webhook for KubeadmConfig.
    45  type KubeadmConfig struct{}
    46  
    47  var _ webhook.CustomValidator = &KubeadmConfig{}
    48  var _ webhook.CustomDefaulter = &KubeadmConfig{}
    49  
    50  // Default implements webhook.Defaulter so a webhook will be registered for the type.
    51  func (webhook *KubeadmConfig) Default(_ context.Context, obj runtime.Object) error {
    52  	c, ok := obj.(*bootstrapv1.KubeadmConfig)
    53  	if !ok {
    54  		return apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfig but got a %T", obj))
    55  	}
    56  
    57  	c.Spec.Default()
    58  
    59  	return nil
    60  }
    61  
    62  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
    63  func (webhook *KubeadmConfig) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
    64  	c, ok := obj.(*bootstrapv1.KubeadmConfig)
    65  	if !ok {
    66  		return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfig but got a %T", obj))
    67  	}
    68  
    69  	return nil, webhook.validate(c.Spec, c.Name)
    70  }
    71  
    72  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
    73  func (webhook *KubeadmConfig) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
    74  	newC, ok := newObj.(*bootstrapv1.KubeadmConfig)
    75  	if !ok {
    76  		return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfig but got a %T", newObj))
    77  	}
    78  
    79  	return nil, webhook.validate(newC.Spec, newC.Name)
    80  }
    81  
    82  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
    83  func (webhook *KubeadmConfig) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
    84  	return nil, nil
    85  }
    86  
    87  func (webhook *KubeadmConfig) validate(c bootstrapv1.KubeadmConfigSpec, name string) error {
    88  	allErrs := c.Validate(field.NewPath("spec"))
    89  
    90  	if len(allErrs) == 0 {
    91  		return nil
    92  	}
    93  
    94  	return apierrors.NewInvalid(bootstrapv1.GroupVersion.WithKind("KubeadmConfig").GroupKind(), name, allErrs)
    95  }