...
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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