...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package v1alpha1
15
16 import (
17 "fmt"
18 "reflect"
19
20 "k8s.io/apimachinery/pkg/runtime"
21 "k8s.io/apimachinery/pkg/util/validation/field"
22 logf "sigs.k8s.io/controller-runtime/pkg/log"
23 "sigs.k8s.io/controller-runtime/pkg/webhook"
24 )
25
26
27 var httpchaoslog = logf.Log.WithName("httpchaos-resource")
28
29
30
31 var _ webhook.Defaulter = &HTTPChaos{}
32
33
34 func (in *HTTPChaos) Default() {
35 httpchaoslog.Info("default", "name", in.Name)
36
37 in.Spec.Selector.DefaultNamespace(in.GetNamespace())
38 in.Spec.Default()
39 }
40
41 func (in *HTTPChaosSpec) Default() {
42
43 }
44
45
46
47 var _ webhook.Validator = &HTTPChaos{}
48
49
50 func (in *HTTPChaos) ValidateCreate() error {
51 httpchaoslog.Info("validate create", "name", in.Name)
52 return in.Validate()
53 }
54
55
56 func (in *HTTPChaos) ValidateUpdate(old runtime.Object) error {
57 httpchaoslog.Info("validate update", "name", in.Name)
58 if !reflect.DeepEqual(in.Spec, old.(*HTTPChaos).Spec) {
59 return ErrCanNotUpdateChaos
60 }
61 return in.Validate()
62 }
63
64
65 func (in *HTTPChaos) ValidateDelete() error {
66 httpchaoslog.Info("validate delete", "name", in.Name)
67
68
69 return nil
70 }
71
72
73 func (in *HTTPChaos) Validate() error {
74
75 allErrs := in.Spec.Validate()
76 if len(allErrs) > 0 {
77 return fmt.Errorf(allErrs.ToAggregate().Error())
78 }
79
80 return nil
81 }
82
83 func (in *HTTPChaosSpec) Validate() field.ErrorList {
84 specField := field.NewPath("spec")
85
86 allErrs := validatePodSelector(in.PodSelector.Value, in.PodSelector.Mode, specField.Child("value"))
87 allErrs = append(allErrs, validateDuration(in, specField)...)
88 return allErrs
89
90 }
91