Skip to content

Commit

Permalink
Extract additional metadata from crds
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Pinkney <[email protected]>
  • Loading branch information
JPinkney authored and sleshchenko committed Sep 17, 2021
1 parent 873804b commit 268c07c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
106 changes: 106 additions & 0 deletions build/typescript-model/generate-metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#
# Copyright (c) 2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

import os
import argparse
import yaml


def write_contents(filename: str, mode: str, contents: str) -> None:
"""
Write the string to the specified filename
"""
with open(filename, mode) as out:
out.write(contents)


def get_crd_metadata(output_path: str) -> None:
"""
Read in the devworkspace and devworkspace template crds and generate metadata into api/apis.ts
"""
crd_path = "crds"
typescript_contents = ""
devworkspace_crd_path = os.path.join(crd_path, 'workspace.devfile.io_devworkspaces.yaml')
with open(devworkspace_crd_path, 'r') as devfile_file:
yaml_data = yaml.load(devfile_file, Loader=yaml.FullLoader)
spec, group, kind, plural, singular, versions, latest_version, latest_api_version = extract_fields(yaml_data)
typescript_contents += generate_typescript(latest_api_version, group, kind, plural, singular, versions,
latest_version)

devworkspacetemplate_crd_path = os.path.join(crd_path, 'workspace.devfile.io_devworkspacetemplates.yaml')
with open(devworkspacetemplate_crd_path, 'r') as devfile_file:
yaml_data = yaml.load(devfile_file, Loader=yaml.FullLoader)
spec, group, kind, plural, singular, versions, latest_version, latest_api_version = extract_fields(yaml_data)
typescript_contents += generate_typescript(latest_api_version, group, kind, plural, singular, versions,
latest_version)

write_contents(os.path.join(output_path, "constants", "constants.ts"), "w", typescript_contents)


def extract_fields(yaml_data: {}) -> (str, str, str, str, str, [], str, str):
"""
Extract metadata from the crds
"""
spec = yaml_data['spec']
group = spec['group']
kind = spec['names']['kind']
plural = spec['names']['plural']
singular = spec['names']['singular']
versions = [version['name'] for version in spec['versions']]
latest_version = versions[len(versions) - 1]
latest_api_version = "{}/{}".format(group, latest_version)
return spec, group, kind, plural, singular, versions, latest_version, latest_api_version


def generate_typescript(api_version: str, group: str, kind: str, plural: str, singular: str, versions: [],
latest_version: str) -> str:
"""
Export a string representation of the typescript
"""
return f"""
export const {singular + "ApiVersion"} = '{api_version}';
export const {singular + "Group"} = '{group}';
export const {singular + "Kind"} = '{kind}';
export const {singular + "Plural"} = '{plural}';
export const {singular + "Singular"} = '{singular}';
export const {singular + "Versions"} = {versions};
export const {singular + "LatestVersion"} = '{latest_version}';
"""


def export_typescript_api(output_path: str) -> None:
"""
Export constants into api.ts
"""
export_contents = """
export * from './constants/constants';
"""
write_contents(os.path.join(output_path, "api.ts"), "a", export_contents)


if __name__ == "__main__":
# Get any additional metadata we can from the crds
parser = argparse.ArgumentParser(description='Generate metadata from crds')
parser.add_argument('-p', '--path', action='store', type=str, help='The path to the constants directory')

args = parser.parse_args()
if not args.path:
parser.print_help()
parser.exit()

path = args.path

# Grab the metadata from the crds and put it into constants/constant.ts in typescript-model
get_crd_metadata(path)

# Export constants/constant.ts so that you can import constants from the package
export_typescript_api(path)
4 changes: 3 additions & 1 deletion build/typescript-model/generate-swagger-json.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
import os
import json


def write_json(filename: str, object: dict) -> None:
"""
Write the json object to the specified filename
"""
with open(filename, 'w') as out:
json.dump(object, out, sort_keys=False, indent=2, separators=(',', ': '), ensure_ascii=True)


def create_ref(path):
"""
Create a json definition reference to a specific path
"""
return '#/definitions/' + path

def consolidate_schemas() -> object:
def consolidate_schemas() -> dict:
"""
Consolidate all schemas into one json object
"""
Expand Down
10 changes: 9 additions & 1 deletion build/typescript-model/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ EOF
sed -i 's/\"license\": \".*\"/"license": "EPL-2.0"/g' $WORK_DIR/typescript-models/package.json
sed -i 's/\"@types\/bluebird\": \".*\"/"@types\/bluebird": "3.5.21"/g' $WORK_DIR/typescript-models/package.json
echo "" > $WORK_DIR/typescript-models/.npmignore
echo "[INFO] Generated typescript model which now is availalbe in $WORK_DIR/typescript-models"
echo "[INFO] Generated typescript model which now is available in $WORK_DIR/typescript-models"
}

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

generate_typescript_metadata() {
echo "[INFO] Generating typescript constants from crds ..."
mkdir -p $WORK_DIR/typescript-models/constants
python3 $SCRIPT_DIR/generate-metadata.py -p $WORK_DIR/typescript-models
echo "[INFO] Finished generating typescript constant from crds. They are available in $WORK_DIR/typescript-models/constants"
}

build_typescript_model() {
echo "[INFO] Verify that generated model is buildable..."
cd $WORK_DIR/typescript-models
Expand All @@ -58,6 +65,7 @@ build_typescript_model() {

generate_swagger_json
k8s_client_gen
generate_typescript_metadata
build_typescript_model

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

0 comments on commit 268c07c

Please sign in to comment.