-
Notifications
You must be signed in to change notification settings - Fork 1
/
kube-annotator.py
executable file
·89 lines (63 loc) · 2.07 KB
/
kube-annotator.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python
import collections
import json
import re
import sys
def loadapi(fn):
with open(fn) as f:
return json.load(f, object_pairs_hook=collections.OrderedDict)
def resolvetype(p):
if "type" in p:
typ = p["type"]
if typ == "array":
typ = "[]" + resolvetype(p["items"])
elif "$ref" in p:
typ = p["$ref"]
else:
raise Exception()
return typ
def printproperties(api, m, indent=""):
for p in api["models"][m]["properties"]:
typ = resolvetype(api["models"][m]["properties"][p])
if typ == "object":
typ = "map[string]string"
key = p + ":"
if indent == "":
if p == "kind":
key += " " + m.split(".", 1)[1]
elif p == "apiVersion":
key += " " + m.split(".", 1)[0]
print "%-40s# %-40s%s" % (indent + key, typ, re.sub(r"\n+", " ", api["models"][m]["properties"][p].get("description", "")))
indent = indent.replace("-", " ")
rtyp = typ.lstrip("[]")
if rtyp in api["models"]:
if typ == rtyp:
newindent = indent + " "
else:
newindent = indent + "- "
printproperties(api, rtyp, newindent)
elif typ == "map[string]string":
print "%-40s# %s" % (indent + " [string]:", "string")
elif rtyp != typ:
print "%-40s# %s" % (indent + "- [" + rtyp + "]", "")
def printmodels(api):
for m in sorted(api["models"]):
if m[0] != "v":
continue
if "kind" not in api["models"][m]["properties"]:
continue
if m.endswith("List"):
continue
print m.split(".", 1)[1]
print "=" * len(m.split(".", 1)[1])
print
if "description" in api["models"][m]:
print api["models"][m]["description"]
print
printproperties(api, m)
print
print
def main(fn):
printmodels(loadapi(fn))
if __name__ == "__main__":
main(*sys.argv[1:])