Skip to content

Commit 268c07c

Browse files
JPinkneysleshchenko
authored andcommitted
Extract additional metadata from crds
Signed-off-by: Josh Pinkney <[email protected]>
1 parent 873804b commit 268c07c

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#
2+
# Copyright (c) 2021 Red Hat, Inc.
3+
# This program and the accompanying materials are made
4+
# available under the terms of the Eclipse Public License 2.0
5+
# which is available at https://www.eclipse.org/legal/epl-2.0/
6+
#
7+
# SPDX-License-Identifier: EPL-2.0
8+
#
9+
# Contributors:
10+
# Red Hat, Inc. - initial API and implementation
11+
#
12+
13+
import os
14+
import argparse
15+
import yaml
16+
17+
18+
def write_contents(filename: str, mode: str, contents: str) -> None:
19+
"""
20+
Write the string to the specified filename
21+
"""
22+
with open(filename, mode) as out:
23+
out.write(contents)
24+
25+
26+
def get_crd_metadata(output_path: str) -> None:
27+
"""
28+
Read in the devworkspace and devworkspace template crds and generate metadata into api/apis.ts
29+
"""
30+
crd_path = "crds"
31+
typescript_contents = ""
32+
devworkspace_crd_path = os.path.join(crd_path, 'workspace.devfile.io_devworkspaces.yaml')
33+
with open(devworkspace_crd_path, 'r') as devfile_file:
34+
yaml_data = yaml.load(devfile_file, Loader=yaml.FullLoader)
35+
spec, group, kind, plural, singular, versions, latest_version, latest_api_version = extract_fields(yaml_data)
36+
typescript_contents += generate_typescript(latest_api_version, group, kind, plural, singular, versions,
37+
latest_version)
38+
39+
devworkspacetemplate_crd_path = os.path.join(crd_path, 'workspace.devfile.io_devworkspacetemplates.yaml')
40+
with open(devworkspacetemplate_crd_path, 'r') as devfile_file:
41+
yaml_data = yaml.load(devfile_file, Loader=yaml.FullLoader)
42+
spec, group, kind, plural, singular, versions, latest_version, latest_api_version = extract_fields(yaml_data)
43+
typescript_contents += generate_typescript(latest_api_version, group, kind, plural, singular, versions,
44+
latest_version)
45+
46+
write_contents(os.path.join(output_path, "constants", "constants.ts"), "w", typescript_contents)
47+
48+
49+
def extract_fields(yaml_data: {}) -> (str, str, str, str, str, [], str, str):
50+
"""
51+
Extract metadata from the crds
52+
"""
53+
spec = yaml_data['spec']
54+
group = spec['group']
55+
kind = spec['names']['kind']
56+
plural = spec['names']['plural']
57+
singular = spec['names']['singular']
58+
versions = [version['name'] for version in spec['versions']]
59+
latest_version = versions[len(versions) - 1]
60+
latest_api_version = "{}/{}".format(group, latest_version)
61+
return spec, group, kind, plural, singular, versions, latest_version, latest_api_version
62+
63+
64+
def generate_typescript(api_version: str, group: str, kind: str, plural: str, singular: str, versions: [],
65+
latest_version: str) -> str:
66+
"""
67+
Export a string representation of the typescript
68+
"""
69+
return f"""
70+
export const {singular + "ApiVersion"} = '{api_version}';
71+
export const {singular + "Group"} = '{group}';
72+
export const {singular + "Kind"} = '{kind}';
73+
export const {singular + "Plural"} = '{plural}';
74+
export const {singular + "Singular"} = '{singular}';
75+
export const {singular + "Versions"} = {versions};
76+
export const {singular + "LatestVersion"} = '{latest_version}';
77+
"""
78+
79+
80+
def export_typescript_api(output_path: str) -> None:
81+
"""
82+
Export constants into api.ts
83+
"""
84+
export_contents = """
85+
export * from './constants/constants';
86+
"""
87+
write_contents(os.path.join(output_path, "api.ts"), "a", export_contents)
88+
89+
90+
if __name__ == "__main__":
91+
# Get any additional metadata we can from the crds
92+
parser = argparse.ArgumentParser(description='Generate metadata from crds')
93+
parser.add_argument('-p', '--path', action='store', type=str, help='The path to the constants directory')
94+
95+
args = parser.parse_args()
96+
if not args.path:
97+
parser.print_help()
98+
parser.exit()
99+
100+
path = args.path
101+
102+
# Grab the metadata from the crds and put it into constants/constant.ts in typescript-model
103+
get_crd_metadata(path)
104+
105+
# Export constants/constant.ts so that you can import constants from the package
106+
export_typescript_api(path)

build/typescript-model/generate-swagger-json.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@
1313
import os
1414
import json
1515

16+
1617
def write_json(filename: str, object: dict) -> None:
1718
"""
1819
Write the json object to the specified filename
1920
"""
2021
with open(filename, 'w') as out:
2122
json.dump(object, out, sort_keys=False, indent=2, separators=(',', ': '), ensure_ascii=True)
2223

24+
2325
def create_ref(path):
2426
"""
2527
Create a json definition reference to a specific path
2628
"""
2729
return '#/definitions/' + path
2830

29-
def consolidate_schemas() -> object:
31+
def consolidate_schemas() -> dict:
3032
"""
3133
Consolidate all schemas into one json object
3234
"""

build/typescript-model/generate.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ EOF
3737
sed -i 's/\"license\": \".*\"/"license": "EPL-2.0"/g' $WORK_DIR/typescript-models/package.json
3838
sed -i 's/\"@types\/bluebird\": \".*\"/"@types\/bluebird": "3.5.21"/g' $WORK_DIR/typescript-models/package.json
3939
echo "" > $WORK_DIR/typescript-models/.npmignore
40-
echo "[INFO] Generated typescript model which now is availalbe in $WORK_DIR/typescript-models"
40+
echo "[INFO] Generated typescript model which now is available in $WORK_DIR/typescript-models"
4141
}
4242

4343
generate_swagger_json() {
@@ -49,6 +49,13 @@ generate_swagger_json() {
4949
echo "[INFO] Generating Swagger JSON. It's in $WORK_DIR/typescript-models/swagger.json.unprocessed"
5050
}
5151

52+
generate_typescript_metadata() {
53+
echo "[INFO] Generating typescript constants from crds ..."
54+
mkdir -p $WORK_DIR/typescript-models/constants
55+
python3 $SCRIPT_DIR/generate-metadata.py -p $WORK_DIR/typescript-models
56+
echo "[INFO] Finished generating typescript constant from crds. They are available in $WORK_DIR/typescript-models/constants"
57+
}
58+
5259
build_typescript_model() {
5360
echo "[INFO] Verify that generated model is buildable..."
5461
cd $WORK_DIR/typescript-models
@@ -58,6 +65,7 @@ build_typescript_model() {
5865

5966
generate_swagger_json
6067
k8s_client_gen
68+
generate_typescript_metadata
6169
build_typescript_model
6270

6371
echo "[INFO] Typescript model is successfully generated and verified."

0 commit comments

Comments
 (0)