...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 "fmt"
20 "reflect"
21
22 "k8s.io/apimachinery/pkg/util/validation/field"
23
24 "github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
25 )
26
27 type DiskName string
28 type LUN int
29
30 func (in *DiskName) Validate(root interface{}, path *field.Path) field.ErrorList {
31 allErrs := field.ErrorList{}
32
33 azurechaos := root.(*AzureChaos)
34 if azurechaos.Spec.Action == AzureDiskDetach {
35 if in == nil {
36 err := fmt.Errorf("the name of data disk should not be empty on %s action", azurechaos.Spec.Action)
37 allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
38 }
39 }
40
41 return allErrs
42 }
43
44 func (in *LUN) Validate(root interface{}, path *field.Path) field.ErrorList {
45 allErrs := field.ErrorList{}
46
47 azurechaos := root.(*AzureChaos)
48 if azurechaos.Spec.Action == AzureDiskDetach {
49 if in == nil {
50 err := fmt.Errorf("the LUN of data disk should not be empty on %s action", azurechaos.Spec.Action)
51 allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
52 }
53 }
54
55 return allErrs
56 }
57
58
59 func (in *AzureChaosAction) Validate(root interface{}, path *field.Path) field.ErrorList {
60 allErrs := field.ErrorList{}
61
62
63 switch *in {
64 case AzureVmStop, AzureDiskDetach:
65 case AzureVmRestart:
66 default:
67 err := fmt.Errorf("azurechaos have unknown action type")
68 log.Error(err, "Wrong AzureChaos 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("DiskName", reflect.PtrTo(reflect.TypeOf(DiskName(""))))
77 genericwebhook.Register("LUN", reflect.PtrTo(reflect.TypeOf(LUN(0))))
78 }
79