...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 "reflect"
20
21 "github.com/pkg/errors"
22 "k8s.io/apimachinery/pkg/util/validation/field"
23
24 "github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
25 )
26
27 type EbsVolume string
28 type AWSDeviceName string
29
30 func (in *EbsVolume) Validate(root interface{}, path *field.Path) field.ErrorList {
31 allErrs := field.ErrorList{}
32
33 awsChaos := root.(*AWSChaos)
34 if awsChaos.Spec.Action == DetachVolume {
35 if in == nil {
36 err := errors.Wrapf(errInvalidValue, "the ID of EBS volume is required on %s action", awsChaos.Spec.Action)
37 allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
38 }
39 }
40
41 return allErrs
42 }
43
44 func (in *AWSDeviceName) Validate(root interface{}, path *field.Path) field.ErrorList {
45 allErrs := field.ErrorList{}
46
47 awsChaos := root.(*AWSChaos)
48 if awsChaos.Spec.Action == DetachVolume {
49 if in == nil {
50 err := errors.Wrapf(errInvalidValue, "the name of device is required on %s action", awsChaos.Spec.Action)
51 allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
52 }
53 }
54
55 return allErrs
56 }
57
58
59 func (in *AWSChaosAction) Validate(root interface{}, path *field.Path) field.ErrorList {
60 allErrs := field.ErrorList{}
61
62
63 switch *in {
64 case Ec2Stop, DetachVolume:
65 case Ec2Restart:
66 default:
67 err := errors.WithStack(errUnknownAction)
68 log.Error(err, "Wrong AWSChaos Action type")
69
70 allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
71 }
72 return allErrs
73 }
74
75 func init() {
76 genericwebhook.Register("EbsVolume", reflect.PtrTo(reflect.TypeOf(EbsVolume(""))))
77 genericwebhook.Register("AWSDeviceName", reflect.PtrTo(reflect.TypeOf(AWSDeviceName(""))))
78 }
79