-
Notifications
You must be signed in to change notification settings - Fork 5
/
scheduler.py
412 lines (314 loc) · 12.7 KB
/
scheduler.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import os
import time
from urllib.parse import urljoin, quote
from collections import defaultdict
import json
import random
from copy import copy, deepcopy
import logging
import requests
API_URL = 'https://kubernetes/api/v1/'
OUR_SCHEDULER_NAME = 'stickToExistingNodeScheduler'
DEFAULT_KUBERNETES_SCHEDULER = 'default-scheduler'
NAMESPACE = 'default'
TOKEN_LOCATION = '/var/run/secrets/kubernetes.io/serviceaccount/token'
CA_BUNDLE_LOCATION = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
logging.basicConfig(level=logging.INFO)
_log = logging.getLogger(__name__)
NODE_FILTER_QUERY = os.environ.get('NODE_FILTER_QUERY', '')
SUPPORT_MINIKUBE = int(os.environ.get('SUPPORT_MINIKUBE', '1'))
POLL_FREQUENCY = float(os.environ.get('POLL_FREQUENCY', '0.5'))
logging.getLogger('requests').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
_nodes_scheduled_to = defaultdict(list)
_nodes_to_skip = defaultdict(list)
class ErrorSchedulingPod(Exception):
pass
class ErrorDeletingPod(Exception):
pass
class ErrorCreatingPod(Exception):
pass
class NoValidNodesToScheduleTo(Exception):
pass
def k8_token_content():
with open(TOKEN_LOCATION) as f:
return f.read()
def k8_request(method, url, headers=None, **kwargs):
if headers is None:
headers = {}
f = getattr(requests, method)
our_headers = copy(headers)
our_headers['Authorization'] = 'Bearer {}'.format(k8_token_content())
return f(url, headers=our_headers, verify=CA_BUNDLE_LOCATION, **kwargs)
def get_unscheduled_pods():
pending_pods_url = urljoin(API_URL, 'pods?fieldSelector=spec.nodeName=')
r = k8_request('get', pending_pods_url)
pending_pods_info = r.json()
return pending_pods_info.get('items', [])
def get_failed_pods():
failed_pods = urljoin(API_URL, 'pods?fieldSelector=status.phase=Failed')
r = k8_request('get', failed_pods)
pending_pods_info = r.json()
return pending_pods_info.get('items', [])
def escape_jsonpatch_value(value):
return value.replace('/', '~1')
def _is_pod_running_or_pending(label_selector, pod_name):
url = urljoin(API_URL, 'pods?labelSelector={}'.format(
quote(label_selector)))
r = k8_request('get', url)
pods_for_selector = r.json()
for pod in pods_for_selector.get('items', []):
if pod['metadata']['name'] != pod_name:
continue
status = pod.get('status', {}).get('phase')
if status.lower() == 'running' or status.lower() == 'pending':
return True
else:
return False
return True
def get_pod_selector(pod):
"""
Returns:
The string representing the labelSelector to select pods of this
general type.
"""
labels = pod['metadata'].get('labels', {})
# We don't use the 'pod-template-hash' label because we want to schedule
# our new pods onto the node they're currently running on, even if the
# pod template has been updated (in our case, that's the point!)
if 'pod-template-hash' in labels:
del labels['pod-template-hash']
selector = []
for k in sorted(labels.keys()):
selector.append('{}={}'.format(k, labels[k]))
return ','.join(selector)
def get_nodes():
"""
Returns:
A list of node name strings.
"""
url = urljoin(API_URL, 'nodes?{}'.format(NODE_FILTER_QUERY))
r = k8_request('get', url)
result = r.json()
nodes = result['items']
nodes = set([n['metadata']['name'] for n in nodes])
# Support for minikube, which doesn't have the usual
# 'kubernetes.io/role=node' label
if SUPPORT_MINIKUBE:
url = urljoin(API_URL, 'nodes?labelSelector=kubernetes.io/hostname=minikube')
r = k8_request('get', url)
result = r.json()
nodes = nodes.union(set([n['metadata']['name'] for n in result['items']]))
return nodes
def get_node_running_pod(pod):
nodes = set()
label_selector = get_pod_selector(pod)
url = urljoin(API_URL, 'pods?labelSelector={}'.format(
quote(label_selector)))
r = k8_request('get', url)
pods_for_selector = r.json()
for pod in pods_for_selector.get('items', []):
node_name = pod['spec'].get('nodeName')
if not node_name:
continue
status = pod.get('status', {}).get('phase')
if status.lower() != 'running' and status.lower() != 'pending':
continue
nodes.add(node_name)
assert len(nodes) <= 1, "Pod should only be running on one or less node"
return nodes.pop() if nodes else None
def create_pod_definition(pod):
"""
Args:
pod: A dictionary describing a pod.
Returns:
A pod definiton suitable for a create request from the API.
"""
pod = deepcopy(pod)
# Remove elements that are not needed in the pod creation
# definition, or elements that aren't allowed in the pod
# creation definition.
pod.pop('status', None)
if 'annotations' in pod['metadata']:
pod['metadata']['annotations'].pop('kubernetes.io/created-by', None)
#pod['metadata'].pop('name', None)
#pod['metadata'].pop('generateName', None)
pod['metadata'].pop('creationTimestamp', None)
pod['metadata'].pop('generateTime', None)
#pod['metadata'].pop('ownerReferences', None)
pod['metadata'].pop('resourceVersion', None)
pod['metadata'].pop('selfLink', None)
pod['metadata'].pop('uid', None)
return pod
def set_default_scheduler_on_pod(pod):
# It's currently not possible to change the scheduler on an existing
# pod -- see https://github.com/kubernetes/kubernetes/issues/24913
# Because of this, we delete the pod and re-create it with the default
# scheduler set.
label_selector = get_pod_selector(pod)
# We first create the new pod, because otherwise a RC/Deployment
# may re-create the deleted pod before we can create it.
new_pod = create_pod_definition(pod)
del new_pod['spec']['schedulerName']
new_pod['metadata']['name'] += '-rescheduled'
create_pod(new_pod)
record_as_default_scheduled(label_selector, new_pod)
delete_pod(pod)
def create_pod(pod):
pod_name = pod['metadata']['name']
label_selector = get_pod_selector(pod)
_log.info('Creating pod {} ({})'.format(
pod_name, label_selector))
url = urljoin(API_URL, 'namespaces/{}/pods'.format(
NAMESPACE))
r = k8_request('post', url, json=pod)
if r.status_code != 201:
raise ErrorCreatingPod(
'There was an error creating pod {}.'.format(
pod_name))
def delete_pod(pod):
pod_name = pod['metadata']['name']
label_selector = get_pod_selector(pod)
_log.info('Deleting pod {} ({})'.format(
pod_name, label_selector))
url = urljoin(API_URL, 'namespaces/{}/pods/{}'.format(
NAMESPACE, pod_name))
payload = {
'apiVersion': 'v1',
'gracePeriodSeconds': 0,
}
r = k8_request('delete', url, json=payload)
if r.status_code != 200:
raise ErrorDeletingPod(
'There was an error deleting pod {}.'.format(
pod_name))
def bind_pod_to_node(pod, node_running_pod):
pod_name = pod['metadata']['name']
label_selector = get_pod_selector(pod)
_log.info('Binding pod {} ({}) to node {}'.format(
pod_name, label_selector, node_running_pod))
url = urljoin(API_URL, 'namespaces/{}/pods/{}/binding'.format(
NAMESPACE, pod_name))
payload = {
'apiVersion': 'v1',
'kind': 'Binding',
'metadata': {
'name': pod_name,
},
'target': {
'apiVersion': 'v1',
'kind': 'Node',
'name': node_running_pod,
}
}
r = k8_request('post', url, json=payload)
if r.status_code != 201:
raise ErrorSchedulingPod(
'There was an error scheduling pod {} on node {}.'.format(
pod_name, node_running_pod))
def pick_node_to_schedule_to(pod):
label_selector = get_pod_selector(pod)
nodes = get_nodes()
# Remove nodes that are now gone
old_nodes = set([n for n in _nodes_scheduled_to])
new_nodes = set(nodes)
for node in (new_nodes - old_nodes):
# Add new nodes
_nodes_scheduled_to[node] = []
for node in (old_nodes - new_nodes):
# Delete nodes that are gone
del _nodes_scheduled_to[node]
nodes_to_skip = _nodes_to_skip[label_selector]
# Pick a node with the smallest number of our pods
# scheduled to it.
nodes = copy(_nodes_scheduled_to)
for node in nodes_to_skip:
if node in nodes:
del nodes[node]
nodes = list(nodes.items())
nodes.sort(key=lambda x: len(_nodes_scheduled_to[x[0]]))
if not nodes:
raise NoValidNodesToScheduleTo('No more valid nodes to schedule to.')
return nodes[0][0]
def mark_pod_as_scheduled(pod, node_name):
label_selector = get_pod_selector(pod)
_nodes_scheduled_to[node_name].append(label_selector)
def unmark_pod_as_scheduled(pod, node_name):
label_selector = get_pod_selector(pod)
if label_selector in _nodes_scheduled_to[node_name]:
_nodes_scheduled_to[node_name].remove(label_selector)
def process_unscheduled_pods(pods):
for pod in pods:
spec = pod.get('spec', {})
pod_scheduler_name = spec.get('schedulerName')
label_selector = get_pod_selector(pod)
# We only schedule pods that are set to use this scheduler.
if pod_scheduler_name == OUR_SCHEDULER_NAME:
node_running_pod = get_node_running_pod(pod)
if node_running_pod:
node_to_schedule_to = node_running_pod
else:
# If the pod isn't already running somewhere, then we pick a
# node to schedule the pod to.
try:
node_to_schedule_to = pick_node_to_schedule_to(pod)
except NoValidNodesToScheduleTo:
# We will re-try the scheduling again in the parent loop,
# but for now we skip it.
_log.info(
'Skipping scheduling pod of form {} for now.'.format(
label_selector))
# We clear out the tainted nodes to avoid the scheduler
# getting stuck -- we want to re-try previously
# failed nodes, now.
if label_selector in _nodes_to_skip:
del _nodes_to_skip[label_selector]
return
_log.info(
'No node currently running pod of form {}. Scheduling it to '
'node {}'.format(label_selector, node_to_schedule_to))
try:
bind_pod_to_node(pod, node_to_schedule_to)
except ErrorSchedulingPod:
if not node_running_pod:
# We want to now taint the node we attempted to schedule
# on to, so that we will rotate over to a new node
# when we try and schedule again.
_nodes_to_skip[label_selector].append(node_to_schedule_to)
else:
mark_pod_as_scheduled(pod, node_to_schedule_to)
# Because we were able to schedule the pod, let's clear out the
# nodes to skip on this label selector. This will allow us to
# try nodes that may now be schedulable next time we.
if not node_running_pod:
if _nodes_to_skip[label_selector]:
del _nodes_to_skip[label_selector]
def process_failed_pods(pods):
for pod in pods:
spec = pod.get('spec', {})
pod_scheduler_name = spec.get('schedulerName')
label_selector = get_pod_selector(pod)
# We only deal with pods that are set to use this scheduler.
if pod_scheduler_name != OUR_SCHEDULER_NAME:
continue
status = pod.get('status')
_log.error(
'Pod of type {} failed. Deleting pod and hoping it will '
're-spawn correctly. Full pod status:\n{}'.format(label_selector, status))
# Delete the failed pod. Hopefully it's wired up to a replication
# controller that will re-spawn it or something.
delete_pod(pod)
node_name = pod.get('spec', {}).get('nodeName')
unmark_pod_as_scheduled(pod, node_name)
def run_loop():
while True:
time.sleep(POLL_FREQUENCY)
unscheduled_pods = get_unscheduled_pods()
# Return in random order to prevent infinite scheduling loops
# when pods fail to schedule.
random.shuffle(unscheduled_pods)
process_unscheduled_pods(unscheduled_pods)
failed_pods = get_failed_pods()
process_failed_pods(failed_pods)
if __name__ == '__main__':
run_loop()