...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/stresschaos_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  	"errors"
    18  	"fmt"
    19  	"reflect"
    20  	"strconv"
    21  
    22  	"github.com/docker/go-units"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  	ctrl "sigs.k8s.io/controller-runtime"
    26  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    27  )
    28  
    29  // +kubebuilder:object:generate=false
    30  
    31  // Validateable defines how a resource is validated
    32  type Validateable interface {
    33  	Validate(parent *field.Path) field.ErrorList
    34  }
    35  
    36  // log is for logging in this package.
    37  var (
    38  	stressChaosLog = ctrl.Log.WithName("stresschaos-resource")
    39  )
    40  
    41  // +kubebuilder:webhook:path=/mutate-chaos-mesh-org-v1alpha1-stresschaos,mutating=true,failurePolicy=fail,groups=chaos-mesh.org,resources=stresschaos,verbs=create;update,versions=v1alpha1,name=mstresschaos.kb.io
    42  
    43  var _ webhook.Defaulter = &StressChaos{}
    44  
    45  // Default implements webhook.Defaulter so a webhook will be registered for the type
    46  func (in *StressChaos) Default() {
    47  	stressChaosLog.Info("default", "name", in.Name)
    48  	in.Spec.Selector.DefaultNamespace(in.GetNamespace())
    49  	in.Spec.Default()
    50  }
    51  
    52  func (in *StressChaosSpec) Default() {
    53  
    54  }
    55  
    56  // +kubebuilder:webhook:verbs=create;update,path=/validate-chaos-mesh-org-v1alpha1-stresschaos,mutating=false,failurePolicy=fail,groups=chaos-mesh.org,resources=stresschaos,versions=v1alpha1,name=vstresschaos.kb.io
    57  
    58  var _ webhook.Validator = &StressChaos{}
    59  
    60  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    61  func (in *StressChaos) ValidateCreate() error {
    62  	stressChaosLog.Info("validate create", "name", in.Name)
    63  	return in.Validate()
    64  }
    65  
    66  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    67  func (in *StressChaos) ValidateUpdate(old runtime.Object) error {
    68  	stressChaosLog.Info("validate update", "name", in.Name)
    69  	if !reflect.DeepEqual(in.Spec, old.(*StressChaos).Spec) {
    70  		return ErrCanNotUpdateChaos
    71  	}
    72  	return in.Validate()
    73  }
    74  
    75  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    76  func (in *StressChaos) ValidateDelete() error {
    77  	stressChaosLog.Info("validate delete", "name", in.Name)
    78  
    79  	// Nothing to do?
    80  	return nil
    81  }
    82  
    83  // Validate validates chaos object
    84  func (in *StressChaos) Validate() error {
    85  	errs := in.Spec.Validate()
    86  	if len(errs) > 0 {
    87  		return fmt.Errorf(errs.ToAggregate().Error())
    88  	}
    89  	return nil
    90  }
    91  
    92  // Validate validates the scheduler and duration
    93  func (in *StressChaosSpec) Validate() field.ErrorList {
    94  	errs := field.ErrorList{}
    95  	specField := field.NewPath("spec")
    96  	var allErrs field.ErrorList
    97  	if len(in.StressngStressors) == 0 && in.Stressors == nil {
    98  		allErrs = append(errs, field.Invalid(specField, in, "missing stressors"))
    99  	} else if in.Stressors != nil {
   100  		allErrs = append(errs, in.Stressors.Validate(specField)...)
   101  	}
   102  	allErrs = append(allErrs, validateDuration(in, specField)...)
   103  	return allErrs
   104  }
   105  
   106  // Validate validates whether the Stressors are all well defined
   107  func (in *Stressors) Validate(parent *field.Path) field.ErrorList {
   108  	errs := field.ErrorList{}
   109  	current := parent.Child("stressors")
   110  	once := false
   111  	if in.MemoryStressor != nil {
   112  		errs = append(errs, in.MemoryStressor.Validate(current)...)
   113  		once = true
   114  	}
   115  	if in.CPUStressor != nil {
   116  		errs = append(errs, in.CPUStressor.Validate(current)...)
   117  		once = true
   118  	}
   119  	if !once {
   120  		errs = append(errs, field.Invalid(current, in, "missing stressors"))
   121  	}
   122  	return errs
   123  }
   124  
   125  // Validate validates whether the Stressor is well defined
   126  func (in *Stressor) Validate(parent *field.Path) field.ErrorList {
   127  	errs := field.ErrorList{}
   128  	if in.Workers <= 0 {
   129  		errs = append(errs, field.Invalid(parent, in, "workers should always be positive"))
   130  	}
   131  	return errs
   132  }
   133  
   134  // Validate validates whether the MemoryStressor is well defined
   135  func (in *MemoryStressor) Validate(parent *field.Path) field.ErrorList {
   136  	errs := field.ErrorList{}
   137  	current := parent.Child("vm")
   138  	errs = append(errs, in.Stressor.Validate(current)...)
   139  	if err := in.tryParseBytes(); err != nil {
   140  		errs = append(errs, field.Invalid(current, in,
   141  			fmt.Sprintf("incorrect bytes format: %s", err)))
   142  	}
   143  	return errs
   144  }
   145  
   146  func (in *MemoryStressor) tryParseBytes() error {
   147  	length := len(in.Size)
   148  	if length == 0 {
   149  		return nil
   150  	}
   151  	if in.Size[length-1] == '%' {
   152  		percent, err := strconv.Atoi(in.Size[:length-1])
   153  		if err != nil {
   154  			return err
   155  		}
   156  		if percent > 100 || percent < 0 {
   157  			return errors.New("illegal proportion")
   158  		}
   159  	} else {
   160  		size, err := units.FromHumanSize(in.Size)
   161  		if err != nil {
   162  			return err
   163  		}
   164  		in.Size = fmt.Sprintf("%db", size)
   165  	}
   166  	return nil
   167  }
   168  
   169  // Validate validates whether the CPUStressor is well defined
   170  func (in *CPUStressor) Validate(parent *field.Path) field.ErrorList {
   171  	errs := field.ErrorList{}
   172  	current := parent.Child("cpu")
   173  	errs = append(errs, in.Stressor.Validate(current)...)
   174  	if in.Load != nil && (*in.Load < 0 || *in.Load > 100) {
   175  		errs = append(errs, field.Invalid(current, in, "illegal proportion"))
   176  	}
   177  	return errs
   178  }
   179