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