Skip to content

Commit b1df899

Browse files
committed
Add javascript generator
1 parent ab6055d commit b1df899

8 files changed

+441
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Intellij IDEA files
2+
.idea/*
3+
*.iml
4+

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,21 @@ Common generator scripts for all client libraries
1111

1212
[![Client Support Level](https://img.shields.io/badge/Kubernetes%20client-Gold-blue.svg?style=plastic&colorB=FFD700&colorA=306CE8)](https://github.com/kubernetes-client)
1313

14+
#Generating a client
15+
To generate a client, first make sure the client generator exists. For any language other than
16+
go, check `openapi/` folder for a script with `${CLIENT_LANGUAGE}.sh` and run this command:
17+
18+
```bash
19+
${CLIENT_LANGUAGE}.sh OUTPUT_DIR SETTING_FILE
20+
```
21+
22+
`SETTING_FILE` is a bash script exporting required setting to generate a client. These
23+
are normally:
24+
25+
- `KUBERNETES_BRANCH`: The kubernetes branch to get OpenAPI spec from.
26+
- `CLIENT_VERSION`: Client version string. e.g. 1.0.0b1
27+
- `PACKAGE_NAME`: Package name for the generated client. e.g. "kubernetes"
28+
29+
Settings may differ from language to language. Refer to language script's help page for
30+
more information.
31+

openapi/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Share generator files for all OpenAPI based clients (non-go clients) such as python.

openapi/client-generator.sh

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
3+
# Copyright 2015 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Script to fetch latest swagger spec.
18+
# Puts the updated spec at api/swagger-spec/
19+
20+
set -o errexit
21+
set -o nounset
22+
set -o pipefail
23+
24+
# Generates client.
25+
# Required env vars:
26+
# CLEANUP_DIRS: List of directories to cleanup before generation for this language
27+
# Input vars:
28+
# $1: output directory
29+
kubeclient::generator::generate_client() {
30+
: "${CLEANUP_DIRS?Must set CLEANUP_DIRS env var}"
31+
: "${KUBERNETES_BRANCH?Must set KUBERNETES_BRANCH env var}"
32+
: "${CLIENT_VERSION?Must set CLIENT_VERSION env var}"
33+
: "${PACKAGE_NAME?Must set PACKAGE_NAME env var}"
34+
: "${CLIENT_LANGUAGE?Must set CLIENT_LANGUAGE env var}"
35+
36+
local output_dir=$1
37+
pushd "${output_dir}" > /dev/null
38+
local output_dir=`pwd`
39+
popd > /dev/null
40+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")
41+
pushd "${SCRIPT_ROOT}" > /dev/null
42+
local SCRIPT_ROOT=`pwd`
43+
popd > /dev/null
44+
45+
if ! which mvn > /dev/null 2>&1; then
46+
echo "Maven is not installed."
47+
exit
48+
fi
49+
50+
mkdir -p "${output_dir}"
51+
52+
echo "--- Downloading and pre-processing OpenAPI spec"
53+
python "${SCRIPT_ROOT}/preprocess_spec.py" "${KUBERNETES_BRANCH}" "${output_dir}/swagger.json"
54+
55+
echo "--- Cleaning up previously generated folders"
56+
for i in ${CLEANUP_DIRS[@]}; do
57+
rm -rf "${output_dir}/${i}"
58+
done
59+
60+
echo "--- Generating client ..."
61+
mvn -f "${SCRIPT_ROOT}/${CLIENT_LANGUAGE}.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}"
62+
63+
echo "--- Patching generated code..."
64+
find "${output_dir}/test" -type f -name \*.py -exec sed -i 's/\bclient/kubernetes.client/g' {} +
65+
find "${output_dir}/" -type f -name \*.md -exec sed -i 's/\bclient/kubernetes.client/g' {} +
66+
find "${output_dir}/" -type f -name \*.md -exec sed -i "s/kubernetes.client-${CLIENT_LANGUAGE}/client-${CLIENT_LANGUAGE}/g" {} +
67+
echo "---Done."
68+
}

openapi/javascript.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
# Copyright 2015 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Script to fetch latest swagger spec.
18+
# Puts the updated spec at api/swagger-spec/
19+
20+
set -o errexit
21+
set -o nounset
22+
set -o pipefail
23+
24+
ARGC=$#
25+
26+
if [ $# -ne 2 ]; then
27+
echo "Usage:\n\n\tjavascript.sh OUTPUT_DIR SETTING_FILE_PATH"
28+
echo "\nSetting file should define KUBERNETES_BRANCH, CLIENT_VERSION, and PACKAGE_NAME\n"
29+
exit 1
30+
fi
31+
32+
33+
OUTPUT_DIR=$1
34+
SETTING_FILE=$2
35+
mkdir -p "${OUTPUT_DIR}"
36+
37+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")
38+
pushd "${SCRIPT_ROOT}" > /dev/null
39+
SCRIPT_ROOT=`pwd`
40+
popd > /dev/null
41+
42+
pushd "${OUTPUT_DIR}" > /dev/null
43+
OUTPUT_DIR=`pwd`
44+
popd > /dev/null
45+
46+
source "${SCRIPT_ROOT}/client-generator.sh"
47+
source "${SETTING_FILE}"
48+
49+
CLIENT_LANGUAGE=javascript; CLEANUP_DIRS=(docs src target gradle); kubeclient::generator::generate_client "${OUTPUT_DIR}"

openapi/javascript.xml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>io.kubernetes</groupId>
4+
<artifactId>client-python</artifactId>
5+
<version>1.0-SNAPSHOT</version>
6+
<name>client-python</name>
7+
<url>http://kubernetes.io</url>
8+
<build>
9+
<plugins>
10+
<plugin>
11+
<groupId>io.swagger</groupId>
12+
<artifactId>swagger-codegen-maven-plugin</artifactId>
13+
<version>2.2.2-SNAPSHOT</version>
14+
<executions>
15+
<execution>
16+
<goals>
17+
<goal>generate</goal>
18+
</goals>
19+
<configuration>
20+
<inputSpec>${generator.spec.path}</inputSpec>
21+
<language>javascript</language>
22+
<gitUserId>kubernetes-incubator</gitUserId>
23+
<gitRepoId>client-javascript</gitRepoId>
24+
<configOptions>
25+
<projectName>kubernetes-js-client</projectName>
26+
<projectDescription>Javascript client for Kubernetes.</projectDescription>
27+
<projectVersion>${generator.client.version}</projectVersion>
28+
<projectLicenseName>Apache V2</projectLicenseName>
29+
<sortParamsByRequiredFlag>true</sortParamsByRequiredFlag>
30+
<modelPackage>${generator.package.name}.models</modelPackage>
31+
<apiPackage>${generator.package.name}.apis</apiPackage>
32+
<invokerPackage>${generator.package.name}</invokerPackage>
33+
</configOptions>
34+
<output>${generator.output.path}</output>
35+
</configuration>
36+
</execution>
37+
</executions>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
<dependencies>
42+
<!-- dependencies are needed for the client being generated -->
43+
<dependency>
44+
<groupId>io.swagger</groupId>
45+
<artifactId>swagger-annotations</artifactId>
46+
<version>${swagger-annotations-version}</version>
47+
</dependency>
48+
</dependencies>
49+
<properties>
50+
<swagger-annotations-version>1.5.0</swagger-annotations-version>
51+
<maven-plugin-version>1.0.0</maven-plugin-version>
52+
53+
<!-- Default values for the generator parameters. -->
54+
<generator.output.path>.</generator.output.path>
55+
<generator.spec.path>swagger.json</generator.spec.path>
56+
<generator.package.name>client</generator.package.name>
57+
<generator.client.version>1.0-snapshot</generator.client.version>
58+
</properties>
59+
</project>

0 commit comments

Comments
 (0)