From 07883b8bb44904a95843189d3f9d53b348220093 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 17 Oct 2020 17:21:33 +0200 Subject: [PATCH] Update Readme, converter helper script can now reads straight from http --- Readme.md | 10 ++++++++++ cmd/openapi2jsonschema/main.py | 7 ++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 4602fde..3d24d87 100644 --- a/Readme.md +++ b/Readme.md @@ -118,6 +118,16 @@ in a local folder - for example schemas. Then we specify this folder as an addit $ ./bin/kubeconform -registry kubernetesjsonschema.dev -registry 'schemas/{{ .ResourceKind }}{{ .KindSuffix }}.json' fixtures/custom-resource.yaml ``` +### Generating a JSON schema from an OpenAPI file + +Kubeconform uses JSON schemas to validate Kubernetes resources. For Custom Resource, the CustomResourceDefinition +first needs to be converted to JSON Schema. A script is provided to convert these CustomResourceDefinitions +to JSON schema. Here is an example how to use it: + +``` +$ ./cmd/openapi2jsonschema/main.py https://raw.githubusercontent.com/aws/amazon-sagemaker-operator-for-k8s/master/config/crd/bases/sagemaker.aws.amazon.com_trainingjobs.yaml > fixtures/registry/trainingjob-sagemaker-v1.json +``` + ### Credits * @garethr for the [Kubeval](https://github.com/instrumenta/kubeval) and diff --git a/cmd/openapi2jsonschema/main.py b/cmd/openapi2jsonschema/main.py index 95f7ebb..77eb3ee 100755 --- a/cmd/openapi2jsonschema/main.py +++ b/cmd/openapi2jsonschema/main.py @@ -4,6 +4,7 @@ import yaml import json import sys +import urllib.request def iteritems(d): if hasattr(dict, "iteritems"): @@ -90,7 +91,11 @@ if len(sys.argv) == 0: print("missing file") exit(1) -with open(sys.argv[1]) as f: +if sys.argv[1].startswith("http"): + f = urllib.request.urlopen(sys.argv[1]) +else: + f = open(sys.argv[1]) +with f: y = yaml.load(f, Loader=yaml.SafeLoader) schema = y["spec"]["validation"]["openAPIV3Schema"] schema = additional_properties(schema)