@@ -31,6 +31,8 @@ import (
31
31
logf "sigs.k8s.io/controller-runtime/pkg/log"
32
32
"sigs.k8s.io/controller-runtime/pkg/webhook"
33
33
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
34
+
35
+ batchv1 "tutorial.kubebuilder.io/project/api/v1"
34
36
)
35
37
36
38
// +kubebuilder:docs-gen:collapse=Go imports
@@ -45,13 +47,12 @@ var cronjoblog = logf.Log.WithName("cronjob-resource")
45
47
Then, we set up the webhook with the manager.
46
48
*/
47
49
48
- // SetupWebhookWithManager will setup the manager to manage the webhooks.
49
- func (r * CronJob ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
50
- return ctrl .NewWebhookManagedBy (mgr ).
51
- For (r ).
50
+ // SetupCronJobWebhookWithManager registers the webhook for CronJob in the manager.
51
+ func SetupCronJobWebhookWithManager (mgr ctrl.Manager ) error {
52
+ return ctrl .NewWebhookManagedBy (mgr ).For (& batchv1.CronJob {}).
52
53
WithValidator (& CronJobCustomValidator {}).
53
54
WithDefaulter (& CronJobCustomDefaulter {
54
- DefaultConcurrencyPolicy : AllowConcurrent ,
55
+ DefaultConcurrencyPolicy : batchv1 . AllowConcurrent ,
55
56
DefaultSuspend : false ,
56
57
DefaultSuccessfulJobsHistoryLimit : 3 ,
57
58
DefaultFailedJobsHistoryLimit : 1 ,
@@ -81,7 +82,7 @@ This marker is responsible for generating a mutation webhook manifest.
81
82
type CronJobCustomDefaulter struct {
82
83
83
84
// Default values for various CronJob fields
84
- DefaultConcurrencyPolicy ConcurrencyPolicy
85
+ DefaultConcurrencyPolicy batchv1. ConcurrencyPolicy
85
86
DefaultSuspend bool
86
87
DefaultSuccessfulJobsHistoryLimit int32
87
88
DefaultFailedJobsHistoryLimit int32
@@ -98,32 +99,34 @@ The `Default`method is expected to mutate the receiver, setting the defaults.
98
99
99
100
// Default implements webhook.CustomDefaulter so a webhook will be registered for the Kind CronJob.
100
101
func (d * CronJobCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
101
- cronjob , ok := obj .(* CronJob )
102
+ cronjob , ok := obj .(* batchv1.CronJob )
103
+
102
104
if ! ok {
103
105
return fmt .Errorf ("expected an CronJob object but got %T" , obj )
104
106
}
105
107
cronjoblog .Info ("Defaulting for CronJob" , "name" , cronjob .GetName ())
106
108
107
109
// Set default values
108
- cronjob .Default ()
109
-
110
+ d .applyDefaults (cronjob )
110
111
return nil
111
112
}
112
113
113
- func (r * CronJob ) Default () {
114
- if r .Spec .ConcurrencyPolicy == "" {
115
- r .Spec .ConcurrencyPolicy = AllowConcurrent
114
+ // applyDefaults applies default values to CronJob fields.
115
+ func (d * CronJobCustomDefaulter ) applyDefaults (cronJob * batchv1.CronJob ) {
116
+ if cronJob .Spec .ConcurrencyPolicy == "" {
117
+ cronJob .Spec .ConcurrencyPolicy = d .DefaultConcurrencyPolicy
116
118
}
117
- if r .Spec .Suspend == nil {
118
- r .Spec .Suspend = new (bool )
119
+ if cronJob .Spec .Suspend == nil {
120
+ cronJob .Spec .Suspend = new (bool )
121
+ * cronJob .Spec .Suspend = d .DefaultSuspend
119
122
}
120
- if r .Spec .SuccessfulJobsHistoryLimit == nil {
121
- r .Spec .SuccessfulJobsHistoryLimit = new (int32 )
122
- * r .Spec .SuccessfulJobsHistoryLimit = 3
123
+ if cronJob .Spec .SuccessfulJobsHistoryLimit == nil {
124
+ cronJob .Spec .SuccessfulJobsHistoryLimit = new (int32 )
125
+ * cronJob .Spec .SuccessfulJobsHistoryLimit = d . DefaultSuccessfulJobsHistoryLimit
123
126
}
124
- if r .Spec .FailedJobsHistoryLimit == nil {
125
- r .Spec .FailedJobsHistoryLimit = new (int32 )
126
- * r .Spec .FailedJobsHistoryLimit = 1
127
+ if cronJob .Spec .FailedJobsHistoryLimit == nil {
128
+ cronJob .Spec .FailedJobsHistoryLimit = new (int32 )
129
+ * cronJob .Spec .FailedJobsHistoryLimit = d . DefaultFailedJobsHistoryLimit
127
130
}
128
131
}
129
132
@@ -168,29 +171,29 @@ var _ webhook.CustomValidator = &CronJobCustomValidator{}
168
171
169
172
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type CronJob.
170
173
func (v * CronJobCustomValidator ) ValidateCreate (ctx context.Context , obj runtime.Object ) (admission.Warnings , error ) {
171
- cronjob , ok := obj .(* CronJob )
174
+ cronjob , ok := obj .(* batchv1. CronJob )
172
175
if ! ok {
173
176
return nil , fmt .Errorf ("expected a CronJob object but got %T" , obj )
174
177
}
175
178
cronjoblog .Info ("Validation for CronJob upon creation" , "name" , cronjob .GetName ())
176
179
177
- return nil , cronjob . validateCronJob ()
180
+ return nil , validateCronJob (cronjob )
178
181
}
179
182
180
183
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type CronJob.
181
184
func (v * CronJobCustomValidator ) ValidateUpdate (ctx context.Context , oldObj , newObj runtime.Object ) (admission.Warnings , error ) {
182
- cronjob , ok := newObj .(* CronJob )
185
+ cronjob , ok := newObj .(* batchv1. CronJob )
183
186
if ! ok {
184
- return nil , fmt .Errorf ("expected a CronJob object but got %T" , newObj )
187
+ return nil , fmt .Errorf ("expected a CronJob object for the newObj but got %T" , newObj )
185
188
}
186
189
cronjoblog .Info ("Validation for CronJob upon update" , "name" , cronjob .GetName ())
187
190
188
- return nil , cronjob . validateCronJob ()
191
+ return nil , validateCronJob (cronjob )
189
192
}
190
193
191
194
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type CronJob.
192
195
func (v * CronJobCustomValidator ) ValidateDelete (ctx context.Context , obj runtime.Object ) (admission.Warnings , error ) {
193
- cronjob , ok := obj .(* CronJob )
196
+ cronjob , ok := obj .(* batchv1. CronJob )
194
197
if ! ok {
195
198
return nil , fmt .Errorf ("expected a CronJob object but got %T" , obj )
196
199
}
@@ -205,12 +208,13 @@ func (v *CronJobCustomValidator) ValidateDelete(ctx context.Context, obj runtime
205
208
We validate the name and the spec of the CronJob.
206
209
*/
207
210
208
- func (r * CronJob ) validateCronJob () error {
211
+ // validateCronJob validates the fields of a CronJob object.
212
+ func validateCronJob (cronjob * batchv1.CronJob ) error {
209
213
var allErrs field.ErrorList
210
- if err := r . validateCronJobName (); err != nil {
214
+ if err := validateCronJobName (cronjob ); err != nil {
211
215
allErrs = append (allErrs , err )
212
216
}
213
- if err := r . validateCronJobSpec (); err != nil {
217
+ if err := validateCronJobSpec (cronjob ); err != nil {
214
218
allErrs = append (allErrs , err )
215
219
}
216
220
if len (allErrs ) == 0 {
@@ -219,7 +223,7 @@ func (r *CronJob) validateCronJob() error {
219
223
220
224
return apierrors .NewInvalid (
221
225
schema.GroupKind {Group : "batch.tutorial.kubebuilder.io" , Kind : "CronJob" },
222
- r .Name , allErrs )
226
+ cronjob .Name , allErrs )
223
227
}
224
228
225
229
/*
@@ -232,11 +236,11 @@ declaring validation by running `controller-gen crd -w`,
232
236
or [here](/reference/markers/crd-validation.md).
233
237
*/
234
238
235
- func ( r * CronJob ) validateCronJobSpec ( ) * field.Error {
239
+ func validateCronJobSpec ( cronjob * batchv1. CronJob ) * field.Error {
236
240
// The field helpers from the kubernetes API machinery help us return nicely
237
241
// structured validation errors.
238
242
return validateScheduleFormat (
239
- r .Spec .Schedule ,
243
+ cronjob .Spec .Schedule ,
240
244
field .NewPath ("spec" ).Child ("schedule" ))
241
245
}
242
246
@@ -261,15 +265,15 @@ the apimachinery repo, so we can't declaratively validate it using
261
265
the validation schema.
262
266
*/
263
267
264
- func ( r * CronJob ) validateCronJobName ( ) * field.Error {
265
- if len (r .ObjectMeta .Name ) > validationutils .DNS1035LabelMaxLength - 11 {
268
+ func validateCronJobName ( cronjob * batchv1. CronJob ) * field.Error {
269
+ if len (cronjob .ObjectMeta .Name ) > validationutils .DNS1035LabelMaxLength - 11 {
266
270
// The job name length is 63 characters like all Kubernetes objects
267
271
// (which must fit in a DNS subdomain). The cronjob controller appends
268
272
// a 11-character suffix to the cronjob (`-$TIMESTAMP`) when creating
269
273
// a job. The job name length limit is 63 characters. Therefore cronjob
270
274
// names must have length <= 63-11=52. If we don't validate this here,
271
275
// then job creation will fail later.
272
- return field .Invalid (field .NewPath ("metadata" ).Child ("name" ), r .ObjectMeta .Name , "must be no more than 52 characters" )
276
+ return field .Invalid (field .NewPath ("metadata" ).Child ("name" ), cronjob .ObjectMeta .Name , "must be no more than 52 characters" )
273
277
}
274
278
return nil
275
279
}
0 commit comments