...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/jvmchaos_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  
    22  	"k8s.io/apimachinery/pkg/util/validation/field"
    23  )
    24  
    25  const DefaultJVMAgentPort int32 = 9277
    26  
    27  func (in *JVMChaosSpec) Default(root interface{}, field *reflect.StructField) {
    28  	if in == nil {
    29  		return
    30  	}
    31  
    32  	jvmChaos := root.(*JVMChaos)
    33  	if len(in.Name) == 0 {
    34  		in.Name = jvmChaos.Name
    35  	}
    36  
    37  	if in.Port == 0 {
    38  		in.Port = DefaultJVMAgentPort
    39  	}
    40  }
    41  
    42  func (in *JVMChaosSpec) Validate(root interface{}, path *field.Path) field.ErrorList {
    43  	allErrs := field.ErrorList{}
    44  
    45  	switch in.Action {
    46  	case JVMStressAction:
    47  		if in.CPUCount == 0 && len(in.MemoryType) == 0 {
    48  			allErrs = append(allErrs, field.Invalid(path, in, "must set one of cpu-count and mem-type when action is 'stress'"))
    49  		}
    50  
    51  		if in.CPUCount > 0 && len(in.MemoryType) > 0 {
    52  			allErrs = append(allErrs, field.Invalid(path, in, "inject stress on both CPU and memory is not support now"))
    53  		}
    54  
    55  		if len(in.MemoryType) != 0 {
    56  			if in.MemoryType != "stack" && in.MemoryType != "heap" {
    57  				allErrs = append(allErrs, field.Invalid(path, in, "value should be 'stack' or 'heap'"))
    58  			}
    59  		}
    60  	case JVMGCAction:
    61  		// do nothing
    62  	case JVMExceptionAction, JVMReturnAction, JVMLatencyAction:
    63  		if len(in.Class) == 0 {
    64  			allErrs = append(allErrs, field.Invalid(path, in, "class not provided"))
    65  		}
    66  
    67  		if len(in.Method) == 0 {
    68  			allErrs = append(allErrs, field.Invalid(path, in, "method not provided"))
    69  		}
    70  		if in.Action == JVMExceptionAction && len(in.ThrowException) == 0 {
    71  			allErrs = append(allErrs, field.Invalid(path, in, "exception not provided"))
    72  		} else if in.Action == JVMReturnAction && len(in.ReturnValue) == 0 {
    73  			allErrs = append(allErrs, field.Invalid(path, in, "value not provided"))
    74  		} else if in.Action == JVMLatencyAction && in.LatencyDuration == 0 {
    75  			allErrs = append(allErrs, field.Invalid(path, in, "latency not provided"))
    76  		}
    77  
    78  	case JVMRuleDataAction:
    79  		if len(in.RuleData) == 0 {
    80  			allErrs = append(allErrs, field.Invalid(path, in, "rule data not provide"))
    81  		}
    82  	case "":
    83  		allErrs = append(allErrs, field.Invalid(path, in, "action not provided"))
    84  	default:
    85  		allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("action %s not supported, action can be 'latency', 'exception', 'return', 'stress', 'gc' or 'ruleData'", in.Action)))
    86  	}
    87  
    88  	return allErrs
    89  }
    90