...
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
28 func (in GCPChaosAction) Validate(root interface{}, path *field.Path) field.ErrorList {
29 allErrs := field.ErrorList{}
30
31 switch in {
32 case NodeStop, DiskLoss:
33 case NodeReset:
34 default:
35 err := errors.WithStack(errUnknownAction)
36 log.Error(err, "Wrong GCPChaos Action type")
37
38 allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
39 }
40 return allErrs
41 }
42
43 type GCPDeviceNames []string
44
45
46 func (in *GCPDeviceNames) Validate(root interface{}, path *field.Path) field.ErrorList {
47 allErrs := field.ErrorList{}
48 obj := root.(*GCPChaos)
49 if obj.Spec.Action == DiskLoss {
50 if *in == nil {
51 err := errors.Errorf("at least one device name is required on %s action", obj.Spec.Action)
52 allErrs = append(allErrs, field.Invalid(path, *in, err.Error()))
53 }
54 }
55 return allErrs
56 }
57
58 func init() {
59 genericwebhook.Register("GCPDeviceNames", reflect.PtrTo(reflect.TypeOf(GCPDeviceNames{})))
60 }
61