Skip to content

Commit

Permalink
Add -g parameter to java_crd_cmd.sh to enable generating CRD APIs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleBaiBai committed Dec 3, 2020
1 parent e219564 commit c3523de
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
9 changes: 7 additions & 2 deletions openapi/java-crd-cmd.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash
#!/usr/bin/env bash

PACKAGE_NAME=${PACKAGE_NAME:-io.kubernetes.client}
CLIENT_VERSION=${CLIENT_VERSION:-5.0-SNAPSHOT}
GENERATE_APIS=${GENERATE_APIS:false}
OUTPUT_DIR=${OUTPUT_DIR:-java}
OPENAPI_MODEL_LENGTH=${OPENAPI_MODEL_LENGTH:-}
OPENAPI_SKIP_BASE_INTERFACE=
Expand All @@ -12,14 +13,16 @@ print_usage() {
echo " -c: project version of the generated java project." >& 2
echo " -x: skips implementing kubernetes common interface (this is for backward compatibility w/ client-java lower than 9.0.0)" >& 2
echo " -n: the prefix of the target CRD's api group to generate." >& 2
echo " -g: generate crd apis." >& 2
echo " -p: the base package name of the generated java project. " >& 2
echo " -o: output directory of the generated java project. " >& 2
echo " -l: keep the n last segments for the generated class name. " >& 2
}

while getopts 'c:n:l:p:o:x' flag; do
while getopts 'c:g:n:l:p:o:x' flag; do
case "${flag}" in
c) CLIENT_VERSION="${CLIENT_VERSION}" ;;
g) GENERATE_APIS="${OPTARG}" ;;
n) KUBERNETES_CRD_GROUP_PREFIX="${OPTARG}" ;;
l) OPENAPI_MODEL_LENGTH="${OPTARG}" ;;
p) PACKAGE_NAME="${OPTARG}" ;;
Expand All @@ -33,6 +36,7 @@ done
echo "KUBERNETES_CRD_GROUP_PREFIX: $KUBERNETES_CRD_GROUP_PREFIX" >& 2
echo "OPENAPI_MODEL_LENGTH: $OPENAPI_MODEL_LENGTH" >& 2
echo "PACKAGE_NAME: $PACKAGE_NAME" >& 2
echo "GENERATE_APIS: $GENERATE_APIS" >& 2
echo "CLIENT_VERSION: $CLIENT_VERSION" >& 2
echo "OUTPUT_DIR: $OUTPUT_DIR" >& 2
echo "" >& 2 # empty line
Expand All @@ -58,6 +62,7 @@ cat > ${OUTPUT_DIR}/swagger.json.unprocessed
source "/tmp/settings"

