mirror of https://github.com/k3s-io/k3s
Limit 52-character cronjob name validation to create
parent
5f8726e427
commit
b8155e107c
|
@ -176,6 +176,14 @@ func ValidateCronJob(scheduledJob *batch.CronJob) field.ErrorList {
|
|||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateCronJobUpdate(job, oldJob *batch.CronJob) field.ErrorList {
|
||||
allErrs := apivalidation.ValidateObjectMetaUpdate(&job.ObjectMeta, &oldJob.ObjectMeta, field.NewPath("metadata"))
|
||||
allErrs = append(allErrs, ValidateCronJobSpec(&job.Spec, field.NewPath("spec"))...)
|
||||
// skip the 52-character name validation limit on update validation
|
||||
// to allow old cronjobs with names > 52 chars to be updated/deleted
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateCronJobSpec(spec *batch.CronJobSpec, fldPath *field.Path) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
|
||||
|
|
|
@ -347,6 +347,14 @@ func TestValidateCronJob(t *testing.T) {
|
|||
if errs := ValidateCronJob(&v); len(errs) != 0 {
|
||||
t.Errorf("expected success for %s: %v", k, errs)
|
||||
}
|
||||
|
||||
// Update validation should pass same success cases
|
||||
// copy to avoid polluting the testcase object, set a resourceVersion to allow validating update, and test a no-op update
|
||||
v = *v.DeepCopy()
|
||||
v.ResourceVersion = "1"
|
||||
if errs := ValidateCronJobUpdate(&v, &v); len(errs) != 0 {
|
||||
t.Errorf("expected success for %s: %v", k, errs)
|
||||
}
|
||||
}
|
||||
|
||||
negative := int32(-1)
|
||||
|
@ -588,6 +596,24 @@ func TestValidateCronJob(t *testing.T) {
|
|||
t.Errorf("unexpected error: %v, expected: %s", err, k)
|
||||
}
|
||||
}
|
||||
|
||||
// Update validation should fail all failure cases other than the 52 character name limit
|
||||
// copy to avoid polluting the testcase object, set a resourceVersion to allow validating update, and test a no-op update
|
||||
v = *v.DeepCopy()
|
||||
v.ResourceVersion = "1"
|
||||
errs = ValidateCronJobUpdate(&v, &v)
|
||||
if len(errs) == 0 {
|
||||
if k == "metadata.name: must be no more than 52 characters" {
|
||||
continue
|
||||
}
|
||||
t.Errorf("expected failure for %s", k)
|
||||
} else {
|
||||
s := strings.Split(k, ":")
|
||||
err := errs[0]
|
||||
if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) {
|
||||
t.Errorf("unexpected error: %v, expected: %s", err, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ func (cronJobStrategy) AllowCreateOnUpdate() bool {
|
|||
|
||||
// ValidateUpdate is the default update validation for an end user.
|
||||
func (cronJobStrategy) ValidateUpdate(ctx genericapirequest.Context, obj, old runtime.Object) field.ErrorList {
|
||||
return validation.ValidateCronJob(obj.(*batch.CronJob))
|
||||
return validation.ValidateCronJobUpdate(obj.(*batch.CronJob), old.(*batch.CronJob))
|
||||
}
|
||||
|
||||
type cronJobStatusStrategy struct {
|
||||
|
|
Loading…
Reference in New Issue