...
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 "time"
22
23 "k8s.io/apimachinery/pkg/util/validation/field"
24
25 "github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
26 )
27
28 type ClockIds []string
29
30
31 func (in *ClockIds) Default(root interface{}, field *reflect.StructField) {
32
33 if *in == nil || len(*in) == 0 {
34 *in = []string{"CLOCK_REALTIME"}
35 }
36 }
37
38 type TimeOffset string
39
40 func (in *TimeOffset) Validate(root interface{}, path *field.Path) field.ErrorList {
41 allErrs := field.ErrorList{}
42
43 _, err := time.ParseDuration(string(*in))
44 if err != nil {
45 allErrs = append(allErrs, field.Invalid(path,
46 in,
47 fmt.Sprintf("parse timeOffset field error:%s", err)))
48 }
49
50 return allErrs
51 }
52
53 func init() {
54 genericwebhook.Register("ClockIds", reflect.PtrTo(reflect.TypeOf(ClockIds{})))
55 genericwebhook.Register("TimeOffset", reflect.PtrTo(reflect.TypeOf(TimeOffset(""))))
56 }
57