Skip to content

Commit

Permalink
feat: introduce openapi-generator (#95)
Browse files Browse the repository at this point in the history
* move files related to swagger-codegen to dedicated directory

* add script to build clients by openapi-generator

* python-asyncio is build by openapi-generator
  • Loading branch information
tomplus authored and k8s-ci-robot committed Jan 15, 2019
1 parent e41b173 commit d0821e6
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 121 deletions.
2 changes: 1 addition & 1 deletion openapi/csharp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pushd "${OUTPUT_DIR}" > /dev/null
OUTPUT_DIR=`pwd`
popd > /dev/null

source "${SCRIPT_ROOT}/client-generator.sh"
source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh"
source "${SETTING_FILE}"

# this is to ensure sed after docker build has perm to modify files generated
Expand Down
2 changes: 1 addition & 1 deletion openapi/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null
OUTPUT_DIR=`pwd`
popd > /dev/null

source "${SCRIPT_ROOT}/client-generator.sh"
source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh"
source "${SETTING_FILE}"

SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT:-v2.3.0}"; \
Expand Down
2 changes: 1 addition & 1 deletion openapi/haskell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null
OUTPUT_DIR=`pwd`
popd > /dev/null

source "${SCRIPT_ROOT}/client-generator.sh"
source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh"
source "${SETTING_FILE}"

# Latest version of HaskellHttpClientCodegen.java as of Nov 19, 2018
Expand Down
2 changes: 1 addition & 1 deletion openapi/java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pushd "${OUTPUT_DIR}" > /dev/null
OUTPUT_DIR=`pwd`
popd > /dev/null

source "${SCRIPT_ROOT}/client-generator.sh"
source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh"
source "${SETTING_FILE}"

SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT:-5d263e1c9cdd395d93adf061c63d5ef58a8e9ec5}"; \
Expand Down
2 changes: 1 addition & 1 deletion openapi/javascript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pushd "${OUTPUT_DIR}" > /dev/null
OUTPUT_DIR=`pwd`
popd > /dev/null

source "${SCRIPT_ROOT}/client-generator.sh"
source "${SCRIPT_ROOT}/swagger-codegen/client-generator.sh"
source "${SETTING_FILE}"

SWAGGER_CODEGEN_COMMIT="${SWAGGER_CODEGEN_COMMIT:-5d263e1c9cdd395d93adf061c63d5ef58a8e9ec5}"; \
Expand Down
66 changes: 66 additions & 0 deletions openapi/openapi-generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2017 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.

FROM maven:3.5-jdk-8-slim
ARG OPENAPI_GENERATOR_COMMIT
ARG GENERATION_XML_FILE
ARG OPENAPI_GENERATOR_USER_ORG=OpenAPITools

# Install preprocessing script requirements
RUN apt-get update && apt-get -y install git python-pip && pip install urllib3

