-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.py
executable file
·62 lines (38 loc) · 1.3 KB
/
split.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
#!/usr/bin/env python3
from pathlib import Path
from ruamel.yaml import YAML
def split_k8s_manifest_by_kind(input_file):
yaml = YAML()
with open(input_file, 'r') as file:
documents = list(yaml.load_all(file))
secrets = []
others = []
for doc in documents:
if doc is None:
continue
kind = doc.get('kind', 'Unknown')
if kind == 'Secret':
secrets.append(doc)
else:
others.append(doc)
basename = Path(input_file).stem
secretname = f"{basename.lower()}-secrets.yaml"
if Path(f"manifests/{secretname}").is_file():
return
with open(f"manifests/{secretname}", 'w') as file:
yaml.dump_all(secrets, file)
with open(f"manifests/{basename.lower()}.yaml", 'w') as file:
yaml.dump_all(others, file)
if __name__ == "__main__":
input_file = 'k8s_manifest.yaml'
folder = Path('manifests')
for file in folder.rglob("*.yaml"): # Recursively iterate through all files
if file.is_file() and not file.name.endswith('secrets.yaml') and not file.name.endswith('others.yaml'):
pass
else:
continue
try:
split_k8s_manifest_by_kind(file)
except Exception as e:
print(f"Error: {e}")
continue