...

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  	"time"
    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 timechaoslog = logf.Log.WithName("timechaos-resource")
    28  
    29  // +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
    30  
    31  var _ webhook.Defaulter = &TimeChaos{}
    32  
    33  // Default implements webhook.Defaulter so a webhook will be registered for the type
    34  func (in *TimeChaos) Default() {
    35  	timechaoslog.Info("default", "name", in.Name)
    36  
    37  	in.Spec.Selector.DefaultNamespace(in.GetNamespace())
    38  	in.Spec.DefaultClockIds()
    39  }
    40  
    41  // +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
    42  
    43  var _ ChaosValidator = &TimeChaos{}
    44  
    45  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    46  func (in *TimeChaos) ValidateCreate() error {
    47  	timechaoslog.Info("validate create", "name", in.Name)
    48  	return in.Validate()
    49  }
    50  
    51  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    52  func (in *TimeChaos) ValidateUpdate(old runtime.Object) error {
    53  	timechaoslog.Info("validate update", "name", in.Name)
    54  	return in.Validate()
    55  }
    56  
    57  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    58  func (in *TimeChaos) ValidateDelete() error {
    59  	timechaoslog.Info("validate delete", "name", in.Name)
    60  
    61  	// Nothing to do?
    62  	return nil
    63  }
    64  
    65  // Validate validates chaos object
    66  func (in *TimeChaos) Validate() error {
    67  	specField := field.NewPath("spec")
    68  	allErrs := in.ValidateScheduler(specField)
    69  	allErrs = append(allErrs, in.ValidatePodMode(specField)...)
    70  	allErrs = append(allErrs, in.Spec.validateTimeOffset(specField.Child("timeOffset"))...)
    71  
    72  	if len(allErrs) > 0 {
    73  		return fmt.Errorf(allErrs.ToAggregate().Error())
    74  	}
    75  	return nil
    76  }
    77  
    78  // ValidateScheduler validates the scheduler and duration
    79  func (in *TimeChaos) ValidateScheduler(spec *field.Path) field.ErrorList {
    80  	return ValidateScheduler(in, spec)
    81  }
    82  
    83  // ValidatePodMode validates the value with podmode
    84  func (in *TimeChaos) ValidatePodMode(spec *field.Path) field.ErrorList {
    85  	return ValidatePodMode(in.Spec.Value, in.Spec.Mode, spec.Child("value"))
    86  }
    87  
    88  // SelectSpec returns the selector config for authority validate
    89  func (in *TimeChaos) GetSelectSpec() []SelectSpec {
    90  	return []SelectSpec{&in.Spec}
    91  }
    92  
    93  // validateTimeOffset validates the timeOffset
    94  func (in *TimeChaosSpec) validateTimeOffset(timeOffset *field.Path) field.ErrorList {
    95  	allErrs := field.ErrorList{}
    96  
    97  	_, err := time.ParseDuration(in.TimeOffset)
    98  	if err != nil {
    99  		allErrs = append(allErrs, field.Invalid(timeOffset,
   100  			in.TimeOffset,
   101  			fmt.Sprintf("parse timeOffset field error:%s", err)))
   102  	}
   103  
   104  	return allErrs
   105  }
   106