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 "time"
22
23 "k8s.io/apimachinery/pkg/util/validation/field"
24 )
25
26 const DefaultJVMAgentPort int32 = 9277
27
28 func (in *JVMChaosSpec) Default(root interface{}, field *reflect.StructField) {
29 if in == nil {
30 return
31 }
32
33 if len(in.Name) == 0 {
34 in.Name = fmt.Sprintf("%s-%s-%s-%d", in.Class, in.Method, in.Action, time.Now().Unix())
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 JVMMySQLAction:
83 if len(in.MySQLConnectorVersion) == 0 {
84 allErrs = append(allErrs, field.Invalid(path, in, "MySQL connector version not provided"))
85 } else if in.MySQLConnectorVersion != "5" && in.MySQLConnectorVersion != "8" {
86 allErrs = append(allErrs, field.Invalid(path, in, "MySQL connector version only support 5.X.X(set to 5) and 8.X.X(set to 8)"))
87 }
88 if len(in.ThrowException) == 0 && in.LatencyDuration == 0 {
89 allErrs = append(allErrs, field.Invalid(path, in, "must set one of exception or latency"))
90 }
91 case "":
92 allErrs = append(allErrs, field.Invalid(path, in, "action not provided"))
93 default:
94 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)))
95 }
96
97 return allErrs
98 }
99