Download OpenAPI spec directly from k8s release branch

This commit is contained in:
mbohlool 2016-12-07 21:44:58 -08:00
parent baba523c28
commit 4c14ab38bb
4 changed files with 66 additions and 42 deletions

27
scripts/constants.py Normal file
View File

@ -0,0 +1,27 @@
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Kubernetes branch to get the OpenAPI spec from.
KUBERNETES_BRANCH = "release-1.5"
# Spec version will be set in downloaded spec and all
# generated code will refer to it.
SPEC_VERSION = "v1.5.0-beta.3"
# client version for packaging and releasing. It can
# be different than SPEC_VERSION.
CLIENT_VERSION = "1.0.0-alpha.2"
# Name of the release package
PACKAGE_NAME = "kubernetes"

View File

@ -18,6 +18,9 @@ import os.path
import sys
from collections import OrderedDict
import urllib3
from constants import KUBERNETES_BRANCH, SPEC_VERSION
# these four constants are shown as part of this example in []:
# "[watch]Pod[List]" is the deprecated version of "[list]Pod?[watch]=True"
WATCH_OP_PREFIX = "watch"
@ -25,6 +28,10 @@ WATCH_OP_SUFFIX = "List"
LIST_OP_PREFIX = "list"
WATCH_QUERY_PARAM_NAME = "watch"
SPEC_URL = 'https://raw.githubusercontent.com/kubernetes/kubernetes/' \
'%s/api/openapi-spec/swagger.json' % KUBERNETES_BRANCH
OUTPUT_PATH = os.path.join(os.path.dirname(__file__), 'swagger.json')
_ops = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch']
@ -94,37 +101,38 @@ def strip_tags_from_operation_id(operation, _):
operation['operationId'] = operation_id
def process_swagger(infile, outfile):
with open(infile, 'r') as f:
spec = json.load(f, object_pairs_hook=OrderedDict)
def process_swagger(spec):
apply_func_to_spec_operations(spec, strip_tags_from_operation_id)
apply_func_to_spec_operations(spec, strip_tags_from_operation_id)
operation_ids = {}
apply_func_to_spec_operations(spec, lambda op, _: operator.setitem(
operation_ids, op['operationId'], op))
operation_ids = {}
apply_func_to_spec_operations(spec, lambda op, _: operator.setitem(
operation_ids, op['operationId'], op))
try:
apply_func_to_spec_operations(
spec, remove_watch_operations, operation_ids)
except PreprocessingException as e:
print(e.message)
try:
apply_func_to_spec_operations(
spec, remove_watch_operations, operation_ids)
except PreprocessingException as e:
print(e.message)
# TODO: Kubernetes does not set a version for OpenAPI spec yet,
# remove this when that is fixed.
spec['info']['version'] = SPEC_VERSION
# TODO: Kubernetes does not set a version for OpenAPI spec yet,
# remove this when that is fixed.
spec['info']['version'] = "v1.5.0-beta.1"
with open(outfile, 'w') as out:
json.dump(spec, out, sort_keys=False, indent=2,
separators=(',', ': '), ensure_ascii=True)
return spec
def main():
if len(sys.argv) < 3:
print "Usage:\n\tpython %s infile outfile.\n" % sys.argv[0]
sys.exit(0)
if not os.path.isfile(sys.argv[1]):
print "Input file %s does not exist." % sys.argv[1]
process_swagger(sys.argv[1], sys.argv[2])
pool = urllib3.PoolManager()
with pool.request('GET', SPEC_URL, preload_content=False) as response:
if response.status != 200:
print "Error downloading spec file. Reason: %s" % response.reason
return 1
in_spec = json.load(response, object_pairs_hook=OrderedDict)
out_spec = process_swagger(in_spec)
with open(OUTPUT_PATH, 'w') as out:
json.dump(out_spec, out, sort_keys=False, indent=2,
separators=(',', ': '), ensure_ascii=True)
return 0
main()
sys.exit(main())

View File

@ -39,16 +39,8 @@ popd > /dev/null
PACKAGE_NAME=${PACKAGE_NAME:-client}
if [[ ! -n ${SWAGGER_FILE-} ]]; then
if [[ ! -n ${KUBE_ROOT-} ]]; then
echo "\${KUBE_ROOT} variable is not set"
exit
fi
SWAGGER_FILE="${KUBE_ROOT}/api/openapi-spec/swagger.json"
fi
echo "--- Preprocessing OpenAPI spec to script directory"
python "${SCRIPT_ROOT}/preprocess_spec.py" "$SWAGGER_FILE" "${SCRIPT_ROOT}/swagger.json"
echo "--- Downloading and processing OpenAPI spec"
python "${SCRIPT_ROOT}/preprocess_spec.py"
echo "--- Cleaning up previously generated folders"
rm -rf "${CLIENT_ROOT}/${PACKAGE_NAME}"

View File

@ -12,12 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from setuptools import find_packages, setup
NAME = "kubernetes"
VERSION = "1.0.0-alpha.2"
from scripts.constants import CLIENT_VERSION, PACKAGE_NAME
# To install the library, run the following
#
@ -36,8 +33,8 @@ REQUIRES = [
"ipaddress"]
setup(
name=NAME,
version=VERSION,
name=PACKAGE_NAME,
version=CLIENT_VERSION,
description="Kubernetes python client",
author_email="",
author="Kubernetes",