Skip to content

Commit 88c8bcb

Browse files
authored
Merge pull request kubernetes#115952 from pacoxu/cleanup-cronjob
cronjob: return immediately when failed to create job for the namespace is terminating
2 parents 8c1dc65 + ade63dd commit 88c8bcb

File tree

3 files changed

+24
-43
lines changed

3 files changed

+24
-43
lines changed

pkg/controller/cronjob/cronjob_controllerv2.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@ func (jm *ControllerV2) syncCronJob(
601601
jobResp, err := jm.jobControl.CreateJob(cronJob.Namespace, jobReq)
602602
switch {
603603
case errors.HasStatusCause(err, corev1.NamespaceTerminatingCause):
604+
// if the namespace is being terminated, we don't have to do
605+
// anything because any creation will fail
606+
return cronJob, nil, updateStatus, err
604607
case errors.IsAlreadyExists(err):
605608
// If the job is created by other actor, assume it has updated the cronjob status accordingly
606609
logger.Info("Job already exists", "cronjob", klog.KObj(cronJob), "job", klog.KObj(jobReq))

pkg/controller/cronjob/cronjob_controllerv2_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cronjob
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"reflect"
2223
"strings"
2324
"testing"
@@ -1157,6 +1158,26 @@ func TestControllerV2SyncCronJob(t *testing.T) {
11571158
expectUpdateStatus: true,
11581159
jobPresentInCJActiveStatus: true,
11591160
},
1161+
"do nothing if the namespace is terminating": {
1162+
jobCreateError: &errors.StatusError{ErrStatus: metav1.Status{Details: &metav1.StatusDetails{Causes: []metav1.StatusCause{
1163+
{
1164+
Type: v1.NamespaceTerminatingCause,
1165+
Message: fmt.Sprintf("namespace %s is being terminated", metav1.NamespaceDefault),
1166+
Field: "metadata.namespace",
1167+
}}}}},
1168+
concurrencyPolicy: "Allow",
1169+
schedule: onTheHour,
1170+
deadline: noDead,
1171+
ranPreviously: true,
1172+
stillActive: true,
1173+
jobCreationTime: justAfterThePriorHour(),
1174+
now: *justAfterTheHour(),
1175+
expectActive: 0,
1176+
expectRequeueAfter: false,
1177+
expectUpdateStatus: false,
1178+
expectErr: true,
1179+
jobPresentInCJActiveStatus: false,
1180+
},
11601181
}
11611182
for name, tc := range testCases {
11621183
name := name

pkg/controller/cronjob/injection.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"k8s.io/apimachinery/pkg/api/errors"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/runtime/schema"
27-
"k8s.io/apimachinery/pkg/types"
2827
clientset "k8s.io/client-go/kubernetes"
2928
"k8s.io/client-go/tools/record"
3029
)
@@ -84,10 +83,6 @@ type jobControlInterface interface {
8483
GetJob(namespace, name string) (*batchv1.Job, error)
8584
// CreateJob creates new Jobs according to the spec.
8685
CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error)
87-
// UpdateJob updates a Job.
88-
UpdateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error)
89-
// PatchJob patches a Job.
90-
PatchJob(namespace string, name string, pt types.PatchType, data []byte, subresources ...string) (*batchv1.Job, error)
9186
// DeleteJob deletes the Job identified by name.
9287
// TODO: delete by UID?
9388
DeleteJob(namespace string, name string) error
@@ -105,14 +100,6 @@ func (r realJobControl) GetJob(namespace, name string) (*batchv1.Job, error) {
105100
return r.KubeClient.BatchV1().Jobs(namespace).Get(context.TODO(), name, metav1.GetOptions{})
106101
}
107102

108-
func (r realJobControl) UpdateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
109-
return r.KubeClient.BatchV1().Jobs(namespace).Update(context.TODO(), job, metav1.UpdateOptions{})
110-
}
111-
112-
func (r realJobControl) PatchJob(namespace string, name string, pt types.PatchType, data []byte, subresources ...string) (*batchv1.Job, error) {
113-
return r.KubeClient.BatchV1().Jobs(namespace).Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
114-
}
115-
116103
func (r realJobControl) CreateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
117104
return r.KubeClient.BatchV1().Jobs(namespace).Create(context.TODO(), job, metav1.CreateOptions{})
118105
}
@@ -156,28 +143,6 @@ func (f *fakeJobControl) GetJob(namespace, name string) (*batchv1.Job, error) {
156143
return f.Job, nil
157144
}
158145

159-
func (f *fakeJobControl) UpdateJob(namespace string, job *batchv1.Job) (*batchv1.Job, error) {
160-
f.Lock()
161-
defer f.Unlock()
162-
if f.Err != nil {
163-
return nil, f.Err
164-
}
165-
f.UpdateJobName = append(f.UpdateJobName, job.Name)
166-
return job, nil
167-
}
168-
169-
func (f *fakeJobControl) PatchJob(namespace string, name string, pt types.PatchType, data []byte, subresources ...string) (*batchv1.Job, error) {
170-
f.Lock()
171-
defer f.Unlock()
172-
if f.Err != nil {
173-
return nil, f.Err
174-
}
175-
f.PatchJobName = append(f.PatchJobName, name)
176-
f.Patches = append(f.Patches, data)
177-
// We don't have anything to return. Just return something non-nil.
178-
return &batchv1.Job{}, nil
179-
}
180-
181146
func (f *fakeJobControl) DeleteJob(namespace string, name string) error {
182147
f.Lock()
183148
defer f.Unlock()
@@ -187,11 +152,3 @@ func (f *fakeJobControl) DeleteJob(namespace string, name string) error {
187152
f.DeleteJobName = append(f.DeleteJobName, name)
188153
return nil
189154
}
190-
191-
func (f *fakeJobControl) Clear() {
192-
f.Lock()
193-
defer f.Unlock()
194-
f.DeleteJobName = []string{}
195-
f.Jobs = []batchv1.Job{}
196-
f.Err = nil
197-
}

0 commit comments

Comments
 (0)