...

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  
    19  	"k8s.io/apimachinery/pkg/runtime"
    20  	"k8s.io/apimachinery/pkg/util/validation/field"
    21  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    22  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    23  )
    24  
    25  // log is for logging in this package.
    26  var kernelchaoslog = logf.Log.WithName("kernelchaos-resource")
    27  
    28  // +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
    29  
    30  var _ webhook.Defaulter = &KernelChaos{}
    31  
    32  // Default implements webhook.Defaulter so a webhook will be registered for the type
    33  func (in *KernelChaos) Default() {
    34  	kernelchaoslog.Info("default", "name", in.Name)
    35  
    36  	in.Spec.Selector.DefaultNamespace(in.GetNamespace())
    37  }
    38  
    39  // +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
    40  
    41  var _ ChaosValidator = &KernelChaos{}
    42  
    43  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    44  func (in *KernelChaos) ValidateCreate() error {
    45  	kernelchaoslog.Info("validate create", "name", in.Name)
    46  	return in.Validate()
    47  }
    48  
    49  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    50  func (in *KernelChaos) ValidateUpdate(old runtime.Object) error {
    51  	kernelchaoslog.Info("validate update", "name", in.Name)
    52  	return in.Validate()
    53  }
    54  
    55  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    56  func (in *KernelChaos) ValidateDelete() error {
    57  	kernelchaoslog.Info("validate delete", "name", in.Name)
    58  
    59  	// Nothing to do?
    60  	return nil
    61  }
    62  
    63  // Validate validates chaos object
    64  func (in *KernelChaos) Validate() error {
    65  	specField := field.NewPath("spec")
    66  	allErrs := in.ValidateScheduler(specField)
    67  	allErrs = append(allErrs, in.ValidatePodMode(specField)...)
    68  
    69  	if len(allErrs) > 0 {
    70  		return fmt.Errorf(allErrs.ToAggregate().Error())
    71  	}
    72  	return nil
    73  }
    74  
    75  // ValidateScheduler validates the scheduler and duration
    76  func (in *KernelChaos) ValidateScheduler(spec *field.Path) field.ErrorList {
    77  	return ValidateScheduler(in, spec)
    78  }
    79  
    80  // ValidatePodMode validates the value with podmode
    81  func (in *KernelChaos) ValidatePodMode(spec *field.Path) field.ErrorList {
    82  	return ValidatePodMode(in.Spec.Value, in.Spec.Mode, spec.Child("value"))
    83  }
    84  
    85  // SelectSpec returns the selector config for authority validate
    86  func (in *KernelChaos) GetSelectSpec() []SelectSpec {
    87  	return []SelectSpec{&in.Spec}
    88  }
    89