-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-schemas.py
48 lines (36 loc) · 1.19 KB
/
extract-schemas.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
import sys
import logging
import argparse
import subprocess
import json
logging.basicConfig(level=logging.INFO)
def parse_args():
p = argparse.ArgumentParser()
p.add_argument("-o", "--output", help="Write output to file")
return p.parse_args()
def main():
args = parse_args()
merged = {
"openapi": "3.0.0",
"components": {
"schemas": {},
},
}
res = subprocess.check_output(["kubectl", "get", "--raw", "/openapi/v3"])
schemas = json.loads(res)
apicount = len([x for x in schemas["paths"] if x.startswith("apis")])
logging.info(f"Extracting {apicount} schemas")
for path, spec in schemas.get("paths", {}).items():
if not path.startswith("apis"):
continue
apicount += 1
logging.info(f"...extracting {path}")
res = subprocess.check_output(
["kubectl", "get", "--raw", spec["serverRelativeURL"]]
)
schema = json.loads(res)
merged["components"]["schemas"].update(schema["components"]["schemas"])
with sys.stdout if args.output is None else open(args.output, "w") as fd:
json.dump(merged, fd, indent=2)
if __name__ == "__main__":
main()