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

     1  /*
     2  Copyright 2020 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  	"net/http"
    22  
    23  	"github.com/pkg/errors"
    24  	autoscalingv1 "k8s.io/api/autoscaling/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	ctrl "sigs.k8s.io/controller-runtime"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    29  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    30  
    31  	controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
    32  )
    33  
    34  func (v *ScaleValidator) SetupWebhookWithManager(mgr ctrl.Manager) error {
    35  	v.decoder = admission.NewDecoder(mgr.GetScheme())
    36  
    37  	mgr.GetWebhookServer().Register("/validate-scale-controlplane-cluster-x-k8s-io-v1beta1-kubeadmcontrolplane", &webhook.Admission{
    38  		Handler: v,
    39  	})
    40  	return nil
    41  }
    42  
    43  // +kubebuilder:webhook:verbs=update,path=/validate-scale-controlplane-cluster-x-k8s-io-v1beta1-kubeadmcontrolplane,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=controlplane.cluster.x-k8s.io,resources=kubeadmcontrolplanes/scale,versions=v1beta1,name=validation-scale.kubeadmcontrolplane.controlplane.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
    44  
    45  // ScaleValidator validates KCP for replicas.
    46  type ScaleValidator struct {
    47  	Client  client.Reader
    48  	decoder *admission.Decoder
    49  }
    50  
    51  // Handle will validate for number of replicas.
    52  func (v *ScaleValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
    53  	scale := &autoscalingv1.Scale{}
    54  
    55  	err := v.decoder.Decode(req, scale)
    56  	if err != nil {
    57  		return admission.Errored(http.StatusBadRequest, errors.Wrapf(err, "failed to decode Scale resource"))
    58  	}
    59  
    60  	kcp := &controlplanev1.KubeadmControlPlane{}
    61  	kcpKey := types.NamespacedName{Namespace: scale.ObjectMeta.Namespace, Name: scale.ObjectMeta.Name}
    62  	if err = v.Client.Get(ctx, kcpKey, kcp); err != nil {
    63  		return admission.Errored(http.StatusInternalServerError, errors.Wrapf(err, "failed to get KubeadmControlPlane %s/%s", scale.ObjectMeta.Namespace, scale.ObjectMeta.Name))
    64  	}
    65  
    66  	if scale.Spec.Replicas == 0 {
    67  		return admission.Denied("replicas cannot be 0")
    68  	}
    69  
    70  	externalEtcd := false
    71  	if kcp.Spec.KubeadmConfigSpec.ClusterConfiguration != nil {
    72  		if kcp.Spec.KubeadmConfigSpec.ClusterConfiguration.Etcd.External != nil {
    73  			externalEtcd = true
    74  		}
    75  	}
    76  
    77  	if !externalEtcd {
    78  		if scale.Spec.Replicas%2 == 0 {
    79  			return admission.Denied("replicas cannot be an even number when etcd is stacked")
    80  		}
    81  	}
    82  
    83  	return admission.Allowed("")
    84  }