diff --git a/pkg/apis/batch/fuzzer/fuzzer.go b/pkg/apis/batch/fuzzer/fuzzer.go index 07a8cc1ed59..4b0a8b9064d 100644 --- a/pkg/apis/batch/fuzzer/fuzzer.go +++ b/pkg/apis/batch/fuzzer/fuzzer.go @@ -53,14 +53,10 @@ var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} { sds := int64(c.RandUint64()) sj.StartingDeadlineSeconds = &sds sj.Schedule = c.RandString() - if hasSuccessLimit := c.RandBool(); hasSuccessLimit { - successfulJobsHistoryLimit := int32(c.Rand.Int31()) - sj.SuccessfulJobsHistoryLimit = &successfulJobsHistoryLimit - } - if hasFailedLimit := c.RandBool(); hasFailedLimit { - failedJobsHistoryLimit := int32(c.Rand.Int31()) - sj.FailedJobsHistoryLimit = &failedJobsHistoryLimit - } + successfulJobsHistoryLimit := int32(c.Rand.Int31()) + sj.SuccessfulJobsHistoryLimit = &successfulJobsHistoryLimit + failedJobsHistoryLimit := int32(c.Rand.Int31()) + sj.FailedJobsHistoryLimit = &failedJobsHistoryLimit }, func(cp *batch.ConcurrencyPolicy, c fuzz.Continue) { policies := []batch.ConcurrencyPolicy{batch.AllowConcurrent, batch.ForbidConcurrent, batch.ReplaceConcurrent} diff --git a/pkg/apis/batch/v1beta1/defaults.go b/pkg/apis/batch/v1beta1/defaults.go index 9ae68472480..58fee5f5834 100644 --- a/pkg/apis/batch/v1beta1/defaults.go +++ b/pkg/apis/batch/v1beta1/defaults.go @@ -32,4 +32,12 @@ func SetDefaults_CronJob(obj *batchv1beta1.CronJob) { if obj.Spec.Suspend == nil { obj.Spec.Suspend = new(bool) } + if obj.Spec.SuccessfulJobsHistoryLimit == nil { + obj.Spec.SuccessfulJobsHistoryLimit = new(int32) + *obj.Spec.SuccessfulJobsHistoryLimit = 3 + } + if obj.Spec.FailedJobsHistoryLimit == nil { + obj.Spec.FailedJobsHistoryLimit = new(int32) + *obj.Spec.FailedJobsHistoryLimit = 1 + } } diff --git a/pkg/apis/batch/v1beta1/defaults_test.go b/pkg/apis/batch/v1beta1/defaults_test.go index 8c23ae19222..1f11b0566df 100644 --- a/pkg/apis/batch/v1beta1/defaults_test.go +++ b/pkg/apis/batch/v1beta1/defaults_test.go @@ -38,22 +38,28 @@ func TestSetDefaultCronJob(t *testing.T) { original: &batchv1beta1.CronJob{}, expected: &batchv1beta1.CronJob{ Spec: batchv1beta1.CronJobSpec{ - ConcurrencyPolicy: batchv1beta1.AllowConcurrent, - Suspend: newBool(false), + ConcurrencyPolicy: batchv1beta1.AllowConcurrent, + Suspend: newBool(false), + SuccessfulJobsHistoryLimit: newInt32(3), + FailedJobsHistoryLimit: newInt32(1), }, }, }, "set fields should not be defaulted": { original: &batchv1beta1.CronJob{ Spec: batchv1beta1.CronJobSpec{ - ConcurrencyPolicy: batchv1beta1.ForbidConcurrent, - Suspend: newBool(true), + ConcurrencyPolicy: batchv1beta1.ForbidConcurrent, + Suspend: newBool(true), + SuccessfulJobsHistoryLimit: newInt32(5), + FailedJobsHistoryLimit: newInt32(5), }, }, expected: &batchv1beta1.CronJob{ Spec: batchv1beta1.CronJobSpec{ - ConcurrencyPolicy: batchv1beta1.ForbidConcurrent, - Suspend: newBool(true), + ConcurrencyPolicy: batchv1beta1.ForbidConcurrent, + Suspend: newBool(true), + SuccessfulJobsHistoryLimit: newInt32(5), + FailedJobsHistoryLimit: newInt32(5), }, }, }, @@ -74,6 +80,12 @@ func TestSetDefaultCronJob(t *testing.T) { if *actual.Spec.Suspend != *expected.Spec.Suspend { t.Errorf("%s: got different suspend than expected: %v %v", name, *actual.Spec.Suspend, *expected.Spec.Suspend) } + if *actual.Spec.SuccessfulJobsHistoryLimit != *expected.Spec.SuccessfulJobsHistoryLimit { + t.Errorf("%s: got different successfulJobsHistoryLimit than expected: %v %v", name, *actual.Spec.SuccessfulJobsHistoryLimit, *expected.Spec.SuccessfulJobsHistoryLimit) + } + if *actual.Spec.FailedJobsHistoryLimit != *expected.Spec.FailedJobsHistoryLimit { + t.Errorf("%s: got different failedJobsHistoryLimit than expected: %v %v", name, *actual.Spec.FailedJobsHistoryLimit, *expected.Spec.FailedJobsHistoryLimit) + } } } @@ -102,3 +114,9 @@ func newBool(val bool) *bool { *p = val return p } + +func newInt32(val int32) *int32 { + p := new(int32) + *p = val + return p +} diff --git a/staging/src/k8s.io/api/batch/v1beta1/types.go b/staging/src/k8s.io/api/batch/v1beta1/types.go index 43e447473d5..ad13bba2f92 100644 --- a/staging/src/k8s.io/api/batch/v1beta1/types.go +++ b/staging/src/k8s.io/api/batch/v1beta1/types.go @@ -114,11 +114,13 @@ type CronJobSpec struct { // The number of successful finished jobs to retain. // This is a pointer to distinguish between explicit zero and not specified. + // Defaults to 3. // +optional SuccessfulJobsHistoryLimit *int32 `json:"successfulJobsHistoryLimit,omitempty" protobuf:"varint,6,opt,name=successfulJobsHistoryLimit"` // The number of failed finished jobs to retain. // This is a pointer to distinguish between explicit zero and not specified. + // Defaults to 1. // +optional FailedJobsHistoryLimit *int32 `json:"failedJobsHistoryLimit,omitempty" protobuf:"varint,7,opt,name=failedJobsHistoryLimit"` }