-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswagger-k8s-crd-codegen.py
More file actions
executable file
·102 lines (81 loc) · 3.14 KB
/
swagger-k8s-crd-codegen.py
File metadata and controls
executable file
·102 lines (81 loc) · 3.14 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/python3
import yaml
import os
import shutil
from prance import ResolvingParser
from jinja2 import Template
# This tool reads a swagger.yml to produce Kubernetes CRDs
# of the specified models. This parser looks for special
# attributes in your model definition. Please define the following:
# * x-groupName = group name to use for generated CRD (e.g. ndslabs.org)
# * x-version = version name to use for generated CRD (e.g. v1)
# * x-singular = name for a single CRD of this type (e.g. workbenchuserapp)
# * x-plural = name for multiple CRD of this type (e.g. workbenchuserapps)
# * x-kind: CamelCase kind for this CRD (e.g. WorkbenchUserApp)
# * x-scope: "Namespaced" or "Cluster" (share to all namespaces)
# * x-shortNames: list of shorter string aliases for this CRD
# Describe the swagger spec to read
# TODO: Read from args and/or env
SWAGGER_URL = './swagger.yml'
DEFN_SUBPATH = 'definitions'
# Template inputs and outputs
TEMPLATE_URL = './template.crd.yaml'
OUTPUT_DIR = 'target'
# Extra customizations added to our swagger models
GROUPNAME_KEY = 'x-groupName'
VERSION_KEY = 'x-version'
PLURAL_KEY = 'x-plural'
SINGULAR_KEY = 'x-singular'
KIND_KEY = 'x-kind'
SCOPE_KEY = 'x-scope'
SHORTNAMES_KEY = 'x-shortNames'
CRD_GEN_COMMENT = """
## This file was generated by Workbench API server.
## Any manual changes to this file may be overwritten.
"""
def write_crd(defn, output_file):
with open(TEMPLATE_URL, "r") as f:
template = Template(f.read())
# Grab custom vars from swagger spec
crd_kind = defn[KIND_KEY]
crd_version = defn[VERSION_KEY]
crd_plural = defn[PLURAL_KEY]
crd_singular = defn[SINGULAR_KEY]
crd_groupname = defn[GROUPNAME_KEY]
crd_scope = defn[SCOPE_KEY]
crd_shortnames = defn[SHORTNAMES_KEY]
# Strip out customizations
crd_spec_schema = defn
for key in [KIND_KEY, VERSION_KEY, PLURAL_KEY, SINGULAR_KEY, GROUPNAME_KEY, SCOPE_KEY, SHORTNAMES_KEY]:
crd_spec_schema.pop(key)
full_crd = template.render(
crd_gen_comment=CRD_GEN_COMMENT,
crd_kind=crd_kind,
crd_version=crd_version,
crd_plural=crd_plural,
crd_singular=crd_singular,
crd_groupname=crd_groupname,
crd_scope=crd_scope,
crd_shortnames=yaml.dump(crd_shortnames),
crd_spec_schema=yaml.dump(crd_spec_schema)
)
# Write resulting CRD
with open(output_file, "w") as f:
print("Writing " + output_file)
f.write(full_crd)
return
if __name__ == '__main__':
# Clean old models
#if os.path.exists(OUTPUT_DIR):
# shutil.rmtree(OUTPUT_DIR)
# Create output directory
if not os.path.exists(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
# Parse swagger spec, resolve any $refs
parser = ResolvingParser(SWAGGER_URL)
for defn in parser.specification[DEFN_SUBPATH]:
schema = parser.specification[DEFN_SUBPATH][defn]
if 'x-groupName' in schema:
plural = schema[PLURAL_KEY]
file_name = "%s.crd.yml" % plural
write_crd(schema, os.path.join(OUTPUT_DIR, file_name))