-
Notifications
You must be signed in to change notification settings - Fork 0
/
informer.go
154 lines (140 loc) · 3.87 KB
/
informer.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/util/taints"
)
type watcher struct {
nodes []string
restarter chan struct{}
clientSet *kubernetes.Clientset
nodeInformer *cache.Controller
config *Config
}
func NewWatcher(clientSet *kubernetes.Clientset, cfg *Config) *watcher {
return &watcher{
clientSet: clientSet,
config: cfg,
}
}
func (w *watcher) Run() {
podInformer := w.NewPodInformer("kube-system")
stop := make(chan struct{})
podInformer.Run(stop)
}
func (w *watcher) NewPodInformer(namespace string) cache.Controller {
podWatchList := cache.NewListWatchFromClient(
w.clientSet.CoreV1().RESTClient(),
"pods",
namespace,
fields.Everything(),
)
_, controller := cache.NewInformer(
podWatchList,
&v1.Pod{},
time.Second*0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pod := obj.(*v1.Pod)
log.Infow("new pod detected",
"podName", pod.Name,
"namespace", namespace,
)
},
DeleteFunc: func(obj interface{}) {
pod := obj.(*v1.Pod)
log.Infow("pod has been deleted",
"podName", pod.Name,
"namespace", namespace,
)
},
// TODO: Move this into its own function.
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
oldPod := oldObj.(*v1.Pod)
newPod := newObj.(*v1.Pod)
log.Infow("pod has changed status phase",
"podName", newPod.Name,
"namespace", namespace,
"oldPhase", oldPod.Status.Phase,
"newPhase", newPod.Status.Phase,
"nodeName", newPod.Spec.NodeName,
)
owner := newPod.GetOwnerReferences()
for _, ds := range cfg.DaemonSets {
log.Debugw("looping through daemonset",
"namespace", ds.Namespace,
"name", ds.Name,
)
for _, ownerRef := range owner {
log.Debugw("looping through owner",
"podName", newPod.Name,
"dsName", ownerRef.Name,
)
// OwnerReference does not expose namespace
// https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#OwnerReference
if ds.Namespace == namespace && ownerRef.Kind == "DaemonSet" && ownerRef.Name == ds.Name {
var err error
if newPod.Status.Phase != "Running" {
log.Debugw("tainting node",
"podName", newPod.Name,
"podPhase", newPod.Status.Phase,
"nodeName", newPod.Spec.NodeName,
)
_, err = w.ChangeNodeTaint(newPod.Spec.NodeName, true)
} else {
log.Debugw("ensuring node is not tainted",
"podName", newPod.Name,
"podPhase", newPod.Status.Phase,
"nodeName", newPod.Spec.NodeName,
)
_, err = w.ChangeNodeTaint(newPod.Spec.NodeName, false)
}
if err != nil {
log.Errorw("unable to taint node",
"podName", newPod.Name,
"nodeName", newPod.Spec.NodeName,
"error", err.Error(),
)
}
}
}
}
},
},
)
return controller
}
func (w *watcher) ChangeNodeTaint(nodeName string, wantTaint bool) (*v1.Node, error) {
node, err := w.clientSet.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
if err != nil {
return nil, err
}
var taintEffect v1.TaintEffect
if w.config.Evict {
taintEffect = v1.TaintEffectNoExecute
} else {
taintEffect = v1.TaintEffectNoSchedule
}
if wantTaint {
taint := &v1.Taint{
Key: "taint.davyjones.github.com",
Value: "true",
Effect: taintEffect,
}
node, _, err := taints.AddOrUpdateTaint(node, taint)
if err != nil {
return node, err
}
_, err = w.clientSet.CoreV1().Nodes().Update(node)
return node, err
}
taintList := node.Spec.Taints
taintList, _ = taints.DeleteTaintsByKey(taintList, "taint.davyjones.github.com")
node.Spec.Taints = taintList
_, err = w.clientSet.CoreV1().Nodes().Update(node)
return node, err
}