Skip to content

Commit 1183f6f

Browse files
committed
go fmt, update README, more flexible makefile
1 parent 1235337 commit 1183f6f

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
NAME:= kube-cleanup-operator
1+
NAME := kube-cleanup-operator
22
AUTHOR=lwolf
3-
VERSION=0.3
4-
REGISTRY := quay.io
3+
VERSION ?= 0.3
4+
REGISTRY ?= quay.io
55
GIT_SHA=$(shell git --no-pager describe --always --dirty)
66
BUILD_TIME=$(shell date '+%s')
77
LFLAGS ?= -X main.gitsha=${GIT_SHA} -X main.compiled=${BUILD_TIME}

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Controller listens for changes in Pods created by Jobs and deletes it on Complet
77
Some defaults:
88
* All Namespaces are monitored by default
99
* Only Pods created by Jobs are monitored
10-
* Only Pods in Completed state with 0 restarts are deleted
1110

1211
## Usage
1312

cmd/main.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
"github.com/lwolf/kube-cleanup-operator/pkg/controller"
1313
"k8s.io/client-go/kubernetes"
14-
"k8s.io/client-go/tools/clientcmd"
1514
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
15+
"k8s.io/client-go/tools/clientcmd"
1616
"strconv"
1717
)
1818

@@ -42,12 +42,12 @@ func main() {
4242
}
4343

4444
options := map[string]string{
45-
"namespace": *namespace,
45+
"namespace": *namespace,
4646
"keepSuccessHours": strconv.Itoa(*keepSuccessHours),
47-
"keepFailedHours": strconv.Itoa(*keepFailedHours),
48-
"dryRun": strconv.FormatBool(*dryRun),
47+
"keepFailedHours": strconv.Itoa(*keepFailedHours),
48+
"dryRun": strconv.FormatBool(*dryRun),
4949
}
50-
if (*dryRun){
50+
if *dryRun {
5151
log.Println("Performing dry run...")
5252
}
5353
log.Printf("Configured namespace: '%s', keepSuccessHours: %d, keepFailedHours: %d", options["namespace"], *keepSuccessHours, *keepFailedHours)
@@ -82,4 +82,4 @@ func newClientSet(runOutsideCluster bool) (*kubernetes.Clientset, error) {
8282
}
8383

8484
return kubernetes.NewForConfig(config)
85-
}
85+
}

pkg/controller/controller.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"k8s.io/client-go/tools/cache"
1111
"log"
1212
"reflect"
13+
"strconv"
1314
"sync"
1415
"time"
15-
"strconv"
1616
)
1717

1818
// PodController watches the kubernetes api for changes to Pods and
@@ -40,8 +40,8 @@ func NewPodController(kclient *kubernetes.Clientset, opts map[string]string) *Po
4040
podWatcher := &PodController{}
4141

4242
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"])
4545
// Create informer for watching Namespaces
4646
podInformer := cache.NewSharedIndexInformer(
4747
&cache.ListWatch{
@@ -101,13 +101,13 @@ func (c *PodController) doTheMagic(cur interface{}, keepSuccessHours int, keepFa
101101

102102
executionTimeHours := c.getExecutionTimeHours(podObj)
103103
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 {
105105
case v1.PodSucceeded:
106-
if keepSuccessHours == 0 || (keepSuccessHours > 0 && executionTimeHours > float32(keepSuccessHours)){
106+
if keepSuccessHours == 0 || (keepSuccessHours > 0 && executionTimeHours > float32(keepSuccessHours)) {
107107
c.deleteObjects(podObj, createdMeta, dryRun)
108108
}
109109
case v1.PodFailed:
110-
if keepFailedHours == 0 || (keepFailedHours > 0 && executionTimeHours > float32(keepFailedHours)){
110+
if keepFailedHours == 0 || (keepFailedHours > 0 && executionTimeHours > float32(keepFailedHours)) {
111111
c.deleteObjects(podObj, createdMeta, dryRun)
112112
}
113113
default:
@@ -116,14 +116,14 @@ func (c *PodController) doTheMagic(cur interface{}, keepSuccessHours int, keepFa
116116
}
117117

118118
// 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) {
120120
executionTimeHours = 0.0
121121
currentUnixTime := time.Now().Unix()
122122
podConditions := podObj.Status.Conditions
123123
var pc v1.PodCondition
124-
for _, pc = range podConditions{
124+
for _, pc = range podConditions {
125125
// 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 {
127127
executionTimeUnix := pc.LastTransitionTime.Unix()
128128
executionTimeHours = (float32(currentUnixTime) - float32(executionTimeUnix)) / float32(3600)
129129
}
@@ -138,15 +138,15 @@ func (c *PodController) deleteObjects(podObj *v1.Pod, createdMeta CreatedByAnnot
138138
log.Printf("Deleting pod '%s'", podObj.Name)
139139
var po metav1.DeleteOptions
140140
c.kclient.CoreV1().Pods(podObj.Namespace).Delete(podObj.Name, &po)
141-
}else{
141+
} else {
142142
log.Printf("Pod '%s' would have been deleted", podObj.Name)
143143
}
144144
// Delete Job itself
145145
if !dryRun {
146146
log.Printf("Deleting job '%s'", createdMeta.Reference.Name)
147147
var jo metav1.DeleteOptions
148148
c.kclient.BatchV1Client.Jobs(createdMeta.Reference.Namespace).Delete(createdMeta.Reference.Name, &jo)
149-
} else{
149+
} else {
150150
log.Printf("Job '%s' would have been deleted", createdMeta.Reference.Name)
151151
}
152152
return

0 commit comments

Comments
 (0)