@@ -55,7 +55,7 @@ This markers is responsible for generating a mutating webhook manifest.
55
55
The meaning of each marker can be found [here](../TODO.md).
56
56
*/
57
57
58
- // +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-tutorial-kubebuilder-io- v1-cronjob,mutating=true,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io .tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io
58
+ // +kubebuilder:webhook:path=/mutate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=true,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=mcronjob.kb.io
59
59
60
60
/*
61
61
We use the `webhook.Defaulter` interface to set defaults to our CRD.
@@ -93,7 +93,7 @@ This markers is responsible for generating a validating webhook manifest.
93
93
The meaning of each marker can be found [here](../TODO.md).
94
94
*/
95
95
96
- // +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-tutorial-kubebuilder-io- v1-cronjob,mutating=false,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io .tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob.kb.io
96
+ // +kubebuilder:webhook:path=/validate-batch-tutorial-kubebuilder-io-v1-cronjob,mutating=false,failurePolicy=fail,groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=create;update,versions=v1,name=vcronjob.kb.io
97
97
98
98
/*
99
99
To validate our CRD beyond what's possible with declarative validation.
@@ -133,15 +133,21 @@ func (r *CronJob) ValidateUpdate(old runtime.Object) error {
133
133
We validate the name and the spec of the CronJob.
134
134
*/
135
135
136
- func (c * CronJob ) validateCronJob () error {
137
- allErrs := c . validateCronJobSpec ()
138
- if err := c .validateCronJobName (); err != nil {
136
+ func (r * CronJob ) validateCronJob () error {
137
+ var allErrs field. ErrorList
138
+ if err := r .validateCronJobName (); err != nil {
139
139
allErrs = append (allErrs , err )
140
140
}
141
+ if err := r .validateCronJobSpec (); err != nil {
142
+ allErrs = append (allErrs , err )
143
+ }
144
+ if len (allErrs ) == 0 {
145
+ return nil
146
+ }
141
147
142
148
return apierrors .NewInvalid (
143
149
schema.GroupKind {Group : "batch.tutorial.kubebuilder.io" , Kind : "CronJob" },
144
- c .Name , allErrs )
150
+ r .Name , allErrs )
145
151
}
146
152
147
153
/*
@@ -152,14 +158,24 @@ You can find all of the kubebuilder supported markers for
152
158
declaring validation in [here](../TODO.md).
153
159
*/
154
160
155
- func (c * CronJob ) validateCronJobSpec () field.ErrorList {
161
+ func (r * CronJob ) validateCronJobSpec () * field.Error {
156
162
// The field helpers from the kubernetes API machinery help us return nicely
157
163
// structured validation errors.
158
- allErrs := field.ErrorList {}
159
- allErrs = append (allErrs , validateScheduleFormat (
160
- c .Spec .Schedule ,
161
- field .NewPath ("spec" ).Child ("schedule" ))... )
162
- return allErrs
164
+ return validateScheduleFormat (
165
+ r .Spec .Schedule ,
166
+ field .NewPath ("spec" ).Child ("schedule" ))
167
+ }
168
+
169
+ /*
170
+ We'll need to validate the [cron](https://en.wikipedia.org/wiki/Cron) schedule
171
+ is well-formatted.
172
+ */
173
+
174
+ func validateScheduleFormat (schedule string , fldPath * field.Path ) * field.Error {
175
+ if _ , err := cron .ParseStandard (schedule ); err != nil {
176
+ return field .Invalid (fldPath , schedule , err .Error ())
177
+ }
178
+ return nil
163
179
}
164
180
165
181
/*
@@ -171,31 +187,17 @@ the apimachinery repo, so we can't declaratively validate it using
171
187
the validation schema.
172
188
*/
173
189
174
- func (c * CronJob ) validateCronJobName () * field.Error {
175
- if len (c .ObjectMeta .Name ) > validationutils .DNS1035LabelMaxLength - 11 {
190
+ func (r * CronJob ) validateCronJobName () * field.Error {
191
+ if len (r .ObjectMeta .Name ) > validationutils .DNS1035LabelMaxLength - 11 {
176
192
// The job name length is 63 character like all Kubernetes objects
177
193
// (which must fit in a DNS subdomain). The cronjob controller appends
178
194
// a 11-character suffix to the cronjob (`-$TIMESTAMP`) when creating
179
195
// a job. The job name length limit is 63 characters. Therefore cronjob
180
196
// names must have length <= 63-11=52. If we don't validate this here,
181
197
// then job creation will fail later.
182
- return field .Invalid (field .NewPath ("metadata" ).Child ("name" ), c .Name , "must be no more than 52 characters" )
198
+ return field .Invalid (field .NewPath ("metadata" ).Child ("name" ), r .Name , "must be no more than 52 characters" )
183
199
}
184
200
return nil
185
201
}
186
202
187
203
// +kubebuilder:docs-gen:collapse=Validate object name
188
-
189
- /*
190
- We'll need to validate the [cron](https://en.wikipedia.org/wiki/Cron) schedule
191
- is well-formatted.
192
- */
193
-
194
- func validateScheduleFormat (schedule string , fldPath * field.Path ) field.ErrorList {
195
- allErrs := field.ErrorList {}
196
- if _ , err := cron .ParseStandard (schedule ); err != nil {
197
- allErrs = append (allErrs , field .Invalid (fldPath , schedule , err .Error ()))
198
- }
199
-
200
- return allErrs
201
- }
0 commit comments