move job to generalized label selector

This commit is contained in:
Mike Danese 2015-10-14 11:04:33 -07:00
parent 5f55be5dc1
commit a7a54cac74
8 changed files with 46 additions and 20 deletions

View File

@ -382,11 +382,9 @@ func (s *StoreToJobLister) GetPodJobs(pod *api.Pod) (jobs []extensions.Job, err
if job.Namespace != pod.Namespace { if job.Namespace != pod.Namespace {
continue continue
} }
labelSet := labels.Set(job.Spec.Selector)
selector = labels.Set(job.Spec.Selector).AsSelector()
// Job with a nil or empty selector match nothing selector, _ = extensions.PodSelectorAsSelector(job.Spec.Selector)
if labelSet.AsSelector().Empty() || !selector.Matches(labels.Set(pod.Labels)) { if !selector.Matches(labels.Set(pod.Labels)) {
continue continue
} }
jobs = append(jobs, job) jobs = append(jobs, job)

View File

@ -313,7 +313,8 @@ func (jm *JobController) syncJob(key string) error {
return err return err
} }
jobNeedsSync := jm.expectations.SatisfiedExpectations(jobKey) jobNeedsSync := jm.expectations.SatisfiedExpectations(jobKey)
podList, err := jm.podStore.Pods(job.Namespace).List(labels.Set(job.Spec.Selector).AsSelector()) selector, _ := extensions.PodSelectorAsSelector(job.Spec.Selector)
podList, err := jm.podStore.Pods(job.Namespace).List(selector)
if err != nil { if err != nil {
glog.Errorf("Error getting pods for job %q: %v", key, err) glog.Errorf("Error getting pods for job %q: %v", key, err)
jm.queue.Add(key) jm.queue.Add(key)

View File

@ -43,7 +43,9 @@ func newJob(parallelism, completions int) *extensions.Job {
Spec: extensions.JobSpec{ Spec: extensions.JobSpec{
Parallelism: &parallelism, Parallelism: &parallelism,
Completions: &completions, Completions: &completions,
Selector: map[string]string{"foo": "bar"}, Selector: &extensions.PodSelector{
MatchLabels: map[string]string{"foo": "bar"},
},
Template: api.PodTemplateSpec{ Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Labels: map[string]string{ Labels: map[string]string{
@ -76,7 +78,7 @@ func newPodList(count int, status api.PodPhase, job *extensions.Job) []api.Pod {
newPod := api.Pod{ newPod := api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: fmt.Sprintf("pod-%v", unversioned.Now().UnixNano()), Name: fmt.Sprintf("pod-%v", unversioned.Now().UnixNano()),
Labels: job.Spec.Selector, Labels: job.Spec.Selector.MatchLabels,
Namespace: job.Namespace, Namespace: job.Namespace,
}, },
Status: api.PodStatus{Phase: status}, Status: api.PodStatus{Phase: status},
@ -289,7 +291,9 @@ func TestJobPodLookup(t *testing.T) {
job: &extensions.Job{ job: &extensions.Job{
ObjectMeta: api.ObjectMeta{Name: "foo"}, ObjectMeta: api.ObjectMeta{Name: "foo"},
Spec: extensions.JobSpec{ Spec: extensions.JobSpec{
Selector: map[string]string{"foo": "bar"}, Selector: &extensions.PodSelector{
MatchLabels: map[string]string{"foo": "bar"},
},
}, },
}, },
pod: &api.Pod{ pod: &api.Pod{
@ -306,7 +310,15 @@ func TestJobPodLookup(t *testing.T) {
job: &extensions.Job{ job: &extensions.Job{
ObjectMeta: api.ObjectMeta{Name: "bar", Namespace: "ns"}, ObjectMeta: api.ObjectMeta{Name: "bar", Namespace: "ns"},
Spec: extensions.JobSpec{ Spec: extensions.JobSpec{
Selector: map[string]string{"foo": "bar"}, Selector: &extensions.PodSelector{
MatchExpressions: []extensions.PodSelectorRequirement{
{
Key: "foo",
Operator: extensions.PodSelectorOpIn,
Values: []string{"bar"},
},
},
},
}, },
}, },
pod: &api.Pod{ pod: &api.Pod{

View File

@ -885,7 +885,8 @@ func describeJob(job *extensions.Job, events *api.EventList) (string, error) {
fmt.Fprintf(out, "Name:\t%s\n", job.Name) fmt.Fprintf(out, "Name:\t%s\n", job.Name)
fmt.Fprintf(out, "Namespace:\t%s\n", job.Namespace) fmt.Fprintf(out, "Namespace:\t%s\n", job.Namespace)
fmt.Fprintf(out, "Image(s):\t%s\n", makeImageList(&job.Spec.Template.Spec)) fmt.Fprintf(out, "Image(s):\t%s\n", makeImageList(&job.Spec.Template.Spec))
fmt.Fprintf(out, "Selector:\t%s\n", labels.FormatLabels(job.Spec.Selector)) selector, _ := extensions.PodSelectorAsSelector(job.Spec.Selector)
fmt.Fprintf(out, "Selector:\t%s\n", selector)
fmt.Fprintf(out, "Parallelism:\t%d\n", *job.Spec.Parallelism) fmt.Fprintf(out, "Parallelism:\t%d\n", *job.Spec.Parallelism)
fmt.Fprintf(out, "Completions:\t%d\n", *job.Spec.Completions) fmt.Fprintf(out, "Completions:\t%d\n", *job.Spec.Completions)
fmt.Fprintf(out, "Labels:\t%s\n", labels.FormatLabels(job.Labels)) fmt.Fprintf(out, "Labels:\t%s\n", labels.FormatLabels(job.Labels))

View File

@ -736,11 +736,13 @@ func printJob(job *extensions.Job, w io.Writer, withNamespace bool, wide bool, s
return err return err
} }
} }
selector, _ := extensions.PodSelectorAsSelector(job.Spec.Selector)
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n",
name, name,
firstContainer.Name, firstContainer.Name,
firstContainer.Image, firstContainer.Image,
labels.FormatLabels(job.Spec.Selector), selector.String(),
job.Status.Succeeded) job.Status.Succeeded)
if err != nil { if err != nil {
return err return err

View File

@ -293,7 +293,10 @@ func TestJobStop(t *testing.T) {
}, },
Spec: extensions.JobSpec{ Spec: extensions.JobSpec{
Parallelism: &zero, Parallelism: &zero,
Selector: map[string]string{"k1": "v1"}}, Selector: &extensions.PodSelector{
MatchLabels: map[string]string{"k1": "v1"},
},
},
}, },
&extensions.JobList{ // LIST &extensions.JobList{ // LIST
Items: []extensions.Job{ Items: []extensions.Job{
@ -304,7 +307,10 @@ func TestJobStop(t *testing.T) {
}, },
Spec: extensions.JobSpec{ Spec: extensions.JobSpec{
Parallelism: &zero, Parallelism: &zero,
Selector: map[string]string{"k1": "v1"}}, Selector: &extensions.PodSelector{
MatchLabels: map[string]string{"k1": "v1"},
},
},
}, },
}, },
}, },

View File

@ -47,7 +47,9 @@ func validNewJob() *extensions.Job {
Spec: extensions.JobSpec{ Spec: extensions.JobSpec{
Completions: &completions, Completions: &completions,
Parallelism: &parallelism, Parallelism: &parallelism,
Selector: map[string]string{"a": "b"}, Selector: &extensions.PodSelector{
MatchLabels: map[string]string{"a": "b"},
},
Template: api.PodTemplateSpec{ Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"a": "b"}, Labels: map[string]string{"a": "b"},
@ -80,7 +82,7 @@ func TestCreate(t *testing.T) {
&extensions.Job{ &extensions.Job{
Spec: extensions.JobSpec{ Spec: extensions.JobSpec{
Completions: validJob.Spec.Completions, Completions: validJob.Spec.Completions,
Selector: map[string]string{}, Selector: &extensions.PodSelector{},
Template: validJob.Spec.Template, Template: validJob.Spec.Template,
}, },
}, },
@ -103,7 +105,7 @@ func TestUpdate(t *testing.T) {
// invalid updateFunc // invalid updateFunc
func(obj runtime.Object) runtime.Object { func(obj runtime.Object) runtime.Object {
object := obj.(*extensions.Job) object := obj.(*extensions.Job)
object.Spec.Selector = map[string]string{} object.Spec.Selector = &extensions.PodSelector{}
return object return object
}, },
func(obj runtime.Object) runtime.Object { func(obj runtime.Object) runtime.Object {

View File

@ -32,10 +32,12 @@ func TestJobStrategy(t *testing.T) {
t.Errorf("Job should not allow create on update") t.Errorf("Job should not allow create on update")
} }
validSelector := map[string]string{"a": "b"} validSelector := &extensions.PodSelector{
MatchLabels: map[string]string{"a": "b"},
}
validPodTemplateSpec := api.PodTemplateSpec{ validPodTemplateSpec := api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Labels: validSelector, Labels: validSelector.MatchLabels,
}, },
Spec: api.PodSpec{ Spec: api.PodSpec{
RestartPolicy: api.RestartPolicyOnFailure, RestartPolicy: api.RestartPolicyOnFailure,
@ -95,10 +97,12 @@ func TestJobStatusStrategy(t *testing.T) {
if StatusStrategy.AllowCreateOnUpdate() { if StatusStrategy.AllowCreateOnUpdate() {
t.Errorf("Job should not allow create on update") t.Errorf("Job should not allow create on update")
} }
validSelector := map[string]string{"a": "b"} validSelector := &extensions.PodSelector{
MatchLabels: map[string]string{"a": "b"},
}
validPodTemplateSpec := api.PodTemplateSpec{ validPodTemplateSpec := api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Labels: validSelector, Labels: validSelector.MatchLabels,
}, },
Spec: api.PodSpec{ Spec: api.PodSpec{
RestartPolicy: api.RestartPolicyOnFailure, RestartPolicy: api.RestartPolicyOnFailure,