...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/timechaos_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  	"time"
    20  
    21  	"k8s.io/apimachinery/pkg/runtime"
    22  	"k8s.io/apimachinery/pkg/util/validation/field"
    23  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    24  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    25  )
    26  
    27  // log is for logging in this package.
    28  var timechaoslog = logf.Log.WithName("timechaos-resource")
    29  
    30  // +kubebuilder:webhook:path=/mutate-chaos-mesh-org-v1alpha1-timechaos,mutating=true,failurePolicy=fail,groups=chaos-mesh.org,resources=timechaos,verbs=create;update,versions=v1alpha1,name=mtimechaos.kb.io
    31  
    32  var _ webhook.Defaulter = &TimeChaos{}
    33  
    34  // Default implements webhook.Defaulter so a webhook will be registered for the type
    35  func (in *TimeChaos) Default() {
    36  	timechaoslog.Info("default", "name", in.Name)
    37  
    38  	in.Spec.Selector.DefaultNamespace(in.GetNamespace())
    39  	in.Spec.Default()
    40  }
    41  
    42  func (in *TimeChaosSpec) Default() {
    43  	in.DefaultClockIds()
    44  }
    45  
    46  // +kubebuilder:webhook:verbs=create;update,path=/validate-chaos-mesh-org-v1alpha1-timechaos,mutating=false,failurePolicy=fail,groups=chaos-mesh.org,resources=timechaos,versions=v1alpha1,name=vtimechaos.kb.io
    47  
    48  var _ webhook.Validator = &TimeChaos{}
    49  
    50  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    51  func (in *TimeChaos) ValidateCreate() error {
    52  	timechaoslog.Info("validate create", "name", in.Name)
    53  	return in.Validate()
    54  }
    55  
    56  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    57  func (in *TimeChaos) ValidateUpdate(old runtime.Object) error {
    58  	timechaoslog.Info("validate update", "name", in.Name)
    59  	if !reflect.DeepEqual(in.Spec, old.(*TimeChaos).Spec) {
    60  		return ErrCanNotUpdateChaos
    61  	}
    62  	return in.Validate()
    63  }
    64  
    65  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    66  func (in *TimeChaos) ValidateDelete() error {
    67  	timechaoslog.Info("validate delete", "name", in.Name)
    68  
    69  	// Nothing to do?
    70  	return nil
    71  }
    72  
    73  // Validate validates chaos object
    74  func (in *TimeChaos) Validate() error {
    75  	allErrs := in.Spec.Validate()
    76  
    77  	if len(allErrs) > 0 {
    78  		return fmt.Errorf(allErrs.ToAggregate().Error())
    79  	}
    80  	return nil
    81  }
    82  
    83  func (in *TimeChaosSpec) Validate() field.ErrorList {
    84  	specField := field.NewPath("spec")
    85  	allErrs := in.validateTimeOffset(specField.Child("timeOffset"))
    86  	allErrs = append(allErrs, validateDuration(in, specField)...)
    87  
    88  	return allErrs
    89  }
    90  
    91  // validateTimeOffset validates the timeOffset
    92  func (in *TimeChaosSpec) validateTimeOffset(timeOffset *field.Path) field.ErrorList {
    93  	allErrs := field.ErrorList{}
    94  
    95  	_, err := time.ParseDuration(in.TimeOffset)
    96  	if err != nil {
    97  		allErrs = append(allErrs, field.Invalid(timeOffset,
    98  			in.TimeOffset,
    99  			fmt.Sprintf("parse timeOffset field error:%s", err)))
   100  	}
   101  
   102  	return allErrs
   103  }
   104