@@ -5,10 +5,11 @@ import (
5
5
"time"
6
6
7
7
batchv1 "k8s.io/api/batch/v1"
8
+ corev1 "k8s.io/api/core/v1"
8
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
10
)
10
11
11
- func createJob (ownedByCronJob bool , completed time.Time , active , succeeded , failed int32 ) * batchv1.Job {
12
+ func createJob (ownedByCronJob bool , completed time.Time , active , succeeded , failed int32 , conditions []batchv1. JobCondition ) * batchv1.Job {
12
13
ts := metav1 .NewTime (completed )
13
14
job := batchv1.Job {
14
15
Spec : batchv1.JobSpec {},
@@ -17,6 +18,7 @@ func createJob(ownedByCronJob bool, completed time.Time, active, succeeded, fail
17
18
Active : active ,
18
19
Succeeded : succeeded ,
19
20
Failed : failed ,
21
+ Conditions : conditions ,
20
22
},
21
23
}
22
24
if ownedByCronJob {
@@ -39,54 +41,100 @@ func TestKleaner_DeleteJob(t *testing.T) {
39
41
expected bool
40
42
}{
41
43
"jobs owned by cronjobs should be ignored" : {
42
- jobSpec : createJob (true , ts .Add (- time .Minute ), 0 , 0 , 0 ),
44
+ jobSpec : createJob (true , ts .Add (- time .Minute ), 0 , 0 , 0 , []batchv1. JobCondition {} ),
43
45
successful : time .Second ,
44
46
failed : time .Second ,
45
47
ignoreCron : true ,
46
48
expected : false ,
47
49
},
48
50
"jobs owned by cronjobs should be deleted" : {
49
- jobSpec : createJob (true , ts .Add (- time .Minute ), 0 , 0 , 0 ),
51
+ jobSpec : createJob (true , ts .Add (- time .Minute ), 0 , 0 , 0 , []batchv1. JobCondition {} ),
50
52
successful : time .Second ,
51
53
failed : time .Second ,
52
54
ignoreCron : false ,
53
55
expected : false ,
54
56
},
55
57
"jobs with active pods should not be deleted" : {
56
- jobSpec : createJob (false , ts .Add (- time .Minute ), 1 , 0 , 0 ), // job.Status.Active > 0
58
+ jobSpec : createJob (false , ts .Add (- time .Minute ), 1 , 0 , 0 , []batchv1. JobCondition {} ), // job.Status.Active > 0
57
59
successful : time .Second ,
58
60
failed : time .Second ,
59
61
ignoreCron : false ,
60
62
expected : false ,
61
63
},
62
64
"expired successful jobs should be deleted" : {
63
- jobSpec : createJob (false , ts .Add (- time .Minute ), 0 , 1 , 0 ),
65
+ jobSpec : createJob (false , ts .Add (- time .Minute ), 0 , 1 , 0 , []batchv1. JobCondition {} ),
64
66
successful : time .Second ,
65
67
failed : time .Second ,
66
68
ignoreCron : false ,
67
69
expected : true ,
68
70
},
69
71
"non-expired successful jobs should not be deleted" : {
70
- jobSpec : createJob (false , ts .Add (- time .Minute ), 0 , 1 , 0 ),
72
+ jobSpec : createJob (false , ts .Add (- time .Minute ), 0 , 1 , 0 , []batchv1. JobCondition {} ),
71
73
successful : time .Minute * 2 ,
72
74
failed : time .Second ,
73
75
ignoreCron : false ,
74
76
expected : false ,
75
77
},
76
78
"expired failed jobs should be deleted" : {
77
- jobSpec : createJob (false , ts .Add (- time .Minute ), 0 , 0 , 1 ),
79
+ jobSpec : createJob (false , ts .Add (- time .Minute ), 0 , 0 , 1 , []batchv1. JobCondition {} ),
78
80
successful : time .Second ,
79
81
failed : time .Second ,
80
82
ignoreCron : false ,
81
83
expected : true ,
82
84
},
83
85
"non-expired failed jobs should not be deleted" : {
84
- jobSpec : createJob (false , ts .Add (- time .Minute ), 0 , 0 , 1 ),
86
+ jobSpec : createJob (false , ts .Add (- time .Minute ), 0 , 0 , 1 , []batchv1. JobCondition {} ),
85
87
successful : time .Second ,
86
88
failed : time .Minute * 2 ,
87
89
ignoreCron : false ,
88
90
expected : false ,
89
91
},
92
+ "failed (based on JobCondition) but not marked as failed jobs should be deleted" : {
93
+ jobSpec : createJob (false , time.Time {}, 0 , 0 , 0 , []batchv1.JobCondition {
94
+ batchv1.JobCondition {
95
+ Type : batchv1 .JobFailed ,
96
+ Status : corev1 .ConditionTrue ,
97
+ LastProbeTime : metav1 .NewTime (ts ),
98
+ LastTransitionTime : metav1 .NewTime (ts .Add (- time .Minute )),
99
+ Reason : "DeadlineExceeded" ,
100
+ Message : "Job was active longer than specified deadline" ,
101
+ },
102
+ }),
103
+ successful : time .Second ,
104
+ failed : time .Second ,
105
+ ignoreCron : false ,
106
+ expected : true ,
107
+ },
108
+ "successful but 'active' jobs should be deleted" : {
109
+ jobSpec : createJob (false , ts .Add (- time .Minute ), 1 , 1 , 0 , []batchv1.JobCondition {}),
110
+ successful : time .Second ,
111
+ failed : time .Second ,
112
+ ignoreCron : false ,
113
+ expected : true ,
114
+ },
115
+ "failed but 'active' jobs should be deleted" : {
116
+ jobSpec : createJob (false , ts .Add (- time .Minute ), 1 , 0 , 1 , []batchv1.JobCondition {}),
117
+ successful : time .Second ,
118
+ failed : time .Second ,
119
+ ignoreCron : false ,
120
+ expected : true ,
121
+ },
122
+ "failed (based on JobCondition) but 'active' jobs should be deleted" : {
123
+ jobSpec : createJob (false , ts .Add (- time .Minute ), 1 , 0 , 0 , []batchv1.JobCondition {
124
+ batchv1.JobCondition {
125
+ Type : batchv1 .JobFailed ,
126
+ Status : corev1 .ConditionTrue ,
127
+ LastProbeTime : metav1 .NewTime (ts ),
128
+ LastTransitionTime : metav1 .NewTime (ts .Add (- time .Minute )),
129
+ Reason : "DeadlineExceeded" ,
130
+ Message : "Job was active longer than specified deadline" ,
131
+ },
132
+ }),
133
+ successful : time .Second ,
134
+ failed : time .Second ,
135
+ ignoreCron : false ,
136
+ expected : true ,
137
+ },
90
138
}
91
139
for name , tc := range testCases {
92
140
t .Run (name , func (t * testing.T ) {
0 commit comments