This repository has been archived by the owner on Jul 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
executable file
·82 lines (65 loc) · 2.35 KB
/
schema.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
#!/usr/bin/env python3
import json
import os.path
import os
import urllib.request
def findRefs(o):
if "$ref" in o and isinstance(o["$ref"], str):
return [o["$ref"]]
if isinstance(o, dict):
res = []
for val in o.values():
res.extend(findRefs(val))
return res
if isinstance(o, list):
res = []
for val in o:
res.extend(findRefs(val))
return res
if isinstance(o, str):
return []
else:
raise Exception("Unsupported type %s" % type(o).__name__)
def extraceSchema(file):
schema = json.load(open(file))
definitions = schema["definitions"]
for (name, root) in definitions.items():
if "x-kubernetes-group-version-kind" not in root:
continue
#if name != "io.k8s.api.core.v1.Pod":
# continue
res = {
name: root
}
refs = findRefs(res)
while len(refs) > 0:
ref = refs[0]
if ref.startswith("#/definitions/"):
ref = ref[14:]
else:
raise Exception("unsupported reference to %s" % ref)
if ref not in res:
res[ref] = definitions[ref]
refs.extend(findRefs(definitions[ref]))
refs = refs[1:]
res = {
"$ref": "#/definitions/%s" % name,
"definitions": res,
}
for gvk in root["x-kubernetes-group-version-kind"]:
target_dir = os.path.join("schema", "core" if gvk["group"] == "" else gvk["group"], gvk["version"])
target = os.path.join(target_dir, gvk["kind"].lower() + ".json")
print(target)
os.makedirs(target_dir, exist_ok=True)
json.dump(res, open(target, "w"))
urlTemplate = "https://raw.githubusercontent.com/kubernetes/kubernetes/v%s/api/openapi-spec/swagger.json"
cacheTemplate = "./cache/k8s-%s-swagger.json"
os.makedirs("./cache", exist_ok=True)
for version in ["1.10.0", "1.11.0", "1.12.0", "1.13.0", "1.14.0", "1.15.0", "1.16.0", "1.17.0", "1.18.0"]:
url = urlTemplate % version
cache = cacheTemplate % version
if not os.path.isfile(cache):
print("Downloading swagger.json for %s from %s" % (version, url))
urllib.request.urlretrieve(url, cache)
print("Extracting schemas for %s" % version)
extraceSchema(cache)