-
Notifications
You must be signed in to change notification settings - Fork 2
/
add_labels.py
58 lines (52 loc) · 1.82 KB
/
add_labels.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
import sys
import yaml
CLUSTER_RESOURCES = set([
'Namespace',
'Node',
'PersistentVolume',
'CustomResourceDefinition',
'ClusterRole',
'ClusterRoleBinding',
'StorageClass',
'MutatingWebhookConfiguration',
'ValidatingWebhookConfiguration',
'PriorityClass',
'PodSecurityPolicy',
'APIService',
'TokenReview',
'CertificateSigningRequest',
'VolumeAttachment'
])
def add_labels(documents, mode, label_key, label_value):
for doc in documents:
if doc is None or 'kind' not in doc:
continue
if 'metadata' not in doc:
doc['metadata'] = {}
if 'labels' not in doc['metadata']:
doc['metadata']['labels'] = {}
if mode == "cluster" and doc.get('kind') in CLUSTER_RESOURCES:
doc['metadata']['labels'][label_key] = label_value
elif mode == "namespace" and doc.get('kind') not in CLUSTER_RESOURCES:
doc['metadata']['labels'][label_key] = label_value
return documents
def main(mode, label_key, label_value):
try:
# Load all YAML documents from stdin
documents = list(yaml.safe_load_all(sys.stdin))
# Add the labels to the documents
documents = add_labels(documents, mode, label_key, label_value)
# Output the updated documents to stdout
for doc in documents:
if doc is not None:
yaml.safe_dump(doc, sys.stdout, default_flow_style=False)
print('---')
except Exception as e:
sys.stderr.write(f"Error: {e}\n")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 4:
sys.stderr.write("Usage: add_labels.py mode labelKey labelValue\n")
sys.stderr.write("Mode should be either 'cluster' or 'namespace'\n")
sys.exit(1)
main(sys.argv[1], sys.argv[2], sys.argv[3])