# Install Autorest
RUN apt-get update && apt-get -qq -y install libunwind8 libicu57 libssl1.0 liblttng-ust0 libcurl3 libuuid1 libkrb5-3 zlib1g
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get -y install \
nodejs \
libunwind8-dev \
&& rm -rf /var/lib/apt/lists/*

RUN npm install -g autorest

# Check out specific commit of openapi-generator
RUN mkdir /source && \
cd /source && \
git clone -n https://github.com/${OPENAPI_GENERATOR_USER_ORG}/openapi-generator.git && \
cd openapi-generator && \
git checkout $OPENAPI_GENERATOR_COMMIT

# Build it and persist local repository
RUN mkdir /.npm && chmod -R go+rwx /.npm && chmod -R go+rwx /root && umask 0 && cd /source/openapi-generator && \
mvn install -DskipTests -Dmaven.test.skip=true -pl modules/openapi-generator-maven-plugin -am && \
cp -r /root/.m2/* /usr/share/maven/ref

RUN mkdir -p /node_modules && chmod -R go+rwx /node_modules
RUN npm install @microsoft.azure/autorest.csharp \
@microsoft.azure/autorest.modeler

RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg
RUN mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/
RUN curl https://packages.microsoft.com/config/debian/9/prod.list > prod.list
RUN mv prod.list /etc/apt/sources.list.d/microsoft-prod.list
RUN chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg
RUN chown root:root /etc/apt/sources.list.d/microsoft-prod.list

RUN apt-get update
RUN apt-get install -yy -q dotnet-hosting-2.0.8


# Copy required files
COPY openapi-generator/generate_client_in_container.sh /generate_client.sh
COPY preprocess_spec.py /
COPY custom_objects_spec.json /
COPY ${GENERATION_XML_FILE} /generation_params.xml

ENTRYPOINT ["mvn-entrypoint.sh", "/generate_client.sh"]
90 changes: 90 additions & 0 deletions openapi/openapi-generator/client-generator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash

# Copyright 2015 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.

# Script to fetch latest swagger spec.
# Puts the updated spec at api/swagger-spec/

set -o errexit
set -o nounset
set -o pipefail

# Generates client.
# Required env vars:
# CLEANUP_DIRS: List of directories to cleanup before generation for this language
# KUBERNETES_BRANCH: Kubernetes branch name to get the swagger spec from
# CLIENT_VERSION: Client version. Will be used in the comment sections of the generated code
# PACKAGE_NAME: Name of the client package.
# CLIENT_LANGUAGE: Language of the client. ${CLIENT_LANGUAGE}.xml should exists.
# Optional env vars:
# OPENAPI_GENERATOR_USER_ORG: openapi-generator-user-org
# OPENAPI_GENERATOR_COMMIT: openapi-generator-version
# Input vars:
# $1: output directory
kubeclient::generator::generate_client() {
: "${CLEANUP_DIRS?Must set CLEANUP_DIRS env var}"
: "${KUBERNETES_BRANCH?Must set KUBERNETES_BRANCH env var}"
: "${CLIENT_VERSION?Must set CLIENT_VERSION env var}"
: "${PACKAGE_NAME?Must set PACKAGE_NAME env var}"
: "${CLIENT_LANGUAGE?Must set CLIENT_LANGUAGE env var}"

OPENAPI_GENERATOR_USER_ORG="${OPENAPI_GENERATOR_USER_ORG:-OpenAPITools}"
OPENAPI_GENERATOR_COMMIT="${OPENAPI_GENERATOR_COMMIT:-v3.3.4}"
USERNAME="${USERNAME:-kubernetes}"
REPOSITORY="${REPOSITORY:-kubernetes}"

local output_dir=$1
pushd "${output_dir}" > /dev/null
local output_dir=`pwd`
popd > /dev/null
local SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")
pushd "${SCRIPT_ROOT}" > /dev/null
local SCRIPT_ROOT=`pwd`
popd > /dev/null

mkdir -p "${output_dir}"

if [ "${USERNAME}" != "kubernetes" ]; then
image_name="${USERNAME}-${REPOSITORY}-${CLIENT_LANGUAGE}-client-gen-with-openapi-generator:v1"
else
image_name="${REPOSITORY}-${CLIENT_LANGUAGE}-client-gen-with-openapi-generator:v1"
fi

echo "--- Building docker image ${image_name}..."
docker build "${SCRIPT_ROOT}"/../ -f "${SCRIPT_ROOT}/Dockerfile" -t "${image_name}" \
--build-arg OPENAPI_GENERATOR_USER_ORG="${OPENAPI_GENERATOR_USER_ORG}" \
--build-arg OPENAPI_GENERATOR_COMMIT="${OPENAPI_GENERATOR_COMMIT}" \
--build-arg GENERATION_XML_FILE="${CLIENT_LANGUAGE}.xml"

# Docker does not support passing arrays, pass the string representation
# of the array instead (space separated)
CLEANUP_DIRS_STRING="${CLEANUP_DIRS[@]}"

echo "--- Running generator inside container..."
docker run -u $(id -u) \
-e CLEANUP_DIRS="${CLEANUP_DIRS_STRING}" \
-e KUBERNETES_BRANCH="${KUBERNETES_BRANCH}" \
-e CLIENT_VERSION="${CLIENT_VERSION}" \
-e CLIENT_LANGUAGE="${CLIENT_LANGUAGE}" \
-e PACKAGE_NAME="${PACKAGE_NAME}" \
-e OPENAPI_GENERATOR_USER_ORG="${OPENAPI_GENERATOR_USER_ORG}" \
-e OPENAPI_GENERATOR_COMMIT="${OPENAPI_GENERATOR_COMMIT}" \
-e USERNAME="${USERNAME}" \
-e REPOSITORY="${REPOSITORY}" \
-v "${output_dir}:/output_dir" \
"${image_name}" "/output_dir"

echo "---Done."
}
103 changes: 103 additions & 0 deletions openapi/openapi-generator/generate_client_in_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/bash

# Copyright 2017 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.

# Script to fetch latest swagger spec.
# Puts the updated spec at api/swagger-spec/

set -o errexit
set -o nounset
set -o pipefail

# Generates client.
# Required env vars:
# CLEANUP_DIRS: List of directories (string separated by space) to cleanup before generation for this language
# KUBERNETES_BRANCH: Kubernetes branch name to get the swagger spec from
# CLIENT_VERSION: Client version. Will be used in the comment sections of the generated code
# PACKAGE_NAME: Name of the client package.
# OPENAPI_GENERATOR_COMMIT: openapi-generator commit sha or tag/branch name. Will only be used as a reference in docs.
# Input vars:
# $1: output directory
: "${CLEANUP_DIRS?Must set CLEANUP_DIRS env var}"
: "${KUBERNETES_BRANCH?Must set KUBERNETES_BRANCH env var}"
: "${CLIENT_VERSION?Must set CLIENT_VERSION env var}"
: "${CLIENT_LANGUAGE?Must set CLIENT_LANGUAGE env var}"
: "${PACKAGE_NAME?Must set PACKAGE_NAME env var}"
: "${OPENAPI_GENERATOR_COMMIT?Must set OPENAPI_GENERATOR_COMMIT env var}"

output_dir=$1
pushd "${output_dir}" > /dev/null
output_dir=`pwd`
popd > /dev/null
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")
pushd "${SCRIPT_ROOT}" > /dev/null
SCRIPT_ROOT=`pwd`
popd > /dev/null

if ! which mvn > /dev/null 2>&1; then
echo "Maven is not installed."
exit
fi

# There should be only one version of openapi-generator-maven-plugin.
unset PLUGIN_VERSION
shopt -s nullglob
FOLDERS=(/root/.m2/repository/org/openapitools/openapi-generator-maven-plugin/*)
for folder in "${FOLDERS[@]}"; do
if [[ -d "${folder}" ]]; then
folder=$(basename "${folder}")
if [[ ! -z "${PLUGIN_VERSION:-}" ]]; then
echo "Multiple openapi-generator-maven-plugin version exists: ${PLUGIN_VERSION} & ${folder}"
exit 1
fi
PLUGIN_VERSION="${folder}"
fi
done
if [[ -z "${PLUGIN_VERSION:-}" ]]; then
echo "Cannot find openapi-generator-maven-plugin version"
exit 1
fi
shopt -u nullglob

# To make sure we can reproduce generation, we would also log code-gen exact commit
pushd /source/openapi-generator
OPENAPI_GENERATOR_COMMIT_ACTUAL=$(git rev-parse HEAD)
popd

mkdir -p "${output_dir}"

echo "--- Downloading and pre-processing OpenAPI spec"
python "${SCRIPT_ROOT}/preprocess_spec.py" "${CLIENT_LANGUAGE}" "${KUBERNETES_BRANCH}" "${output_dir}/swagger.json" "${USERNAME}" "${REPOSITORY}"

echo "--- Cleaning up previously generated folders"
for i in ${CLEANUP_DIRS}; do
echo "--- Cleaning up ${output_dir}/${i}"
rm -rf "${output_dir}/${i}"
done

echo "--- Generating client ..."
mvn -f "${SCRIPT_ROOT}/generation_params.xml" clean generate-sources \
-Dgenerator.spec.path="${output_dir}/swagger.json" \
-Dgenerator.output.path="${output_dir}" \
-D=generator.client.version="${CLIENT_VERSION}" \
-D=generator.package.name="${PACKAGE_NAME}" \
-D=openapi-generator-version="${PLUGIN_VERSION}" \
-Duser.home=/root

mkdir -p "${output_dir}/.openapi-generator"
echo "Requested Commit: ${OPENAPI_GENERATOR_COMMIT}" > "${output_dir}/.openapi-generator/COMMIT"
echo "Actual Commit: ${OPENAPI_GENERATOR_COMMIT_ACTUAL}" >> "${output_dir}/.openapi-generator/COMMIT"

echo "---Done."
27 changes: 0 additions & 27 deletions openapi/python-asyncio-api_client.py.patch

This file was deleted.

39 changes: 0 additions & 39 deletions openapi/python-asyncio-rest.py.patch

This file was deleted.

Loading

0 comments on commit d0821e6

Please sign in to comment.