Skip to content

Commit

Permalink
Issue 37166: remove everything from batch/v2alpha1 that is not new
Browse files Browse the repository at this point in the history
  • Loading branch information
soltysh committed Mar 6, 2017
1 parent df70b30 commit 7cba9d9
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 500 deletions.
49 changes: 1 addition & 48 deletions pkg/apis/batch/v2alpha1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,11 @@ package v2alpha1
import (
"fmt"

"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime"
v1 "k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apis/batch"
)

func addConversionFuncs(scheme *runtime.Scheme) error {
// Add non-generated conversion functions
err := scheme.AddConversionFuncs(
Convert_batch_JobSpec_To_v2alpha1_JobSpec,
Convert_v2alpha1_JobSpec_To_batch_JobSpec,
)
if err != nil {
return err
}

var err error
// Add field label conversions for kinds having selectable nothing but ObjectMeta fields.
for _, k := range []string{"Job", "JobTemplate", "CronJob"} {
kind := k // don't close over range variables
Expand All @@ -53,39 +42,3 @@ func addConversionFuncs(scheme *runtime.Scheme) error {
}
return nil
}

func Convert_batch_JobSpec_To_v2alpha1_JobSpec(in *batch.JobSpec, out *JobSpec, s conversion.Scope) error {
out.Parallelism = in.Parallelism
out.Completions = in.Completions
out.ActiveDeadlineSeconds = in.ActiveDeadlineSeconds
out.Selector = in.Selector
if in.ManualSelector != nil {
out.ManualSelector = new(bool)
*out.ManualSelector = *in.ManualSelector
} else {
out.ManualSelector = nil
}

if err := v1.Convert_api_PodTemplateSpec_To_v1_PodTemplateSpec(&in.Template, &out.Template, s); err != nil {
return err
}
return nil
}

func Convert_v2alpha1_JobSpec_To_batch_JobSpec(in *JobSpec, out *batch.JobSpec, s conversion.Scope) error {
out.Parallelism = in.Parallelism
out.Completions = in.Completions
out.ActiveDeadlineSeconds = in.ActiveDeadlineSeconds
out.Selector = in.Selector
if in.ManualSelector != nil {
out.ManualSelector = new(bool)
*out.ManualSelector = *in.ManualSelector
} else {
out.ManualSelector = nil
}

if err := v1.Convert_v1_PodTemplateSpec_To_api_PodTemplateSpec(&in.Template, &out.Template, s); err != nil {
return err
}
return nil
}
20 changes: 0 additions & 20 deletions pkg/apis/batch/v2alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,10 @@ import (
func addDefaultingFuncs(scheme *runtime.Scheme) error {
RegisterDefaults(scheme)
return scheme.AddDefaultingFuncs(
SetDefaults_Job,
SetDefaults_CronJob,
)
}

func SetDefaults_Job(obj *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
}
if obj.Spec.Parallelism == nil {
obj.Spec.Parallelism = new(int32)
*obj.Spec.Parallelism = 1
}
labels := obj.Spec.Template.Labels
if labels != nil && len(obj.Labels) == 0 {
obj.Labels = labels
}
}

func SetDefaults_CronJob(obj *CronJob) {
if obj.Spec.ConcurrencyPolicy == "" {
obj.Spec.ConcurrencyPolicy = AllowConcurrent
Expand Down
171 changes: 25 additions & 146 deletions pkg/apis/batch/v2alpha1/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,179 +20,58 @@ import (
"reflect"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/api"
_ "k8s.io/kubernetes/pkg/api/install"
"k8s.io/kubernetes/pkg/api/v1"
_ "k8s.io/kubernetes/pkg/apis/batch/install"
. "k8s.io/kubernetes/pkg/apis/batch/v2alpha1"
)

func TestSetDefaultJob(t *testing.T) {
defaultLabels := map[string]string{"default": "default"}
func TestSetDefaultCronJob(t *testing.T) {
tests := map[string]struct {
original *Job
expected *Job
expectLabels bool
original *CronJob
expected *CronJob
}{
"both unspecified -> sets both to 1": {
original: &Job{
Spec: JobSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
},
"empty CronJob should default ConcurrencyPolicy and Suspend": {
original: &CronJob{},
expected: &CronJob{
Spec: CronJobSpec{
ConcurrencyPolicy: AllowConcurrent,
Suspend: newBool(false),
},
},
expected: &Job{
Spec: JobSpec{
Completions: newInt32(1),
Parallelism: newInt32(1),
},
},
expectLabels: true,
},
"both unspecified -> sets both to 1 and no default labels": {
original: &Job{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"mylabel": "myvalue"},
},
Spec: JobSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
},
},
},
expected: &Job{
Spec: JobSpec{
Completions: newInt32(1),
Parallelism: newInt32(1),
},
},
},
"WQ: Parallelism explicitly 0 and completions unset -> no change": {
original: &Job{
Spec: JobSpec{
Parallelism: newInt32(0),
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
},
},
},
expected: &Job{
Spec: JobSpec{
Parallelism: newInt32(0),
},
},
expectLabels: true,
},
"WQ: Parallelism explicitly 2 and completions unset -> no change": {
original: &Job{
Spec: JobSpec{
Parallelism: newInt32(2),
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
},
},
},
expected: &Job{
Spec: JobSpec{
Parallelism: newInt32(2),
},
},
expectLabels: true,
},
"Completions explicitly 2 and parallelism unset -> parallelism is defaulted": {
original: &Job{
Spec: JobSpec{
Completions: newInt32(2),
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
},
},
},
expected: &Job{
Spec: JobSpec{
Completions: newInt32(2),
Parallelism: newInt32(1),
},
},
expectLabels: true,
},
"Both set -> no change": {
original: &Job{
Spec: JobSpec{
Completions: newInt32(10),
Parallelism: newInt32(11),
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
},
},
},
expected: &Job{
Spec: JobSpec{
Completions: newInt32(10),
Parallelism: newInt32(11),
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
},
},
},
expectLabels: true,
},
"Both set, flipped -> no change": {
original: &Job{
Spec: JobSpec{
Completions: newInt32(11),
Parallelism: newInt32(10),
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
},
"nothing should be defaulted": {
original: &CronJob{
Spec: CronJobSpec{
ConcurrencyPolicy: ForbidConcurrent,
Suspend: newBool(true),
},
},
expected: &Job{
Spec: JobSpec{
Completions: newInt32(11),
Parallelism: newInt32(10),
expected: &CronJob{
Spec: CronJobSpec{
ConcurrencyPolicy: ForbidConcurrent,
Suspend: newBool(true),
},
},
expectLabels: true,
},
}

for name, test := range tests {
original := test.original
expected := test.expected
obj2 := roundTrip(t, runtime.Object(original))
actual, ok := obj2.(*Job)
actual, ok := obj2.(*CronJob)
if !ok {
t.Errorf("%s: unexpected object: %v", name, actual)
t.FailNow()
}
if (actual.Spec.Completions == nil) != (expected.Spec.Completions == nil) {
t.Errorf("%s: got different *completions than expected: %v %v", name, actual.Spec.Completions, expected.Spec.Completions)
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.Completions != nil && expected.Spec.Completions != nil {
if *actual.Spec.Completions != *expected.Spec.Completions {
t.Errorf("%s: got different completions than expected: %d %d", name, *actual.Spec.Completions, *expected.Spec.Completions)
}
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.Parallelism == nil) != (expected.Spec.Parallelism == nil) {
t.Errorf("%s: got different *Parallelism than expected: %v %v", name, actual.Spec.Parallelism, expected.Spec.Parallelism)
}
if actual.Spec.Parallelism != nil && expected.Spec.Parallelism != nil {
if *actual.Spec.Parallelism != *expected.Spec.Parallelism {
t.Errorf("%s: got different parallelism than expected: %d %d", name, *actual.Spec.Parallelism, *expected.Spec.Parallelism)
}
}
if test.expectLabels != reflect.DeepEqual(actual.Labels, actual.Spec.Template.Labels) {
if test.expectLabels {
t.Errorf("%s: expected: %v, got: %v", name, actual.Spec.Template.Labels, actual.Labels)
} else {
t.Errorf("%s: unexpected equality: %v", name, actual.Labels)
}
}

}
}

Expand All @@ -216,8 +95,8 @@ func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
return obj3
}

func newInt32(val int32) *int32 {
p := new(int32)
func newBool(val bool) *bool {
p := new(bool)
*p = val
return p
}
2 changes: 0 additions & 2 deletions pkg/apis/batch/v2alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ var (
// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Job{},
&JobList{},
&JobTemplate{},
&CronJob{},
&CronJobList{},
Expand Down
Loading

0 comments on commit 7cba9d9

Please sign in to comment.