Added Selector Generation to Job.
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.
This commit is contained in:
@@ -53,6 +53,7 @@ func validNewJob() *extensions.Job {
|
||||
Selector: &unversioned.LabelSelector{
|
||||
MatchLabels: map[string]string{"a": "b"},
|
||||
},
|
||||
ManualSelector: newBool(true),
|
||||
Template: api.PodTemplateSpec{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Labels: map[string]string{"a": "b"},
|
||||
@@ -165,3 +166,9 @@ func TestWatch(t *testing.T) {
|
||||
}
|
||||
|
||||
// TODO: test update /status
|
||||
|
||||
func newBool(val bool) *bool {
|
||||
p := new(bool)
|
||||
*p = val
|
||||
return p
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions/validation"
|
||||
"k8s.io/kubernetes/pkg/fields"
|
||||
@@ -60,9 +61,63 @@ func (jobStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
||||
// Validate validates a new job.
|
||||
func (jobStrategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
|
||||
job := obj.(*extensions.Job)
|
||||
// TODO: move UID generation earlier and do this in defaulting logic?
|
||||
if job.Spec.ManualSelector == nil || *job.Spec.ManualSelector == false {
|
||||
generateSelector(job)
|
||||
}
|
||||
return validation.ValidateJob(job)
|
||||
}
|
||||
|
||||
// generateSelector adds a selector to a job and labels to its template
|
||||
// which can be used to uniquely identify the pods created by that job,
|
||||
// if the user has requested this behavior.
|
||||
func generateSelector(obj *extensions.Job) {
|
||||
if obj.Spec.Template.Labels == nil {
|
||||
obj.Spec.Template.Labels = make(map[string]string)
|
||||
}
|
||||
// The job-name label is unique except in cases that are expected to be
|
||||
// quite uncommon, and is more user friendly than uid. So, we add it as
|
||||
// a label.
|
||||
_, found := obj.Spec.Template.Labels["job-name"]
|
||||
if found {
|
||||
// User asked us to not automatically generate a selector and labels,
|
||||
// but set a possibly conflicting value. If there is a conflict,
|
||||
// we will reject in validation.
|
||||
} else {
|
||||
obj.Spec.Template.Labels["job-name"] = string(obj.ObjectMeta.Name)
|
||||
}
|
||||
// The controller-uid label makes the pods that belong to this job
|
||||
// only match this job.
|
||||
_, found = obj.Spec.Template.Labels["controller-uid"]
|
||||
if found {
|
||||
// User asked us to automatically generate a selector and labels,
|
||||
// but set a possibly conflicting value. If there is a conflict,
|
||||
// we will reject in validation.
|
||||
} else {
|
||||
obj.Spec.Template.Labels["controller-uid"] = string(obj.ObjectMeta.UID)
|
||||
}
|
||||
// Select the controller-uid label. This is sufficient for uniqueness.
|
||||
if obj.Spec.Selector == nil {
|
||||
obj.Spec.Selector = &unversioned.LabelSelector{}
|
||||
}
|
||||
if obj.Spec.Selector.MatchLabels == nil {
|
||||
obj.Spec.Selector.MatchLabels = make(map[string]string)
|
||||
}
|
||||
if _, found := obj.Spec.Selector.MatchLabels["controller-uid"]; !found {
|
||||
obj.Spec.Selector.MatchLabels["controller-uid"] = string(obj.ObjectMeta.UID)
|
||||
}
|
||||
// If the user specified matchLabel controller-uid=$WRONGUID, then it should fail
|
||||
// in validation, either because the selector does not match the pod template
|
||||
// (controller-uid=$WRONGUID does not match controller-uid=$UID, which we applied
|
||||
// above, or we will reject in validation because the template has the wrong
|
||||
// labels.
|
||||
}
|
||||
|
||||
// TODO: generalize generateSelector so it can work for other controller
|
||||
// objects such as ReplicaSet. Can use pkg/api/meta to generically get the
|
||||
// UID, but need some way to generically access the selector and pod labels
|
||||
// fields.
|
||||
|
||||
// Canonicalize normalizes the object after validation.
|
||||
func (jobStrategy) Canonicalize(obj runtime.Object) {
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package job
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
@@ -25,8 +26,15 @@ import (
|
||||
"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() {
|
||||
@@ -55,8 +63,9 @@ func TestJobStrategy(t *testing.T) {
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: extensions.JobSpec{
|
||||
Selector: validSelector,
|
||||
Template: validPodTemplateSpec,
|
||||
Selector: validSelector,
|
||||
Template: validPodTemplateSpec,
|
||||
ManualSelector: newBool(true),
|
||||
},
|
||||
Status: extensions.JobStatus{
|
||||
Active: 11,
|
||||
@@ -93,6 +102,56 @@ func TestJobStrategy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
|
Reference in New Issue
Block a user