@@ -10,9 +10,9 @@ import (
10
10
"k8s.io/client-go/tools/cache"
11
11
"log"
12
12
"reflect"
13
+ "strconv"
13
14
"sync"
14
15
"time"
15
- "strconv"
16
16
)
17
17
18
18
// PodController watches the kubernetes api for changes to Pods and
@@ -40,8 +40,8 @@ func NewPodController(kclient *kubernetes.Clientset, opts map[string]string) *Po
40
40
podWatcher := & PodController {}
41
41
42
42
keepSuccessHours , _ := strconv .Atoi (opts ["keepSuccessHours" ])
43
- keepFailedHours , _ := strconv .Atoi (opts ["keepFailedHours" ])
44
- dryRun , _ := strconv .ParseBool (opts ["dryRun" ])
43
+ keepFailedHours , _ := strconv .Atoi (opts ["keepFailedHours" ])
44
+ dryRun , _ := strconv .ParseBool (opts ["dryRun" ])
45
45
// Create informer for watching Namespaces
46
46
podInformer := cache .NewSharedIndexInformer (
47
47
& cache.ListWatch {
@@ -101,13 +101,13 @@ func (c *PodController) doTheMagic(cur interface{}, keepSuccessHours int, keepFa
101
101
102
102
executionTimeHours := c .getExecutionTimeHours (podObj )
103
103
log .Printf ("Checking pod %s with %s status that was executed %f hours ago" , podObj .Name , podObj .Status .Phase , executionTimeHours )
104
- switch podObj .Status .Phase {
104
+ switch podObj .Status .Phase {
105
105
case v1 .PodSucceeded :
106
- if keepSuccessHours == 0 || (keepSuccessHours > 0 && executionTimeHours > float32 (keepSuccessHours )){
106
+ if keepSuccessHours == 0 || (keepSuccessHours > 0 && executionTimeHours > float32 (keepSuccessHours )) {
107
107
c .deleteObjects (podObj , createdMeta , dryRun )
108
108
}
109
109
case v1 .PodFailed :
110
- if keepFailedHours == 0 || (keepFailedHours > 0 && executionTimeHours > float32 (keepFailedHours )){
110
+ if keepFailedHours == 0 || (keepFailedHours > 0 && executionTimeHours > float32 (keepFailedHours )) {
111
111
c .deleteObjects (podObj , createdMeta , dryRun )
112
112
}
113
113
default :
@@ -116,14 +116,14 @@ func (c *PodController) doTheMagic(cur interface{}, keepSuccessHours int, keepFa
116
116
}
117
117
118
118
// method to calcualte the hours that passed since the pod's excecution end time
119
- func (c * PodController ) getExecutionTimeHours (podObj * v1.Pod ) (executionTimeHours float32 ){
119
+ func (c * PodController ) getExecutionTimeHours (podObj * v1.Pod ) (executionTimeHours float32 ) {
120
120
executionTimeHours = 0.0
121
121
currentUnixTime := time .Now ().Unix ()
122
122
podConditions := podObj .Status .Conditions
123
123
var pc v1.PodCondition
124
- for _ , pc = range podConditions {
124
+ for _ , pc = range podConditions {
125
125
// Looking for the time when pod's condition "Ready" became "false" (equals end of execution)
126
- if pc .Type == v1 .PodReady && pc .Status == v1 .ConditionFalse {
126
+ if pc .Type == v1 .PodReady && pc .Status == v1 .ConditionFalse {
127
127
executionTimeUnix := pc .LastTransitionTime .Unix ()
128
128
executionTimeHours = (float32 (currentUnixTime ) - float32 (executionTimeUnix )) / float32 (3600 )
129
129
}
@@ -138,15 +138,15 @@ func (c *PodController) deleteObjects(podObj *v1.Pod, createdMeta CreatedByAnnot
138
138
log .Printf ("Deleting pod '%s'" , podObj .Name )
139
139
var po metav1.DeleteOptions
140
140
c .kclient .CoreV1 ().Pods (podObj .Namespace ).Delete (podObj .Name , & po )
141
- }else {
141
+ } else {
142
142
log .Printf ("Pod '%s' would have been deleted" , podObj .Name )
143
143
}
144
144
// Delete Job itself
145
145
if ! dryRun {
146
146
log .Printf ("Deleting job '%s'" , createdMeta .Reference .Name )
147
147
var jo metav1.DeleteOptions
148
148
c .kclient .BatchV1Client .Jobs (createdMeta .Reference .Namespace ).Delete (createdMeta .Reference .Name , & jo )
149
- } else {
149
+ } else {
150
150
log .Printf ("Job '%s' would have been deleted" , createdMeta .Reference .Name )
151
151
}
152
152
return
0 commit comments