-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
82 lines (70 loc) · 1.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"os"
)
const MainContainerAnnotation = "k8s-job-sidekiller.killmeplz.github.com/main-container"
var LoginMethod = os.Getenv("AUTH_METHOD")
var Namespace = os.Getenv("POD_NAMESPACE")
var KubeConfig = kubeconfig(LoginMethod)
func main() {
clientSet, err := kubernetes.NewForConfig(KubeConfig)
if err != nil {
panic(err.Error())
}
//channel to synchronize goroutines
stopCh := make(chan struct{})
defer close(stopCh)
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
informer := cache.NewSharedIndexInformer(
listWatchFuncs(clientSet),
&v1.Pod{},
0,
cache.Indexers{},
)
informer.AddEventHandler(handlerFuncs(queue))
listener := Listener{
informer: informer,
queue: queue,
handler: NewShutdownKiller(),
}
listener.Run(stopCh)
}
func listWatchFuncs(c *kubernetes.Clientset) *cache.ListWatch {
return &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return c.CoreV1().Pods(Namespace).List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return c.CoreV1().Pods(Namespace).Watch(options)
},
}
}
func handlerFuncs(q workqueue.RateLimitingInterface) *cache.ResourceEventHandlerFuncs {
return &cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
q.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
q.Add(key)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(newObj)
if err == nil {
q.Add(key)
}
},
}
}