
Added selector generation to Job's strategy.Validate, right before validation. Can't do in defaulting since UID is not known. Added a validation to Job to ensure that the generated labels and selector are correct when generation was requested. This happens right after generation, but validation is in a better place to return an error. Adds "manualSelector" field to batch/v1 Job to control selector generation. Adds same field to extensions/__internal. Conversion between those two is automatic. Adds "autoSelector" field to extensions/v1beta1 Job. Used for storing batch/v1 Jobs - Default for v1 is to do generation. - Default for v1beta1 is to not do it. - In both cases, unset == false == do the default thing. Release notes: Added batch/v1 group, which contains just Job, and which is the next version of extensions/v1beta1 Job. The changes from the previous version are: - Users no longer need to ensure labels on their pod template are unique to the enclosing job (but may add labels as needed for categorization). - In v1beta1, job.spec.selector was defaulted from pod labels, with the user responsible for uniqueness. In v1, a unique label is generated and added to the pod template, and used as the selector (other labels added by user stay on pod template, but need not be used by selector). - a new field called "manualSelector" field exists to control whether the new behavior is used, versus a more error-prone but more flexible "manual" (not generated) seletor. Most users will not need to use this field and should leave it unset. Users who are creating extensions.Job go objects and then posting them using the go client will see a change in the default behavior. They need to either stop providing a selector (relying on selector generation) or else specify "spec.manualSelector" until they are ready to do the former.
233 lines
6.3 KiB
Go
233 lines
6.3 KiB
Go
/*
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package job
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
|
apitesting "k8s.io/kubernetes/pkg/api/testing"
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
"k8s.io/kubernetes/pkg/types"
|
|
)
|
|
|
|
func newBool(a bool) *bool {
|
|
r := new(bool)
|
|
*r = a
|
|
return r
|
|
}
|
|
|
|
func TestJobStrategy(t *testing.T) {
|
|
ctx := api.NewDefaultContext()
|
|
if !Strategy.NamespaceScoped() {
|
|
t.Errorf("Job must be namespace scoped")
|
|
}
|
|
if Strategy.AllowCreateOnUpdate() {
|
|
t.Errorf("Job should not allow create on update")
|
|
}
|
|
|
|
validSelector := &unversioned.LabelSelector{
|
|
MatchLabels: map[string]string{"a": "b"},
|
|
}
|
|
validPodTemplateSpec := api.PodTemplateSpec{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Labels: validSelector.MatchLabels,
|
|
},
|
|
Spec: api.PodSpec{
|
|
RestartPolicy: api.RestartPolicyOnFailure,
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
|
|
},
|
|
}
|
|
job := &extensions.Job{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Name: "myjob",
|
|
Namespace: api.NamespaceDefault,
|
|
},
|
|
Spec: extensions.JobSpec{
|
|
Selector: validSelector,
|
|
Template: validPodTemplateSpec,
|
|
ManualSelector: newBool(true),
|
|
},
|
|
Status: extensions.JobStatus{
|
|
Active: 11,
|
|
},
|
|
}
|
|
|
|
Strategy.PrepareForCreate(job)
|
|
if job.Status.Active != 0 {
|
|
t.Errorf("Job does not allow setting status on create")
|
|
}
|
|
errs := Strategy.Validate(ctx, job)
|
|
if len(errs) != 0 {
|
|
t.Errorf("Unexpected error validating %v", errs)
|
|
}
|
|
parallelism := 10
|
|
updatedJob := &extensions.Job{
|
|
ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "4"},
|
|
Spec: extensions.JobSpec{
|
|
Parallelism: ¶llelism,
|
|
},
|
|
Status: extensions.JobStatus{
|
|
Active: 11,
|
|
},
|
|
}
|
|
// ensure we do not change status
|
|
job.Status.Active = 10
|
|
Strategy.PrepareForUpdate(updatedJob, job)
|
|
if updatedJob.Status.Active != 10 {
|
|
t.Errorf("PrepareForUpdate should have preserved prior version status")
|
|
}
|
|
errs = Strategy.ValidateUpdate(ctx, updatedJob, job)
|
|
if len(errs) == 0 {
|
|
t.Errorf("Expected a validation error")
|
|
}
|
|
}
|
|
|
|
func TestJobStrategyWithGeneration(t *testing.T) {
|
|
ctx := api.NewDefaultContext()
|
|
|
|
theUID := types.UID("1a2b3c4d5e6f7g8h9i0k")
|
|
|
|
validPodTemplateSpec := api.PodTemplateSpec{
|
|
Spec: api.PodSpec{
|
|
RestartPolicy: api.RestartPolicyOnFailure,
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
|
|
},
|
|
}
|
|
job := &extensions.Job{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Name: "myjob2",
|
|
Namespace: api.NamespaceDefault,
|
|
UID: theUID,
|
|
},
|
|
Spec: extensions.JobSpec{
|
|
Selector: nil,
|
|
Template: validPodTemplateSpec,
|
|
},
|
|
}
|
|
|
|
Strategy.PrepareForCreate(job)
|
|
errs := Strategy.Validate(ctx, job)
|
|
if len(errs) != 0 {
|
|
t.Errorf("Unexpected error validating %v", errs)
|
|
}
|
|
|
|
// Validate the stuff that validation should have validated.
|
|
if job.Spec.Selector == nil {
|
|
t.Errorf("Selector not generated")
|
|
}
|
|
expectedLabels := make(map[string]string)
|
|
expectedLabels["controller-uid"] = string(theUID)
|
|
if !reflect.DeepEqual(job.Spec.Selector.MatchLabels, expectedLabels) {
|
|
t.Errorf("Expected label selector not generated")
|
|
}
|
|
if job.Spec.Template.ObjectMeta.Labels == nil {
|
|
t.Errorf("Expected template labels not generated")
|
|
}
|
|
if v, ok := job.Spec.Template.ObjectMeta.Labels["job-name"]; !ok || v != "myjob2" {
|
|
t.Errorf("Expected template labels not present")
|
|
}
|
|
if v, ok := job.Spec.Template.ObjectMeta.Labels["controller-uid"]; !ok || v != string(theUID) {
|
|
t.Errorf("Expected template labels not present: ok: %v, v: %v", ok, v)
|
|
}
|
|
}
|
|
|
|
func TestJobStatusStrategy(t *testing.T) {
|
|
ctx := api.NewDefaultContext()
|
|
if !StatusStrategy.NamespaceScoped() {
|
|
t.Errorf("Job must be namespace scoped")
|
|
}
|
|
if StatusStrategy.AllowCreateOnUpdate() {
|
|
t.Errorf("Job should not allow create on update")
|
|
}
|
|
validSelector := &unversioned.LabelSelector{
|
|
MatchLabels: map[string]string{"a": "b"},
|
|
}
|
|
validPodTemplateSpec := api.PodTemplateSpec{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Labels: validSelector.MatchLabels,
|
|
},
|
|
Spec: api.PodSpec{
|
|
RestartPolicy: api.RestartPolicyOnFailure,
|
|
DNSPolicy: api.DNSClusterFirst,
|
|
Containers: []api.Container{{Name: "abc", Image: "image", ImagePullPolicy: "IfNotPresent"}},
|
|
},
|
|
}
|
|
oldParallelism := 10
|
|
newParallelism := 11
|
|
oldJob := &extensions.Job{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Name: "myjob",
|
|
Namespace: api.NamespaceDefault,
|
|
ResourceVersion: "10",
|
|
},
|
|
Spec: extensions.JobSpec{
|
|
Selector: validSelector,
|
|
Template: validPodTemplateSpec,
|
|
Parallelism: &oldParallelism,
|
|
},
|
|
Status: extensions.JobStatus{
|
|
Active: 11,
|
|
},
|
|
}
|
|
newJob := &extensions.Job{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Name: "myjob",
|
|
Namespace: api.NamespaceDefault,
|
|
ResourceVersion: "9",
|
|
},
|
|
Spec: extensions.JobSpec{
|
|
Selector: validSelector,
|
|
Template: validPodTemplateSpec,
|
|
Parallelism: &newParallelism,
|
|
},
|
|
Status: extensions.JobStatus{
|
|
Active: 12,
|
|
},
|
|
}
|
|
|
|
StatusStrategy.PrepareForUpdate(newJob, oldJob)
|
|
if newJob.Status.Active != 12 {
|
|
t.Errorf("Job status updates must allow changes to job status")
|
|
}
|
|
if *newJob.Spec.Parallelism != 10 {
|
|
t.Errorf("Job status updates must now allow changes to job spec")
|
|
}
|
|
errs := StatusStrategy.ValidateUpdate(ctx, newJob, oldJob)
|
|
if len(errs) != 0 {
|
|
t.Errorf("Unexpected error %v", errs)
|
|
}
|
|
if newJob.ResourceVersion != "9" {
|
|
t.Errorf("Incoming resource version on update should not be mutated")
|
|
}
|
|
}
|
|
|
|
func TestSelectableFieldLabelConversions(t *testing.T) {
|
|
apitesting.TestSelectableFieldLabelConversionsOfKind(t,
|
|
testapi.Extensions.GroupVersion().String(),
|
|
"Job",
|
|
labels.Set(JobToSelectableFields(&extensions.Job{})),
|
|
nil,
|
|
)
|
|
}
|