You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate-groups.sh 3.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env bash
  2. # Copyright 2017 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. # generate-groups generates everything for a project with external types only, e.g. a project based
  19. # on CustomResourceDefinitions.
  20. if [ "$#" -lt 4 ] || [ "${1}" == "--help" ]; then
  21. cat <<EOF
  22. Usage: $(basename "$0") <generators> <output-package> <apis-package> <groups-versions> ...
  23. <generators> the generators comma separated to run (deepcopy,defaulter,client,lister,informer) or "all".
  24. <output-package> the output package name (e.g. github.com/example/project/pkg/generated).
  25. <apis-package> the external types dir (e.g. github.com/example/api or github.com/example/project/pkg/apis).
  26. <groups-versions> the groups and their versions in the format "groupA:v1,v2 groupB:v1 groupC:v2", relative
  27. to <api-package>.
  28. ... arbitrary flags passed to all generator binaries.
  29. Examples:
  30. $(basename "$0") all github.com/example/project/pkg/client github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1"
  31. $(basename "$0") deepcopy,client github.com/example/project/pkg/client github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1"
  32. EOF
  33. exit 0
  34. fi
  35. GENS="$1"
  36. OUTPUT_PKG="$2"
  37. APIS_PKG="$3"
  38. GROUPS_WITH_VERSIONS="$4"
  39. shift 4
  40. (
  41. # To support running this script from anywhere, we have to first cd into this directory
  42. # so we can install the tools.
  43. cd "$(dirname "${0}")"
  44. GO111MODULE=on go install k8s.io/code-generator/cmd/{defaulter-gen,client-gen,lister-gen,informer-gen,deepcopy-gen}
  45. )
  46. function codegen::join() { local IFS="$1"; shift; echo "$*"; }
  47. # enumerate group versions
  48. FQ_APIS=() # e.g. k8s.io/api/apps/v1
  49. for GVs in ${GROUPS_WITH_VERSIONS}; do
  50. IFS=: read -r G Vs <<<"${GVs}"
  51. # enumerate versions
  52. for V in ${Vs//,/ }; do
  53. FQ_APIS+=("${APIS_PKG}/${G}/${V}")
  54. done
  55. done
  56. if [ "${GENS}" = "all" ] || grep -qw "deepcopy" <<<"${GENS}"; then
  57. echo "Generating deepcopy funcs"
  58. "${GOPATH}/bin/deepcopy-gen" --input-dirs "$(codegen::join , "${FQ_APIS[@]}")" -O zz_generated.deepcopy --bounding-dirs "${APIS_PKG}" "$@"
  59. fi
  60. if [ "${GENS}" = "all" ] || grep -qw "client" <<<"${GENS}"; then
  61. echo "Generating clientset for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}"
  62. "${GOPATH}/bin/client-gen" --clientset-name "${CLIENTSET_NAME_VERSIONED:-versioned}" --input-base "" --input "$(codegen::join , "${FQ_APIS[@]}")" --output-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}" "$@"
  63. fi
  64. if [ "${GENS}" = "all" ] || grep -qw "lister" <<<"${GENS}"; then
  65. echo "Generating listers for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/listers"
  66. "${GOPATH}/bin/lister-gen" --input-dirs "$(codegen::join , "${FQ_APIS[@]}")" --output-package "${OUTPUT_PKG}/listers" "$@"
  67. fi
  68. if [ "${GENS}" = "all" ] || grep -qw "informer" <<<"${GENS}"; then
  69. echo "Generating informers for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/informers"
  70. "${GOPATH}/bin/informer-gen" \
  71. --input-dirs "$(codegen::join , "${FQ_APIS[@]}")" \
  72. --versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}/${CLIENTSET_NAME_VERSIONED:-versioned}" \
  73. --listers-package "${OUTPUT_PKG}/listers" \
  74. --output-package "${OUTPUT_PKG}/informers" \
  75. "$@"
  76. fi