...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/kernelchaos_webhook.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package v1alpha1
    15  
    16  import (
    17  	"fmt"
    18  	"reflect"
    19  
    20  	"k8s.io/apimachinery/pkg/runtime"
    21  	"k8s.io/apimachinery/pkg/util/validation/field"
    22  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    23  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    24  )
    25  
    26  // log is for logging in this package.
    27  var kernelchaoslog = logf.Log.WithName("kernelchaos-resource")
    28  
    29  // +kubebuilder:webhook:path=/mutate-chaos-mesh-org-v1alpha1-kernelchaos,mutating=true,failurePolicy=fail,groups=chaos-mesh.org,resources=kernelchaos,verbs=create;update,versions=v1alpha1,name=mkernelchaos.kb.io
    30  
    31  var _ webhook.Defaulter = &KernelChaos{}
    32  
    33  // Default implements webhook.Defaulter so a webhook will be registered for the type
    34  func (in *KernelChaos) Default() {
    35  	kernelchaoslog.Info("default", "name", in.Name)
    36  
    37  	in.Spec.Selector.DefaultNamespace(in.GetNamespace())
    38  	in.Spec.Default()
    39  }
    40  
    41  func (in *KernelChaosSpec) Default() {
    42  
    43  }
    44  
    45  // +kubebuilder:webhook:verbs=create;update,path=/validate-chaos-mesh-org-v1alpha1-kernelchaos,mutating=false,failurePolicy=fail,groups=chaos-mesh.org,resources=kernelchaos,versions=v1alpha1,name=vkernelchaos.kb.io
    46  
    47  var _ webhook.Validator = &KernelChaos{}
    48  
    49  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    50  func (in *KernelChaos) ValidateCreate() error {
    51  	kernelchaoslog.Info("validate create", "name", in.Name)
    52  	return in.Validate()
    53  }
    54  
    55  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    56  func (in *KernelChaos) ValidateUpdate(old runtime.Object) error {
    57  	kernelchaoslog.Info("validate update", "name", in.Name)
    58  	if !reflect.DeepEqual(in.Spec, old.(*KernelChaos).Spec) {
    59  		return ErrCanNotUpdateChaos
    60  	}
    61  	return in.Validate()
    62  }
    63  
    64  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    65  func (in *KernelChaos) ValidateDelete() error {
    66  	kernelchaoslog.Info("validate delete", "name", in.Name)
    67  
    68  	// Nothing to do?
    69  	return nil
    70  }
    71  
    72  // Validate validates chaos object
    73  func (in *KernelChaos) Validate() error {
    74  	allErrs := in.Spec.Validate()
    75  	if len(allErrs) > 0 {
    76  		return fmt.Errorf(allErrs.ToAggregate().Error())
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  func (in *KernelChaosSpec) Validate() field.ErrorList {
    83  	specField := field.NewPath("spec")
    84  	allErrs := validatePodSelector(in.PodSelector.Value, in.PodSelector.Mode, specField.Child("value"))
    85  	allErrs = append(allErrs, validateDuration(in, specField)...)
    86  
    87  	return allErrs
    88  }
    89