...
1
2
3
4
5
6
7
8
9
10
11
12
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
30
31
32 type Validateable interface {
33 Validate(parent *field.Path) field.ErrorList
34 }
35
36
37 var (
38 stressChaosLog = ctrl.Log.WithName("stresschaos-resource")
39 )
40
41
42
43 var _ webhook.Defaulter = &StressChaos{}
44
45
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
57
58 var _ webhook.Validator = &StressChaos{}
59
60
61 func (in *StressChaos) ValidateCreate() error {
62 stressChaosLog.Info("validate create", "name", in.Name)
63 return in.Validate()
64 }
65
66
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
76 func (in *StressChaos) ValidateDelete() error {
77 stressChaosLog.Info("validate delete", "name", in.Name)
78
79
80 return nil
81 }
82
83
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
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
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
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
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
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