-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
295 lines (251 loc) · 12.5 KB
/
main.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import sys
import argparse
import datetime
from operator import itemgetter, attrgetter
from itertools import groupby
from typing import List
from humanfriendly import parse_timespan, format_timespan
import requests
from kubernetes import client, config
def parse_args(argv=sys.argv[1:]):
p = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=(
"Automatically cordon old worker nodes, send notifications about pending evictions and finally evict pods from "
"cordoned old worker nodes"
))
p.add_argument('--dry-run', default=False, action='store_true',
help="don't perform any changes, just print what would have been done")
p.add_argument('--nodes', default=None, nargs='+', type=str, metavar='NODE',
help='act on the given nodes only')
p.add_argument('--cordon-nodes-after', default='30d',
help='cordon nodes as old after they have been up for the given amount of time')
p.add_argument('--notify-after', default='1d',
help=(
"send notifications about soon-to-be-drained nodes and the pods running on them after they have "
"been cordoned for the given amount of time; needs to be smaller than --evict-after)"))
p.add_argument('--evict-after', default='2d',
help=(
"start evicting pods from nodes after they given amount of time after notifications have been "
"sent"))
group = p.add_argument_group('slack notification settings')
group.add_argument('--slack-webhook')
group.add_argument('--slack-username', default=None)
group.add_argument('--slack-icon', default=None)
group.add_argument('--slack-default-message',
default=(
"*Heads up <!everyone>*\n"
"The node(s) running the following pods have been cordoned and will be gracefully drained soon. "
"The pods will be evicted at the given time. Make sure to define pod disruption budgets to prevent "
"service downtime."),
help='default message that will appear in slack notification')
group.add_argument('--slack-additional-message', default=None,
help='will be added after the default message')
group.add_argument('--slack-admin-contact',
help=(
"if given, this slack contact will be included in the notification messages to direct "
"people to the right team or person"))
group.add_argument('--slack-target',
help='target for sending slack notifications about any actions performed by this program')
group.add_argument('--slack-target-annotation',
help=(
"annotation key to look for on pods and the namespaces of pods that are running on nodes "
"that are to be drained soon; the value of the annotation is used as a target for sending "
"slack notifications about the actions performed by this program that affect the nodes that "
"the pods are running on"))
args = p.parse_args(argv)
args.cordon_nodes_after = datetime.timedelta(seconds=parse_timespan(args.cordon_nodes_after))
args.notify_after = datetime.timedelta(seconds=parse_timespan(args.notify_after))
args.evict_after = datetime.timedelta(seconds=parse_timespan(args.evict_after))
if args.notify_after >= args.evict_after:
print('--notify-after needs to be smaller than --evict-after')
sys.exit(1)
return args
def annotation(key: str):
return 'node-drainer.k8s.logmein.com/' + key
def generate_action_plan(all_nodes: List[client.V1Node], all_namespaces: List[client.V1Namespace], all_pods, args):
actions = {
'cordon': {
'nodes': [],
'affected_pods': []
},
'notify': {
'nodes': [],
'affected_pods': []
},
'drain': {
'nodes': [],
'affected_pods': []
},
}
namespace_annotations = {ns.metadata.name: (ns.metadata.annotations or {}) for ns in all_namespaces}
now = datetime.datetime.utcnow().replace(microsecond=0)
for node in all_nodes:
if node.metadata.annotations.get(annotation('ignored')) is not None:
continue
cordoned = (args.dry_run or node.spec.unschedulable) and node.metadata.annotations.get(annotation('cordoned')) is not None
notifications_sent_at = node.metadata.annotations.get(annotation('notifications-sent'))
if notifications_sent_at is not None:
try:
notifications_sent_at = datetime.datetime.fromtimestamp(int(notifications_sent_at))
except:
print(f"Failed parsing timestamp of notifications-sent annotation")
notifications_sent_at = now + args.notify_after
action = None
eviction_time = None
cordon_at = node.metadata.creation_timestamp.replace(tzinfo=None) + args.cordon_nodes_after
if notifications_sent_at is not None:
if notifications_sent_at + args.evict_after < now:
action = 'drain'
print(
f"Node {node.metadata.name} was already cordoned and notifications had been sent. "
"It will be drained now.")
elif cordoned:
if args.dry_run:
cordoned_at = cordon_at
else:
unschedulable_taint = next(filter(
lambda taint: taint.key == 'node.kubernetes.io/unschedulable',
node.spec.taints))
cordoned_at = unschedulable_taint.time_added.replace(tzinfo=None)
if cordoned_at + args.notify_after < now:
action = 'notify'
eviction_time = f"{format_timespan(args.evict_after)} from now"
print(
f"Node {node.metadata.name} was already cordoned and will be drained {eviction_time}. "
"Notifications will be sent now.")
elif not node.spec.unschedulable and cordon_at < now:
action = 'cordon'
print(
f"Node {node.metadata.name} is older than {args.cordon_nodes_after} and will be cordoned now. "
f"Notifications will be sent in {format_timespan(args.notify_after)}.")
if action is None:
continue
pods = [pod for pod in all_pods if pod.spec.node_name == node.metadata.name]
if len(pods) > 0:
print(" Pods running on this instance:")
# group pods by namespace
keyfunc = attrgetter('metadata.namespace')
pods = sorted(pods, key=keyfunc)
pods_by_namespace = groupby(pods, key=keyfunc)
for namespace, pods in pods_by_namespace:
print(f" Namespace: {namespace}")
for pod in pods:
print(f" {pod.metadata.name}")
actions[action]['affected_pods'].append({
'namespace': pod.metadata.namespace,
'name': pod.metadata.name,
'annotations': {
**namespace_annotations[pod.metadata.namespace],
**(pod.metadata.annotations or {})
},
'eviction_time': eviction_time
})
actions[action]['nodes'].append(node.metadata.name)
return actions
def notify(affected_pods, args):
"""
Sends notification to the owners of the given list of affected pods.
"""
if args.slack_webhook and (args.slack_target or args.slack_target_annotation):
pods_by_slack_target = {}
if args.slack_target_annotation:
for pod in affected_pods:
value = pod['annotations'].get(args.slack_target_annotation, None)
if value is None:
continue
targets = value.split(',')
for target in targets:
if target not in pods_by_slack_target.keys():
pods_by_slack_target[target] = []
pods_by_slack_target[target].append(pod)
if args.slack_target:
pods_by_slack_target[args.slack_target] = affected_pods
for target, pods_for_target in pods_by_slack_target.items():
if len(pods_for_target) == 0:
continue
keyfunc = itemgetter('eviction_time')
pods_by_eviction_time = groupby(sorted(pods_for_target, key=keyfunc), key=keyfunc)
attachments = []
for eviction_time, pods_for_eviction_time in pods_by_eviction_time:
keyfunc = itemgetter('namespace')
pods_by_namespace = groupby(sorted(pods_for_eviction_time, key=keyfunc), key=keyfunc)
text = ''
for namespace, pods in pods_by_namespace:
text += f"*Namespace:* {namespace}\n"
for pod in pods:
text += f"• `{pod['name']}`\n"
attachments.append({
'mrkdwn_in': ['text', 'pretext'],
'pretext': f"*Eviction:* {eviction_time}",
'text': text
})
text = args.slack_default_message
if args.slack_additional_message:
text += "\n" + args.slack_additional_message
if args.slack_admin_contact and args.slack_admin_contact != target:
text += f"\nQuestions regarding this message? Contact <{args.slack_admin_contact}>"
requests.post(args.slack_webhook, json={
'username': args.slack_username,
'icon_emoji': args.slack_icon,
'channel': target,
'text': text,
'attachments': attachments,
})
def drain_node(v1, node: client.V1Node, all_pods: List[client.V1Pod], args):
print(f"Draining node {node}")
pods = [pod for pod in all_pods if pod.spec.node_name == node]
for pod in pods:
if pod.metadata.deletion_timestamp is not None:
continue
print(f" Evicting pod {pod.metadata.name}")
try:
if not args.dry_run:
v1.create_namespaced_pod_eviction(
pod.metadata.name,
pod.metadata.namespace,
{'metadata': {'name': pod.metadata.name}}
)
except client.rest.ApiException as exc:
if exc.status == 429:
print('Pod cannot be evicted right now due to PDB')
elif exc.status == 404:
pass
elif exc.status == 500:
print(f"Failed evicting pod {pod.metadata.name}; check PDB configuration")
else:
raise
def run():
args = parse_args()
try:
config.load_kube_config()
except:
print("Failed loading kube config. Trying to use in-cluster config.")
config.load_incluster_config()
v1 = client.CoreV1Api()
all_nodes = v1.list_node().items
if args.nodes is not None:
all_nodes = [node for node in all_nodes if node.metadata.name in args.nodes]
all_namespaces = v1.list_namespace().items
all_pods = [
pod for pod in v1.list_pod_for_all_namespaces().items
if not any(owner.kind == 'DaemonSet' for owner in (pod.metadata.owner_references or []))
]
for action, info in generate_action_plan(all_nodes, all_namespaces, all_pods, args).items():
nodes = info['nodes']
pods = info['affected_pods']
if action == 'notify':
notify(pods, args)
for node in nodes:
print(f"Notifications sent for node {node}")
v1.patch_node(node, {'metadata': {'annotations': {annotation('notifications-sent'): str(int(datetime.datetime.utcnow().timestamp()))}}})
if action == 'cordon':
for node in nodes:
print(f"Cordoning node {node}")
patch = {'metadata': {'annotations': {annotation('cordoned'): ''}}}
if not args.dry_run:
patch = {**patch, 'spec': {'unschedulable': True}}
v1.patch_node(node, patch)
if action == 'drain':
for node in nodes:
drain_node(v1, node, all_pods, args)
if __name__ == "__main__":
run()