-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel-nodes.py
executable file
·45 lines (41 loc) · 1.81 KB
/
label-nodes.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
#!/usr/bin/env python
###############################################################################
import os
import time
import logging
from kubernetes import config
from kubernetes import client
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
level=os.environ.get('LOG_LEVEL', 'INFO').upper())
###############################################################################
if __name__ == '__main__':
interval = int(os.getenv('INTERVAL_SECONDS', '60'))
source_label_prefix = os.getenv('SOURCE_LABEL_PREFIX', 'node-role.kubernetes/')
destination_label_prefix = os.getenv('DESTINATION_LABEL_PREFIX', 'node-role.kubernetes.io/')
config.load_incluster_config()
logger = logging.getLogger('label-nodes')
logger.info('Running')
while True:
try:
api_instance = client.CoreV1Api()
node_list = api_instance.list_node()
for node in node_list.items:
relabeled = False
for key, val in node.metadata.labels.items():
if key.startswith(source_label_prefix):
role = key.split('/')[1]
if key.startswith(destination_label_prefix):
relabeled = True
if not relabeled:
body = {
"metadata": {
"labels": {
f"{destination_label_prefix.rstrip('/')}/{ role }": "true"
}
}
}
logger.info(f'Patching node { node.metadata.name } with {body.get("metadata").get("labels")}')
api_instance.patch_node(node.metadata.name, body)
except Exception as ex:
logger.error(ex)
time.sleep(interval)