...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 "fmt"
20
21 "k8s.io/apimachinery/pkg/runtime"
22 "k8s.io/apimachinery/pkg/util/validation/field"
23 logf "sigs.k8s.io/controller-runtime/pkg/log"
24 "sigs.k8s.io/controller-runtime/pkg/webhook"
25 )
26
27
28 var schedulelog = logf.Log.WithName("schedule-resource")
29
30
31
32 var _ webhook.Defaulter = &Schedule{}
33
34
35 func (in *Schedule) Default() {
36 schedulelog.Info("default", "name", in.Name)
37 }
38
39
40
41 var _ webhook.Validator = &Schedule{}
42
43
44 func (in *Schedule) ValidateCreate() error {
45 schedulelog.Info("validate create", "name", in.Name)
46 return in.Validate()
47 }
48
49
50 func (in *Schedule) ValidateUpdate(old runtime.Object) error {
51 schedulelog.Info("validate update", "name", in.Name)
52 return in.Validate()
53 }
54
55
56 func (in *Schedule) ValidateDelete() error {
57 schedulelog.Info("validate delete", "name", in.Name)
58 return nil
59 }
60
61
62 func (in *Schedule) Validate() error {
63 allErrs := in.Spec.Validate()
64 if len(allErrs) > 0 {
65 return fmt.Errorf(allErrs.ToAggregate().Error())
66 }
67 return nil
68 }
69
70 func (in *ScheduleSpec) Validate() field.ErrorList {
71 specField := field.NewPath("spec")
72 allErrs := field.ErrorList{}
73 allErrs = append(allErrs, in.validateSchedule(specField.Child("schedule"))...)
74 allErrs = append(allErrs, in.validateChaos(specField)...)
75 return allErrs
76 }
77
78
79 func (in *ScheduleSpec) validateSchedule(schedule *field.Path) field.ErrorList {
80 allErrs := field.ErrorList{}
81 _, err := StandardCronParser.Parse(in.Schedule)
82 if err != nil {
83 allErrs = append(allErrs, field.Invalid(schedule,
84 in.Schedule,
85 fmt.Sprintf("parse schedule field error:%s", err)))
86 }
87
88 return allErrs
89 }
90
91
92 func (in *ScheduleSpec) validateChaos(chaos *field.Path) field.ErrorList {
93 allErrs := field.ErrorList{}
94 if in.Type != ScheduleTypeWorkflow {
95 allErrs = append(allErrs, in.EmbedChaos.Validate(chaos, string(in.Type))...)
96 }
97 return allErrs
98 }
99