github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/apps/v1alpha1/servicedescriptor_webhook.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package v1alpha1
    21  
    22  import (
    23  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/apimachinery/pkg/util/validation/field"
    27  	ctrl "sigs.k8s.io/controller-runtime"
    28  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    29  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    30  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    31  )
    32  
    33  // log is for logging in this package.
    34  var servicedescriptorlog = logf.Log.WithName("servicedescriptor-resource")
    35  
    36  func (r *ServiceDescriptor) SetupWebhookWithManager(mgr ctrl.Manager) error {
    37  	return ctrl.NewWebhookManagedBy(mgr).
    38  		For(r).
    39  		Complete()
    40  }
    41  
    42  // TODO(user): EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN!
    43  
    44  //+kubebuilder:webhook:path=/mutate-apps-kubeblocks-io-v1alpha1-servicedescriptor,mutating=true,failurePolicy=fail,sideEffects=None,groups=apps.kubeblocks.io,resources=servicedescriptors,verbs=create;update,versions=v1alpha1,name=mservicedescriptor.kb.io,admissionReviewVersions=v1
    45  
    46  var _ webhook.Defaulter = &ServiceDescriptor{}
    47  
    48  // Default implements webhook.Defaulter so a webhook will be registered for the type
    49  func (r *ServiceDescriptor) Default() {
    50  	servicedescriptorlog.Info("default", "name", r.Name)
    51  
    52  	// TODO(user): fill in your defaulting logic.
    53  }
    54  
    55  // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
    56  //+kubebuilder:webhook:path=/validate-apps-kubeblocks-io-v1alpha1-servicedescriptor,mutating=false,failurePolicy=fail,sideEffects=None,groups=apps.kubeblocks.io,resources=servicedescriptors,verbs=create;update,versions=v1alpha1,name=vservicedescriptor.kb.io,admissionReviewVersions=v1
    57  
    58  var _ webhook.Validator = &ServiceDescriptor{}
    59  
    60  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    61  func (r *ServiceDescriptor) ValidateCreate() (admission.Warnings, error) {
    62  	servicedescriptorlog.Info("validate create", "name", r.Name)
    63  
    64  	return nil, r.validate()
    65  }
    66  
    67  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    68  func (r *ServiceDescriptor) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
    69  	servicedescriptorlog.Info("validate update", "name", r.Name)
    70  
    71  	return nil, r.validate()
    72  }
    73  
    74  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    75  func (r *ServiceDescriptor) ValidateDelete() (admission.Warnings, error) {
    76  	servicedescriptorlog.Info("validate delete", "name", r.Name)
    77  
    78  	return nil, r.validate()
    79  }
    80  
    81  func (r *ServiceDescriptor) validate() error {
    82  	var allErrs field.ErrorList
    83  
    84  	checkValueAndValueFrom := func(filed string, cvs ...*CredentialVar) {
    85  		if len(cvs) == 0 {
    86  			return
    87  		}
    88  		for _, cv := range cvs {
    89  			if cv == nil {
    90  				continue
    91  			}
    92  			if cv.Value != "" && cv.ValueFrom != nil {
    93  				allErrs = append(allErrs,
    94  					field.Forbidden(field.NewPath("ServiceDescriptor filed").Child(filed),
    95  						"value and valueFrom cannot be specified at the same time"))
    96  			}
    97  		}
    98  	}
    99  
   100  	checkValueAndValueFrom("auth", r.Spec.Auth.Username, r.Spec.Auth.Password)
   101  	checkValueAndValueFrom("endpoint", r.Spec.Endpoint)
   102  	checkValueAndValueFrom("port", r.Spec.Port)
   103  
   104  	if len(allErrs) > 0 {
   105  		return apierrors.NewInvalid(
   106  			schema.GroupKind{
   107  				Group: "apps.kubeblocks.io/v1alpha1",
   108  				Kind:  "ServiceDescriptor",
   109  			},
   110  			r.Name, allErrs)
   111  	}
   112  	return nil
   113  }