-
Notifications
You must be signed in to change notification settings - Fork 784
/
generate-json-schema.py
executable file
·64 lines (49 loc) · 2.05 KB
/
generate-json-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
#!/usr/bin/env python3
"""
This script reads values.schema.yaml and generates a values.schema.json that we
can package with the Helm chart, allowing helm the CLI perform validation.
While we can directly generate a values.schema.json from values.schema.yaml, it
contains a lot of description text we use to generate our configuration
reference that isn't helpful to ship along the validation schema. Due to that,
we trim away everything that isn't needed.
"""
import json
import os
from collections.abc import MutableMapping
import yaml
here_dir = os.path.abspath(os.path.dirname(__file__))
schema_yaml = os.path.join(here_dir, os.pardir, "jupyterhub", "values.schema.yaml")
values_schema_json = os.path.join(
here_dir, os.pardir, "jupyterhub", "values.schema.json"
)
def clean_jsonschema(d, parent_key=""):
"""
Modifies a dictionary representing a jsonschema in place to not contain
jsonschema keys not relevant for a values.schema.json file solely for use by
the helm CLI.
"""
JSONSCHEMA_KEYS_TO_REMOVE = {"description"}
# start by cleaning up the current level
for k in set.intersection(JSONSCHEMA_KEYS_TO_REMOVE, set(d.keys())):
del d[k]
# Recursively cleanup nested levels, bypassing one level where there could
# be a valid Helm chart configuration named just like the jsonschema
# specific key to remove.
if "properties" in d:
for k, v in d["properties"].items():
if isinstance(v, MutableMapping):
clean_jsonschema(v, k)
def run():
# Using these sets, we can validate further manually by printing the results
# of set operations.
with open(schema_yaml) as f:
schema = yaml.safe_load(f)
# Drop what isn't relevant for a values.schema.json file packaged with the
# Helm chart, such as the description keys only relevant for our
# configuration reference.
clean_jsonschema(schema)
# dump schema to values.schema.json
with open(values_schema_json, "w") as f:
json.dump(schema, f)
print("jupyterhub/values.schema.json created")
run()