...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 "fmt"
20 "net/http"
21 "reflect"
22 "time"
23
24 "k8s.io/apimachinery/pkg/util/validation/field"
25
26 "github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
27 )
28
29 type Delay string
30
31 func (in *Delay) Validate(root interface{}, path *field.Path) field.ErrorList {
32 allErrs := field.ErrorList{}
33 if in != nil {
34 _, err := time.ParseDuration(string(*in))
35 if err != nil {
36 allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("invalid duration %s", *in)))
37 }
38 }
39 return allErrs
40 }
41
42 type Port int32
43
44 func (in *Port) Validate(root interface{}, path *field.Path) field.ErrorList {
45 allErrs := field.ErrorList{}
46
47 if *in <= 0 {
48 allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("port %d is not supported", *in)))
49 }
50 return allErrs
51 }
52
53 type HTTPMethod string
54
55 func (in *HTTPMethod) Validate(root interface{}, path *field.Path) field.ErrorList {
56 allErrs := field.ErrorList{}
57 if in != nil && root.(*HTTPChaos).Spec.Target == PodHttpRequest {
58 switch *in {
59 case http.MethodGet:
60 case http.MethodPost:
61 case http.MethodPut:
62 case http.MethodDelete:
63 case http.MethodPatch:
64 case http.MethodHead:
65 case http.MethodOptions:
66 case http.MethodTrace:
67 case http.MethodConnect:
68 default:
69 allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("method %s is not supported", *in)))
70 }
71 }
72 return allErrs
73 }
74
75 func (in *PodHttpChaosTarget) Validate(root interface{}, path *field.Path) field.ErrorList {
76 allErrs := field.ErrorList{}
77 switch *in {
78 case PodHttpRequest:
79 case PodHttpResponse:
80 default:
81 allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("target %s is not supported", *in)))
82 }
83 return allErrs
84 }
85
86 func init() {
87 genericwebhook.Register("Delay", reflect.PtrTo(reflect.TypeOf(Delay(""))))
88 genericwebhook.Register("Port", reflect.PtrTo(reflect.TypeOf(Port(0))))
89 genericwebhook.Register("HTTPMethod", reflect.PtrTo(reflect.TypeOf(HTTPMethod(""))))
90 genericwebhook.Register("PodHttpChaosTarget", reflect.PtrTo(reflect.TypeOf(PodHttpChaosTarget(""))))
91 }
92