...

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 2021 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package v1alpha1
    17  
    18  import (
    19  	"fmt"
    20  	"reflect"
    21  	"strconv"
    22  
    23  	"github.com/docker/go-units"
    24  	"github.com/pkg/errors"
    25  	"k8s.io/apimachinery/pkg/util/validation/field"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
    28  )
    29  
    30  // Validate validates the scheduler and duration
    31  func (in *StressChaosSpec) Validate(root interface{}, path *field.Path) field.ErrorList {
    32  	if len(in.StressngStressors) == 0 && in.Stressors == nil {
    33  		return field.ErrorList{
    34  			field.Invalid(path, in, "missing stressors"),
    35  		}
    36  	}
    37  	return nil
    38  }
    39  
    40  // Validate validates whether the Stressors are all well defined
    41  func (in *Stressors) Validate(root interface{}, path *field.Path) field.ErrorList {
    42  	if in == nil {
    43  		return nil
    44  	}
    45  
    46  	if in.MemoryStressor == nil && in.CPUStressor == nil {
    47  		return field.ErrorList{
    48  			field.Invalid(path, in, "missing stressors"),
    49  		}
    50  	}
    51  	return nil
    52  }
    53  
    54  type Bytes string
    55  
    56  func (in *Bytes) Validate(root interface{}, path *field.Path) field.ErrorList {
    57  	packError := func(err error) field.ErrorList {
    58  		return field.ErrorList{
    59  			field.Invalid(path, in, fmt.Sprintf("incorrect bytes format: %s", err.Error())),
    60  		}
    61  	}
    62  
    63  	// in cannot be nil
    64  	size := *in
    65  	length := len(size)
    66  	if length == 0 {
    67  		return nil
    68  	}
    69  
    70  	var err error
    71  	if size[length-1] == '%' {
    72  		var percent int
    73  		percent, err = strconv.Atoi(string(size)[:length-1])
    74  		if err != nil {
    75  			return packError(err)
    76  		}
    77  		if percent > 100 || percent < 0 {
    78  			err = errors.New("illegal proportion")
    79  			return packError(err)
    80  		}
    81  	} else {
    82  		_, err = units.FromHumanSize(string(size))
    83  		if err != nil {
    84  			return packError(err)
    85  		}
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  // Validate validates whether the Stressor is well defined
    92  func (in *Stressor) Validate(parent *field.Path) field.ErrorList {
    93  	errs := field.ErrorList{}
    94  	if in.Workers <= 0 {
    95  		errs = append(errs, field.Invalid(parent, in, "workers should always be positive"))
    96  	}
    97  	return errs
    98  }
    99  
   100  func init() {
   101  	genericwebhook.Register("Bytes", reflect.PtrTo(reflect.TypeOf(Bytes(""))))
   102  }
   103