Merge pull request #18876 from erictune/dynamic-job

Auto commit by PR queue bot
This commit is contained in:
k8s-merge-robot
2016-01-25 08:06:22 -08:00
14 changed files with 325 additions and 91 deletions

View File

@@ -481,7 +481,7 @@ type JobSpec struct {
Parallelism *int `json:"parallelism,omitempty"`
// Completions specifies the desired number of successfully finished pods the
// job should be run with. Defaults to 1.
// job should be run with. When unset, any pod exiting signals the job to complete.
Completions *int `json:"completions,omitempty"`
// Optional duration in seconds relative to the startTime that the job may be active

View File

@@ -120,12 +120,17 @@ func addDefaultingFuncs(scheme *runtime.Scheme) {
obj.Labels = labels
}
}
if obj.Spec.Completions == nil {
completions := int32(1)
obj.Spec.Completions = &completions
// 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 = obj.Spec.Completions
obj.Spec.Parallelism = new(int32)
*obj.Spec.Parallelism = 1
}
},
func(obj *HorizontalPodAutoscaler) {

View File

@@ -302,7 +302,124 @@ func TestSetDefaultDeployment(t *testing.T) {
}
}
func TestSetDefaultJob(t *testing.T) {
func TestSetDefaultJobParallelismAndCompletions(t *testing.T) {
tests := []struct {
original *Job
expected *Job
}{
// both unspecified -> sets both to 1
{
original: &Job{
Spec: JobSpec{},
},
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),
},
},
expected: &Job{
Spec: JobSpec{
Parallelism: newInt32(0),
},
},
},
// WQ: Parallelism explicitly 2 and completions unset -> no change
{
original: &Job{
Spec: JobSpec{
Parallelism: newInt32(2),
},
},
expected: &Job{
Spec: JobSpec{
Parallelism: newInt32(2),
},
},
},
// Completions explicitly 2 and parallelism unset -> parallelism is defaulted
{
original: &Job{
Spec: JobSpec{
Completions: newInt32(2),
},
},
expected: &Job{
Spec: JobSpec{
Completions: newInt32(2),
Parallelism: newInt32(1),
},
},
},
// Both set -> no change
{
original: &Job{
Spec: JobSpec{
Completions: newInt32(10),
Parallelism: newInt32(11),
},
},
expected: &Job{
Spec: JobSpec{
Completions: newInt32(10),
Parallelism: newInt32(11),
},
},
},
// Both set, flipped -> no change
{
original: &Job{
Spec: JobSpec{
Completions: newInt32(11),
Parallelism: newInt32(10),
},
},
expected: &Job{
Spec: JobSpec{
Completions: newInt32(11),
Parallelism: newInt32(10),
},
},
},
}
for _, tc := range tests {
original := tc.original
expected := tc.expected
obj2 := roundTrip(t, runtime.Object(original))
got, ok := obj2.(*Job)
if !ok {
t.Errorf("unexpected object: %v", got)
t.FailNow()
}
if (got.Spec.Completions == nil) != (expected.Spec.Completions == nil) {
t.Errorf("got different *completions than expected: %v %v", got.Spec.Completions, expected.Spec.Completions)
}
if got.Spec.Completions != nil && expected.Spec.Completions != nil {
if *got.Spec.Completions != *expected.Spec.Completions {
t.Errorf("got different completions than expected: %d %d", *got.Spec.Completions, *expected.Spec.Completions)
}
}
if (got.Spec.Parallelism == nil) != (expected.Spec.Parallelism == nil) {
t.Errorf("got different *Parallelism than expected: %v %v", got.Spec.Parallelism, expected.Spec.Parallelism)
}
if got.Spec.Parallelism != nil && expected.Spec.Parallelism != nil {
if *got.Spec.Parallelism != *expected.Spec.Parallelism {
t.Errorf("got different parallelism than expected: %d %d", *got.Spec.Parallelism, *expected.Spec.Parallelism)
}
}
}
}
func TestSetDefaultJobSelector(t *testing.T) {
expected := &Job{
Spec: JobSpec{
Selector: &LabelSelector{
@@ -331,28 +448,6 @@ func TestSetDefaultJob(t *testing.T) {
},
},
},
// selector from template labels, completions set explicitly, parallelism - default
{
Spec: JobSpec{
Completions: newInt32(1),
Template: v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{"job": "selector"},
},
},
},
},
// selector from template labels, completions - default, parallelism set explicitly
{
Spec: JobSpec{
Parallelism: newInt32(1),
Template: v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{"job": "selector"},
},
},
},
},
}
for _, original := range tests {
@@ -362,12 +457,6 @@ func TestSetDefaultJob(t *testing.T) {
t.Errorf("unexpected object: %v", got)
t.FailNow()
}
if *got.Spec.Completions != *expected.Spec.Completions {
t.Errorf("got different completions than expected: %d %d", *got.Spec.Completions, *expected.Spec.Completions)
}
if *got.Spec.Parallelism != *expected.Spec.Parallelism {
t.Errorf("got different parallelism than expected: %d %d", *got.Spec.Parallelism, *expected.Spec.Parallelism)
}
if !reflect.DeepEqual(got.Spec.Selector, expected.Spec.Selector) {
t.Errorf("got different selectors %#v %#v", got.Spec.Selector, expected.Spec.Selector)
}

View File

@@ -473,8 +473,10 @@ type JobSpec struct {
Parallelism *int32 `json:"parallelism,omitempty"`
// Completions specifies the desired number of successfully finished pods the
// job should be run with. Defaults to 1.
// More info: http://releases.k8s.io/HEAD/docs/user-guide/jobs.md
// job should be run with. Setting to nil means that the success of any
// pod signals the success of all pods, and allows parallelism to have any positive
// value. Setting to 1 means that parallelism is limited to 1 and the success of that
// pod signals the success of the job.
Completions *int32 `json:"completions,omitempty"`
// Optional duration in seconds relative to the startTime that the job may be active

View File

@@ -381,7 +381,7 @@ func (JobList) SwaggerDoc() map[string]string {
var map_JobSpec = map[string]string{
"": "JobSpec describes how the job execution will look like.",
"parallelism": "Parallelism specifies the maximum desired number of pods the job should run at any given time. The actual number of pods running in steady state will be less than this number when ((.spec.completions - .status.successful) < .spec.parallelism), i.e. when the work left to do is less than max parallelism. More info: http://releases.k8s.io/HEAD/docs/user-guide/jobs.md",
"completions": "Completions specifies the desired number of successfully finished pods the job should be run with. Defaults to 1. More info: http://releases.k8s.io/HEAD/docs/user-guide/jobs.md",
"completions": "Completions specifies the desired number of successfully finished pods the job should be run with. Setting to nil means that the success of any pod signals the success of all pods, and allows parallelism to have any positive value. Setting to 1 means that parallelism is limited to 1 and the success of that pod signals the success of the job.",
"activeDeadlineSeconds": "Optional duration in seconds relative to the startTime that the job may be active before the system tries to terminate it; value must be positive integer",
"selector": "Selector is a label query over pods that should match the pod count. More info: http://releases.k8s.io/HEAD/docs/user-guide/labels.md#label-selectors",
"template": "Template is the object that describes the pod that will be created when executing a job. More info: http://releases.k8s.io/HEAD/docs/user-guide/jobs.md",