10000 Download OpenAPI spec directly from k8s release branch · mbohlool/client-python@4c14ab3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c14ab3

Browse files
committed
Download OpenAPI spec directly from k8s release branch
1 parent baba523 commit 4c14ab3

File tree

4 files changed

+66
-42
lines changed

4 files changed

+66
-42
lines changed

scripts/constants.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Kubernetes branch to get the OpenAPI spec from.
16+
KUBERNETES_BRANCH = "release-1.5"
17+
18+
# Spec version will be set in downloaded spec and all
19+
# generated code will refer to it.
20+
SPEC_VERSION = "v1.5.0-beta.3"
21+
22+
# client version for packaging and releasing. It can
23+
# be different than SPEC_VERSION.
24+
CLIENT_VERSION = "1.0.0-alpha.2"
25+
26+
# Name of the release package
27+
PACKAGE_NAME = "kubernetes"

scripts/preprocess_spec.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
import sys
1919
from collections import OrderedDict
2020

21+
import urllib3
22+
from constants import KUBERNETES_BRANCH, SPEC_VERSION
23+
2124
# these four constants are shown as part of this example in []:
2225
# "[watch]Pod[List]" is the deprecated version of "[list]Pod?[watch]=True"
2326
WATCH_OP_PREFIX = "watch"
2427
WATCH_OP_SUFFIX = "List"
2528
LIST_OP_PREFIX = "list"
2629
WATCH_QUERY_PARAM_NAME = "watch"
2730

31+
SPEC_URL = 'https://raw.githubusercontent.com/kubernetes/kubernetes/' \
32+
'%s/api/openapi-spec/swagger.json' % KUBERNETES_BRANCH
33+
34+
OUTPUT_PATH = os.path.join(os.path.dirname(__file__), 'swagger.json')
2835

2936
_ops = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch']
3037

@@ -94,37 +101,38 @@ def strip_tags_from_operation_id(operation, _):
94101
operation['operationId'] = operation_id
95102

96103

97-
def process_swagger(infile, outfile):
98-
with open(infile, 'r') as f:
99-
spec = json.load(f, object_pairs_hook=OrderedDict)
104+
def process_swagger(spec):
105+
apply_func_to_spec_operations(spec, strip_tags_from_operation_id)
106+
107+
operation_ids = {}
108+
apply_func_to_spec_operations(spec, lambda op, _: operator.setitem(
109+
operation_ids, op['operationId'], op))
100110

101-
apply_func_to_spec_operations(spec, strip_tags_from_operation_id)
111+
try:
112+
apply_func_to_spec_operations(
113+
spec, remove_watch_operations, operation_ids)
114+
except PreprocessingException as e:
115+
print(e.message)
102116

103-
operation_ids = {}
104-
apply_func_to_spec_operations(spec, lambda op, _: operator.setitem(
105-
operation_ids, op['operationId'], op))
117+
# TODO: Kubernetes does not set a version for OpenAPI spec yet,
118+
# remove this when that is fixed.
119+
spec['info']['version'] = SPEC_VERSION
106120

107-
try:
108-
apply_func_to_spec_operations(
109-
spec, remove_watch_operations, operation_ids)
110-
except PreprocessingException as e:
111-
print(e.message)
121+
return spec
112122

113-
# TODO: Kubernetes does not set a version for OpenAPI spec yet,
114-
# remove this when that is fixed.
115-
spec['info']['version'] = "v1.5.0-beta.1"
116123

117-
with open(outfile, 'w') as out:
118-
json.dump(spec, out, sort_keys=False, indent=2,
124+
def main():
125+
pool = urllib3.PoolManager()
126+
with pool.request('GET', SPEC_URL, preload_content=False) as response:
127+
if response.status != 200:
128+
print "Error downloading spec file. Reason: %s" % response.reason
129+
return 1
130+
in_spec = json.load(response, object_pairs_hook=OrderedDict)
131+
out_spec = process_swagger(in_spec)
132+
with open(OUTPUT_PATH, 'w') as out:
133+
json.dump(out_spec, out, sort_keys=False, indent=2,
119134
separators=(',', ': '), ensure_ascii=True)
135+
return 0
120136

121137

122-
def main():
123-
if len(sys.argv) < 3:
124-
print "Usage:\n\tpython %s infile outfile.\n" % sys.argv[0]
125-
sys.exit(0)
126-
if not os.path.isfile(sys.argv[1]):
127-
print "Input file %s does not exist." % sys.argv[1]
128-
process_swagger(sys.argv[1], sys.argv[2])
129-
130-
main()
138+
sys.exit(main())

scripts/generate.sh renamed to scripts/update-client.sh

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,8 @@ popd > /dev/null
3939

4040
PACKAGE_NAME=${PACKAGE_NAME:-client}
4141

42-
if [[ ! -n ${SWAGGER_FILE-} ]]; then
43-
if [[ ! -n ${KUBE_ROOT-} ]]; then
44-
echo "\${KUBE_ROOT} variable is not set"
45-
exit
46-
fi
47-
SWAGGER_FILE="${KUBE_ROOT}/api/openapi-spec/swagger.json"
48-
fi
49-
50-
echo "--- Preprocessing OpenAPI spec to script directory"
51-
python "${SCRIPT_ROOT}/preprocess_spec.py" "$SWAGGER_FILE" "${SCRIPT_ROOT}/swagger.json"
42+
echo "--- Downloading and processing OpenAPI spec"
43+
python "${SCRIPT_ROOT}/preprocess_spec.py"
5244

5345
echo "--- Cleaning up previously generated folders"
5446
rm -rf "${CLIENT_ROOT}/${PACKAGE_NAME}"

setup.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import sys
16-
1715
from setuptools import find_packages, setup
1816

19-
NAME = "kubernetes"
20-
VERSION = "1.0.0-alpha.2"
17+
from scripts.constants import CLIENT_VERSION, PACKAGE_NAME
2118

2219
# To install the library, run the following
2320
#
@@ -36,8 +33,8 @@
3633
"ipaddress"]
3734

3835
setup(
39-
name=NAME,
40-
version=VERSION,
36+
name=PACKAGE_NAME,
37+
version=CLIENT_VERSION,
4138
description="Kubernetes python client",
4239
author_email="",
4340
author="Kubernetes",

0 commit comments

Comments
 (0)
0