...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/dnschaos_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/runtime/log"
    23  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    24  )
    25  
    26  // log is for logging in this package.
    27  var dnschaoslog = logf.Log.WithName("dnschaos-resource")
    28  
    29  // +kubebuilder:webhook:path=/mutate-chaos-mesh-org-v1alpha1-dnschaos,mutating=true,failurePolicy=fail,groups=chaos-mesh.org,resources=dnschaos,verbs=create;update,versions=v1alpha1,name=mdnschaos.kb.io
    30  
    31  var _ webhook.Defaulter = &DNSChaos{}
    32  
    33  // Default implements webhook.Defaulter so a webhook will be registered for the type
    34  func (in *DNSChaos) Default() {
    35  	dnschaoslog.Info("default", "name", in.Name)
    36  
    37  	in.Spec.Selector.DefaultNamespace(in.GetNamespace())
    38  	in.Spec.Default()
    39  }
    40  
    41  func (in *DNSChaosSpec) Default() {
    42  }
    43  
    44  // +kubebuilder:webhook:verbs=create;update,path=/validate-chaos-mesh-org-v1alpha1-dnschaos,mutating=false,failurePolicy=fail,groups=chaos-mesh.org,resources=dnschaos,versions=v1alpha1,name=vdnschaos.kb.io
    45  
    46  var _ webhook.Validator = &DNSChaos{}
    47  
    48  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    49  func (in *DNSChaos) ValidateCreate() error {
    50  	dnschaoslog.Info("validate create", "name", in.Name)
    51  	return in.Validate()
    52  }
    53  
    54  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    55  func (in *DNSChaos) ValidateUpdate(old runtime.Object) error {
    56  	dnschaoslog.Info("validate update", "name", in.Name)
    57  	if !reflect.DeepEqual(in.Spec, old.(*DNSChaos).Spec) {
    58  		return ErrCanNotUpdateChaos
    59  	}
    60  	return in.Validate()
    61  }
    62  
    63  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    64  func (in *DNSChaos) ValidateDelete() error {
    65  	dnschaoslog.Info("validate delete", "name", in.Name)
    66  
    67  	// Nothing to do?
    68  	return nil
    69  }
    70  
    71  // Validate validates chaos object
    72  func (in *DNSChaos) Validate() error {
    73  	allErrs := in.Spec.Validate()
    74  	if len(allErrs) > 0 {
    75  		return fmt.Errorf(allErrs.ToAggregate().Error())
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func (in *DNSChaosSpec) Validate() field.ErrorList {
    82  	specField := field.NewPath("spec")
    83  	allErrs := validatePodSelector(in.PodSelector.Value, in.PodSelector.Mode, specField.Child("value"))
    84  	allErrs = append(allErrs, validateDuration(in, specField)...)
    85  	return allErrs
    86  }
    87