Promote CronJobs to batch/v1

This commit is contained in:
Maciej Szulik
2021-02-10 12:49:43 +01:00
parent 22b3a9cd21
commit 9364d0fc81
10 changed files with 232 additions and 12 deletions

View File

@@ -32,14 +32,13 @@ func addConversionFuncs(scheme *runtime.Scheme) error {
case "metadata.name", "metadata.namespace", "status.successful":
return label, value, nil
default:
return "", "", fmt.Errorf("field label %q not supported for batchv1.Job", label)
return "", "", fmt.Errorf("field label %q not supported for Job", label)
}
},
)
})
}
// The following functions don't do anything special, but they need to be added
// here due to the dependency of v1beta1 and v2alpha1 on v1.
// here due to the dependency of v1beta1 on v1.
func Convert_batch_JobSpec_To_v1_JobSpec(in *batch.JobSpec, out *v1.JobSpec, s conversion.Scope) error {
return autoConvert_batch_JobSpec_To_v1_JobSpec(in, out, s)

View File

@@ -19,6 +19,7 @@ package v1
import (
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime"
utilpointer "k8s.io/utils/pointer"
)
func addDefaultingFuncs(scheme *runtime.Scheme) error {
@@ -29,18 +30,14 @@ func SetDefaults_Job(obj *batchv1.Job) {
// For a non-parallel job, you can leave both `.spec.completions` and
// `.spec.parallelism` unset. When both are unset, both are defaulted to 1.
if obj.Spec.Completions == nil && obj.Spec.Parallelism == nil {
obj.Spec.Completions = new(int32)
*obj.Spec.Completions = 1
obj.Spec.Parallelism = new(int32)
*obj.Spec.Parallelism = 1
obj.Spec.Completions = utilpointer.Int32Ptr(1)
obj.Spec.Parallelism = utilpointer.Int32Ptr(1)
}
if obj.Spec.Parallelism == nil {
obj.Spec.Parallelism = new(int32)
*obj.Spec.Parallelism = 1
obj.Spec.Parallelism = utilpointer.Int32Ptr(1)
}
if obj.Spec.BackoffLimit == nil {
obj.Spec.BackoffLimit = new(int32)
*obj.Spec.BackoffLimit = 6
obj.Spec.BackoffLimit = utilpointer.Int32Ptr(6)
}
labels := obj.Spec.Template.Labels
if labels != nil && len(obj.Labels) == 0 {
@@ -50,3 +47,18 @@ func SetDefaults_Job(obj *batchv1.Job) {
obj.Spec.CompletionMode = batchv1.NonIndexedCompletion
}
}
func SetDefaults_CronJob(obj *batchv1.CronJob) {
if obj.Spec.ConcurrencyPolicy == "" {
obj.Spec.ConcurrencyPolicy = batchv1.AllowConcurrent
}
if obj.Spec.Suspend == nil {
obj.Spec.Suspend = utilpointer.BoolPtr(false)
}
if obj.Spec.SuccessfulJobsHistoryLimit == nil {
obj.Spec.SuccessfulJobsHistoryLimit = utilpointer.Int32Ptr(3)
}
if obj.Spec.FailedJobsHistoryLimit == nil {
obj.Spec.FailedJobsHistoryLimit = utilpointer.Int32Ptr(1)
}
}

View File

@@ -259,3 +259,69 @@ func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
}
return obj3
}
func TestSetDefaultCronJob(t *testing.T) {
tests := map[string]struct {
original *batchv1.CronJob
expected *batchv1.CronJob
}{
"empty batchv1.CronJob should default batchv1.ConcurrencyPolicy and Suspend": {
original: &batchv1.CronJob{},
expected: &batchv1.CronJob{
Spec: batchv1.CronJobSpec{
ConcurrencyPolicy: batchv1.AllowConcurrent,
Suspend: newBool(false),
SuccessfulJobsHistoryLimit: utilpointer.Int32Ptr(3),
FailedJobsHistoryLimit: utilpointer.Int32Ptr(1),
},
},
},
"set fields should not be defaulted": {
original: &batchv1.CronJob{
Spec: batchv1.CronJobSpec{
ConcurrencyPolicy: batchv1.ForbidConcurrent,
Suspend: newBool(true),
SuccessfulJobsHistoryLimit: utilpointer.Int32Ptr(5),
FailedJobsHistoryLimit: utilpointer.Int32Ptr(5),
},
},
expected: &batchv1.CronJob{
Spec: batchv1.CronJobSpec{
ConcurrencyPolicy: batchv1.ForbidConcurrent,
Suspend: newBool(true),
SuccessfulJobsHistoryLimit: utilpointer.Int32Ptr(5),
FailedJobsHistoryLimit: utilpointer.Int32Ptr(5),
},
},
},
}
for name, test := range tests {
original := test.original
expected := test.expected
obj2 := roundTrip(t, runtime.Object(original))
actual, ok := obj2.(*batchv1.CronJob)
if !ok {
t.Errorf("%s: unexpected object: %v", name, actual)
t.FailNow()
}
if actual.Spec.ConcurrencyPolicy != expected.Spec.ConcurrencyPolicy {
t.Errorf("%s: got different concurrencyPolicy than expected: %v %v", name, actual.Spec.ConcurrencyPolicy, expected.Spec.ConcurrencyPolicy)
}
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)
}
}
}
func newBool(val bool) *bool {
p := new(bool)
*p = val
return p
}