mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Merge pull request #107997 from d-honeybadger/fix-tracking-cronjob-owned-jobs
Fix cronjob status reconciliation when job template labels change
This commit is contained in:
commit
343125cc6c
@ -243,13 +243,9 @@ func (jm *ControllerV2) resolveControllerRef(namespace string, controllerRef *me
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jm *ControllerV2) getJobsToBeReconciled(cronJob *batchv1.CronJob) ([]*batchv1.Job, error) {
|
func (jm *ControllerV2) getJobsToBeReconciled(cronJob *batchv1.CronJob) ([]*batchv1.Job, error) {
|
||||||
var jobSelector labels.Selector
|
// list all jobs: there may be jobs with labels that don't match the template anymore,
|
||||||
if len(cronJob.Spec.JobTemplate.Labels) == 0 {
|
// but that still have a ControllerRef to the given cronjob
|
||||||
jobSelector = labels.Everything()
|
jobList, err := jm.jobLister.Jobs(cronJob.Namespace).List(labels.Everything())
|
||||||
} else {
|
|
||||||
jobSelector = labels.Set(cronJob.Spec.JobTemplate.Labels).AsSelector()
|
|
||||||
}
|
|
||||||
jobList, err := jm.jobLister.Jobs(cronJob.Namespace).List(jobSelector)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ import (
|
|||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
|
|
||||||
batchv1 "k8s.io/api/batch/v1"
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
@ -1126,6 +1126,36 @@ func TestControllerV2GetJobsToBeReconciled(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expected: []*batchv1.Job{},
|
expected: []*batchv1.Job{},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "test getting jobs whose labels do not match job template",
|
||||||
|
cronJob: &batchv1.CronJob{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Namespace: "foo-ns", Name: "fooer"},
|
||||||
|
Spec: batchv1.CronJobSpec{JobTemplate: batchv1.JobTemplateSpec{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"key": "value"}},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
jobs: []runtime.Object{
|
||||||
|
&batchv1.Job{ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Namespace: "foo-ns",
|
||||||
|
Name: "foo-fooer-owner-ref",
|
||||||
|
Labels: map[string]string{"key": "different-value"},
|
||||||
|
OwnerReferences: []metav1.OwnerReference{{Name: "fooer", Controller: &trueRef}}},
|
||||||
|
},
|
||||||
|
&batchv1.Job{ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Namespace: "foo-ns",
|
||||||
|
Name: "foo-other-owner-ref",
|
||||||
|
Labels: map[string]string{"key": "different-value"},
|
||||||
|
OwnerReferences: []metav1.OwnerReference{{Name: "another-cronjob", Controller: &trueRef}}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []*batchv1.Job{{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Namespace: "foo-ns",
|
||||||
|
Name: "foo-fooer-owner-ref",
|
||||||
|
Labels: map[string]string{"key": "different-value"},
|
||||||
|
OwnerReferences: []metav1.OwnerReference{{Name: "fooer", Controller: &trueRef}}},
|
||||||
|
}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user