From 32084c24b4679c8ddc44749cc0d2a5cfce9955b7 Mon Sep 17 00:00:00 2001 From: James Munnelly Date: Tue, 13 Aug 2019 20:00:39 +0100 Subject: [PATCH] Add generate-groups-internal.sh script from code-generator Signed-off-by: James Munnelly --- hack/BUILD.bazel | 2 +- hack/update-codegen.sh | 3 +- third_party/k8s.io/code-generator/BUILD.bazel | 22 ++++ .../generate-groups-internal.sh | 123 ++++++++++++++++++ 4 files changed, 148 insertions(+), 2 deletions(-) create mode 100755 third_party/k8s.io/code-generator/generate-groups-internal.sh diff --git a/hack/BUILD.bazel b/hack/BUILD.bazel index 132eeef5c..a69703cc9 100644 --- a/hack/BUILD.bazel +++ b/hack/BUILD.bazel @@ -73,7 +73,7 @@ sh_binary( "//hack:update-bazel", "//hack/boilerplate:all-srcs", "//third_party/k8s.io/code-generator:generate-groups", - "//third_party/k8s.io/code-generator:openapi-gen", + "//third_party/k8s.io/code-generator:generate-groups-internal", ], ) diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index e65e92c27..891b56167 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -31,7 +31,8 @@ generate-groups.sh "deepcopy" \ --go-header-file "${runfiles}/hack/boilerplate/boilerplate.go.txt" generate-groups.sh "deepcopy,client,informer,lister" \ - github.com/jetstack/cert-manager/pkg/client github.com/jetstack/cert-manager/pkg/apis \ + github.com/jetstack/cert-manager/pkg/client \ + github.com/jetstack/cert-manager/pkg/apis \ certmanager:v1alpha1 \ --output-base "${GOPATH}/src/" \ --go-header-file "${runfiles}/hack/boilerplate/boilerplate.go.txt" diff --git a/third_party/k8s.io/code-generator/BUILD.bazel b/third_party/k8s.io/code-generator/BUILD.bazel index 35afad864..dee057755 100644 --- a/third_party/k8s.io/code-generator/BUILD.bazel +++ b/third_party/k8s.io/code-generator/BUILD.bazel @@ -3,6 +3,20 @@ licenses(["notice"]) sh_binary( name = "generate-groups", srcs = ["generate-groups.sh"], + data = [ + ":client", + ":conversion", + ":deepcopy", + ":informer", + ":lister", + ":openapi", + ], + visibility = ["//visibility:public"], +) + +sh_binary( + name = "generate-groups-internal", + srcs = ["generate-groups-internal.sh"], data = [ ":client", ":deepcopy", @@ -40,6 +54,14 @@ genrule( cmd = "cp $(SRCS) $@", ) +genrule( + name = "conversion", + srcs = ["//vendor/k8s.io/code-generator/cmd/conversion-gen"], + outs = ["conversion-gen"], + cmd = "cp $(SRCS) $@", + executable = True, +) + genrule( name = "openapi", srcs = ["//vendor/k8s.io/kube-openapi/cmd/openapi-gen"], diff --git a/third_party/k8s.io/code-generator/generate-groups-internal.sh b/third_party/k8s.io/code-generator/generate-groups-internal.sh new file mode 100755 index 000000000..979caeffd --- /dev/null +++ b/third_party/k8s.io/code-generator/generate-groups-internal.sh @@ -0,0 +1,123 @@ +#!/usr/bin/env 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. + +# With modifications for jetstack/cert-manager under the Apache2 license detailed +# in the repo root. + +set -o errexit +set -o nounset +set -o pipefail + +# generate-internal-groups generates everything for a project with internal types, e.g. an +# user-provided API server based on k8s.io/apiserver. + +if [ "$#" -lt 5 ] || [ "${1}" == "--help" ]; then + cat < ... + + the generators comma separated to run (deepcopy,defaulter,conversion,client,lister,informer,openapi) or "all". + the output package name (e.g. github.com/example/project/pkg/generated). + the internal types dir (e.g. github.com/example/project/pkg/apis). + the external types dir (e.g. github.com/example/project/pkg/apis or githubcom/example/apis). + the groups and their versions in the format "groupA:v1,v2 groupB:v1 groupC:v2", relative + to . + ... arbitrary flags passed to all generator binaries. + +Examples: + $(basename "$0") all github.com/example/project/pkg/client github.com/example/project/pkg/apis github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1" + $(basename "$0") deepcopy,defaulter,conversion github.com/example/project/pkg/client github.com/example/project/pkg/apis github.com/example/project/apis "foo:v1 bar:v1alpha1,v1beta1" +EOF + exit 0 +fi + +GENS="$1" +OUTPUT_PKG="$2" +INT_APIS_PKG="$3" +EXT_APIS_PKG="$4" +GROUPS_WITH_VERSIONS="$5" +shift 5 + +function codegen::join() { local IFS="$1"; shift; echo "$*"; } + +# enumerate group versions +ALL_FQ_APIS=() # e.g. k8s.io/kubernetes/pkg/apis/apps k8s.io/api/apps/v1 +INT_FQ_APIS=() # e.g. k8s.io/kubernetes/pkg/apis/apps +EXT_FQ_APIS=() # e.g. k8s.io/api/apps/v1 +for GVs in ${GROUPS_WITH_VERSIONS}; do + IFS=: read -r G Vs <<<"${GVs}" + + if [ -n "${INT_APIS_PKG}" ]; then + ALL_FQ_APIS+=("${INT_APIS_PKG}/${G}") + INT_FQ_APIS+=("${INT_APIS_PKG}/${G}") + fi + + # enumerate versions + for V in ${Vs//,/ }; do + ALL_FQ_APIS+=("${EXT_APIS_PKG}/${G}/${V}") + EXT_FQ_APIS+=("${EXT_APIS_PKG}/${G}/${V}") + done +done + +if [ "${GENS}" = "all" ] || grep -qw "deepcopy" <<<"${GENS}"; then + echo "Generating deepcopy funcs" + "deepcopy-gen" --input-dirs "$(codegen::join , "${ALL_FQ_APIS[@]}")" -O zz_generated.deepcopy --bounding-dirs "${INT_APIS_PKG},${EXT_APIS_PKG}" "$@" +fi + +if [ "${GENS}" = "all" ] || grep -qw "defaulter" <<<"${GENS}"; then + echo "Generating defaulters" + "defaulter-gen" --input-dirs "$(codegen::join , "${EXT_FQ_APIS[@]}")" -O zz_generated.defaults "$@" +fi + +if [ "${GENS}" = "all" ] || grep -qw "conversion" <<<"${GENS}"; then + echo "Generating conversions" + "conversion-gen" --input-dirs "$(codegen::join , "${ALL_FQ_APIS[@]}")" -O zz_generated.conversion "$@" +fi + +if [ "${GENS}" = "all" ] || grep -qw "client" <<<"${GENS}"; then + echo "Generating clientset for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}" + if [ -n "${INT_APIS_PKG}" ]; then + IFS=" " read -r -a APIS <<< "$(printf '%s/ ' "${INT_FQ_APIS[@]}")" + "client-gen" --clientset-name "${CLIENTSET_NAME_INTERNAL:-internalversion}" --input-base "" --input "$(codegen::join , "${APIS[@]}")" --output-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}" "$@" + fi + "client-gen" --clientset-name "${CLIENTSET_NAME_VERSIONED:-versioned}" --input-base "" --input "$(codegen::join , "${EXT_FQ_APIS[@]}")" --output-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}" "$@" +fi + +if [ "${GENS}" = "all" ] || grep -qw "lister" <<<"${GENS}"; then + echo "Generating listers for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/listers" + "lister-gen" --input-dirs "$(codegen::join , "${ALL_FQ_APIS[@]}")" --output-package "${OUTPUT_PKG}/listers" "$@" +fi + +if [ "${GENS}" = "all" ] || grep -qw "informer" <<<"${GENS}"; then + echo "Generating informers for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/informers" + "informer-gen" \ + --input-dirs "$(codegen::join , "${ALL_FQ_APIS[@]}")" \ + --versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}/${CLIENTSET_NAME_VERSIONED:-versioned}" \ + --internal-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}/${CLIENTSET_NAME_INTERNAL:-internalversion}" \ + --listers-package "${OUTPUT_PKG}/listers" \ + --output-package "${OUTPUT_PKG}/informers" \ + "$@" +fi + +if [ "${GENS}" = "all" ] || grep -qw "openapi" <<<"${GENS}"; then + echo "Generating OpenAPI definitions for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/openapi" + declare -a OPENAPI_EXTRA_PACKAGES + "openapi-gen" \ + --input-dirs "$(codegen::join , "${EXT_FQ_APIS[@]}" "${OPENAPI_EXTRA_PACKAGES[@]}")" \ + --input-dirs "k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/runtime,k8s.io/apimachinery/pkg/version" \ + --output-package "${OUTPUT_PKG}/openapi" \ + -O zz_generated.openapi \ + "$@" +fi