KUBERNETES_CRD_MODE=true \
GENERATE_APIS=${GENERATE_APIS} \
OPENAPI_SKIP_FETCH_SPEC=true \
OPENAPI_MODEL_LENGTH=${OPENAPI_MODEL_LENGTH} \
KUBERNETES_CRD_GROUP_PREFIX=${KUBERNETES_CRD_GROUP_PREFIX} \
Expand Down
15 changes: 13 additions & 2 deletions openapi/java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@
<goal>generate</goal>
</goals>
<configuration>
<generateApis>false</generateApis>
<generateApis>${env.GENERATE_APIS}</generateApis>
<generateModelDocumentation>false</generateModelDocumentation>
<importMappings>V1ListMeta=io.kubernetes.client.openapi.models.V1ListMeta,V1ObjectMeta=io.kubernetes.client.openapi.models.V1ObjectMeta,IntOrString=io.kubernetes.client.custom.IntOrString,Quantity=io.kubernetes.client.custom.Quantity,V1Patch=io.kubernetes.client.custom.V1Patch</importMappings>
<importMappings>
V1ListMeta=io.kubernetes.client.openapi.models.V1ListMeta,
V1ObjectMeta=io.kubernetes.client.openapi.models.V1ObjectMeta,
IntOrString=io.kubernetes.client.custom.IntOrString,
Quantity=io.kubernetes.client.custom.Quantity,
V1Patch=io.kubernetes.client.custom.V1Patch,
V1DeleteOptions=io.kubernetes.client.openapi.models.V1DeleteOptions,
V1Status=io.kubernetes.client.openapi.models.V1Status,
</importMappings>
<configOptions>
<invokerPackage>io.kubernetes.client.openapi</invokerPackage>
</configOptions>
</configuration>
</execution>
</executions>
Expand Down
2 changes: 2 additions & 0 deletions openapi/openapi-generator/client-generator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ kubeclient::generator::generate_client() {
OPENAPI_SKIP_BASE_INTERFACE="${OPENAPI_SKIP_BASE_INTERFACE:-}"
KUBERNETES_CRD_MODE="${KUBERNETES_CRD_MODE:-}"
KUBERNETES_CRD_GROUP_PREFIX="${KUBERNETES_CRD_GROUP_PREFIX:-}"
GENERATE_APIS="${GENERATE_APIS:-true}"
USERNAME="${USERNAME:-kubernetes}"
REPOSITORY="${REPOSITORY:-kubernetes}"

Expand Down Expand Up @@ -90,6 +91,7 @@ kubeclient::generator::generate_client() {
-e OPENAPI_SKIP_FETCH_SPEC="${OPENAPI_SKIP_FETCH_SPEC}" \
-e KUBERNETES_CRD_MODE="${KUBERNETES_CRD_MODE}" \
-e KUBERNETES_CRD_GROUP_PREFIX="${KUBERNETES_CRD_GROUP_PREFIX}" \
-e GENERATE_APIS="${GENERATE_APIS}" \
-e OPENAPI_SKIP_BASE_INTERFACE="${OPENAPI_SKIP_BASE_INTERFACE}" \
-e USERNAME="${USERNAME}" \
-e REPOSITORY="${REPOSITORY}" \
Expand Down
20 changes: 16 additions & 4 deletions openapi/preprocess_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def clean_crd_meta(spec):
v['properties']['metadata'].pop('properties', None)
find_rename_ref_recursive(spec, '#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta', '#/definitions/v1.ListMeta')
find_rename_ref_recursive(spec, '#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta', '#/definitions/v1.ObjectMeta')
find_rename_ref_recursive(spec, '#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.Status', '#/definitions/v1.Status')
find_rename_ref_recursive(spec, '#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.Patch', '#/definitions/v1.Patch')
find_rename_ref_recursive(spec, '#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions', '#/definitions/v1.DeleteOptions')


def add_custom_objects_spec(spec):
Expand All @@ -160,8 +163,17 @@ def add_codegen_request_body(operation, _):
operation['x-codegen-request-body-name'] = 'body'

def drop_paths(spec):
spec['paths'] = {}

paths = {}
if (os.environ.get('GENERATE_APIS') or False):
group_prefix = os.environ.get('KUBERNETES_CRD_GROUP_PREFIX')
group_prefix_reversed = '.'.join(group_prefix.split('.')[::-1])
for k, v in spec['paths'].items():
if k.startswith('/apis/' + group_prefix_reversed):
paths[k] = v
else:
print("Ignoring non Custom Resource api path %s" %k)
spec['paths'] = paths


def process_swagger(spec, client_language, crd_mode=False):
spec = add_custom_objects_spec(spec)
Expand Down Expand Up @@ -419,7 +431,7 @@ def add_openapi_codegen_x_implement_extension(spec, client_language):
return
if os.environ.get('OPENAPI_SKIP_BASE_INTERFACE') or False:
return
for k, v in spec['definitions'].items():
for k, v in spec['definitions'].items():
if "x-kubernetes-group-version-kind" not in v:
continue
if k == "v1.Status":
Expand All @@ -428,7 +440,7 @@ def add_openapi_codegen_x_implement_extension(spec, client_language):
continue
if "metadata" not in v['properties']:
continue # not a legitimate kubernetes api object
if v["properties"]["metadata"]["$ref"] == "#/definitions/v1.ListMeta":
if v["properties"]["metadata"]["$ref"] == "#/definitions/v1.ListMeta":
v["x-implements"] = ["io.kubernetes.client.common.KubernetesListObject"]
elif v["properties"]["metadata"]["$ref"] == "#/definitions/v1.ObjectMeta":
v["x-implements"] = ["io.kubernetes.client.common.KubernetesObject"]
Expand Down

0 comments on commit c3523de

Please sign in to comment.