-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (71 loc) · 2.38 KB
/
Copy pathapp.py
File metadata and controls
79 lines (71 loc) · 2.38 KB
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
"""
This app watches for new nodes in the Kubernetes cluster
and labels them with the appropriate role label.
"""
import json
from kubernetes import client, config, watch
config.load_incluster_config()
v1 = client.CoreV1Api()
ROLE_LABEL_KEY = "node-group"
def label_node(node_name, key, value):
"""
Label a node with a key-value pair.
"""
body = {
"metadata": {
"labels": {
key: value
}
}
}
try:
v1.patch_node(node_name, body)
label_node_response = {
"status": "success",
"message": f"Successfully labeled node {node_name} with {key}={value}",
"node_name": node_name,
"label": {key: value}
}
except client.exceptions.ApiException as e:
label_node_response = {
"status": "error",
"message": f"Failed to label node {node_name}: {e}",
"node_name": node_name
}
return json.dumps(label_node_response)
def watch_new_nodes():
"""
Watch for new nodes in the cluster
and label them with the appropriate role label.
"""
w = watch.Watch()
for event in w.stream(v1.list_node):
node = event["object"]
node_name = node.metadata.name
node_role_label = node.metadata.labels.get(ROLE_LABEL_KEY)
node_role_label_key = f'node-role.kubernetes.io/{node_role_label}'
event_type = event["type"]
if event_type == "ADDED":
labels = set(node.metadata.labels.keys())
if node_role_label_key not in labels:
watch_node_response = {
"status": "info",
"message": f"New node detected: {node_name}, labeling it...",
"node_name": node_name
}
label_node(node_name, node_role_label_key, 'true')
print(json.dumps(watch_node_response))
else:
watch_node_response = {
"status": "info",
"message": f"Node {node_name} already has the role label {node_role_label}",
"node_name": node_name
}
print(json.dumps(watch_node_response))
if __name__ == "__main__":
response = {
"status": "info",
"message": "Starting to watch for new nodes..."
}
print(json.dumps(response))
watch_new_nodes()