2020-08-13 21:10:41 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Copyright 2020 VMware, Inc.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
set -euo pipefail
|
2020-08-18 15:06:59 +00:00
|
|
|
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
2020-08-13 21:10:41 +00:00
|
|
|
|
|
|
|
GOPATH="${GOPATH:-$(mktemp -d)}"
|
|
|
|
|
2020-08-18 17:18:14 +00:00
|
|
|
K8S_PKG_VERSION="${K8S_PKG_VERSION:-"1.19"}"
|
2020-08-13 21:10:41 +00:00
|
|
|
CODEGEN_IMAGE=${CODEGEN_IMAGE:-"gcr.io/tanzu-user-authentication/k8s-code-generator-${K8S_PKG_VERSION}:latest"}
|
|
|
|
|
2020-08-20 17:54:15 +00:00
|
|
|
BASE_PKG="github.com/suzerain-io/pinniped"
|
2020-08-13 21:10:41 +00:00
|
|
|
|
2020-08-21 19:30:50 +00:00
|
|
|
# This script assumes that your current working directory is the top of the module
|
|
|
|
# in which you would like to generate code.
|
|
|
|
MOD_DIR=$(pwd)
|
|
|
|
|
2020-08-13 21:10:41 +00:00
|
|
|
function codegen::ensure_module_in_gopath() {
|
2020-08-18 13:22:44 +00:00
|
|
|
# This should be something like "kubernetes/1.19/api".
|
|
|
|
local pkg_name="$(realpath "--relative-to=$ROOT" "$MOD_DIR")"
|
|
|
|
|
|
|
|
# Use --canonicalize-missing to since pkg_name could end up as "." - this would
|
|
|
|
# lead to a pkg_gosrc_path like "foo/bar/bat/." which ln(1) (below) does not like.
|
|
|
|
local pkg_gosrc_path="$(realpath --canonicalize-missing "${GOPATH}/src/${BASE_PKG}/${pkg_name}")"
|
2020-08-13 21:10:41 +00:00
|
|
|
|
|
|
|
if [[ ! -e "${pkg_gosrc_path}" ]]; then
|
|
|
|
mkdir -p "$(dirname "${pkg_gosrc_path}")"
|
|
|
|
ln -s "${ROOT}/${pkg_name}" "${pkg_gosrc_path}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function codegen::invoke_code_generator() {
|
|
|
|
local generator_command="${1}"
|
|
|
|
local mod_basename_for_version="${2}"
|
|
|
|
shift 2 # generator args are now in $@
|
|
|
|
|
|
|
|
if [ "${BASH_VERSINFO[0]}" -lt 5 ]; then
|
|
|
|
echo "ERROR: invalid BASH version"
|
|
|
|
echo " using v${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}.${BASH_VERSINFO[2]} @ ${BASH}"
|
|
|
|
echo " require v5.0.0+"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
bash "${GOPATH}/src/k8s.io/code-generator/${generator_command}.sh" \
|
|
|
|
"$@" \
|
|
|
|
--go-header-file "${ROOT}/hack/boilerplate.go.txt" |
|
|
|
|
sed "s|^|${mod_basename_for_version} > ${generator_command} > |"
|
|
|
|
}
|
|
|
|
|
|
|
|
function codegen::generate_for_module() {
|
|
|
|
local mod_basename_for_version="${1}"
|
|
|
|
|
|
|
|
case "${mod_basename_for_version}" in
|
|
|
|
1.19/api)
|
2020-08-21 19:30:50 +00:00
|
|
|
echo "GENERATING CODE for $mod_basename_for_version"
|
2020-08-13 21:10:41 +00:00
|
|
|
codegen::invoke_code_generator generate-groups "${mod_basename_for_version}" \
|
|
|
|
deepcopy,defaulter \
|
|
|
|
"${BASE_PKG}/kubernetes/1.19/api/generated" \
|
|
|
|
"${BASE_PKG}/kubernetes/1.19/api/apis" \
|
2020-08-20 17:54:15 +00:00
|
|
|
"pinniped:v1alpha1 crdpinniped:v1alpha1"
|
2020-08-13 21:10:41 +00:00
|
|
|
codegen::invoke_code_generator generate-internal-groups "${mod_basename_for_version}" \
|
|
|
|
deepcopy,defaulter,conversion,openapi \
|
|
|
|
"${BASE_PKG}/kubernetes/1.19/api/generated" \
|
|
|
|
"${BASE_PKG}/kubernetes/1.19/api/apis" \
|
|
|
|
"${BASE_PKG}/kubernetes/1.19/api/apis" \
|
2020-08-20 17:54:15 +00:00
|
|
|
"pinniped:v1alpha1 crdpinniped:v1alpha1"
|
2020-08-13 21:10:41 +00:00
|
|
|
;;
|
|
|
|
1.19/client-go)
|
2020-08-21 19:30:50 +00:00
|
|
|
echo "GENERATING CODE for $mod_basename_for_version"
|
2020-08-13 21:10:41 +00:00
|
|
|
codegen::invoke_code_generator generate-groups "${mod_basename_for_version}" \
|
|
|
|
client,lister,informer \
|
|
|
|
"${BASE_PKG}/kubernetes/1.19/client-go" \
|
|
|
|
"${BASE_PKG}/kubernetes/1.19/api/apis" \
|
2020-08-20 17:54:15 +00:00
|
|
|
"pinniped:v1alpha1 crdpinniped:v1alpha1"
|
2020-08-13 21:10:41 +00:00
|
|
|
;;
|
2020-08-21 19:30:50 +00:00
|
|
|
*)
|
|
|
|
echo "Skipping $mod_basename_for_version because it does not contain any code to generate"
|
2020-08-13 21:10:41 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
function codegen::generate() {
|
2020-08-15 00:34:21 +00:00
|
|
|
local mod_basename_for_version
|
|
|
|
mod_basename_for_version="${K8S_PKG_VERSION}/$(basename "${MOD_DIR}")"
|
|
|
|
|
2020-08-13 21:10:41 +00:00
|
|
|
codegen::ensure_module_in_gopath
|
2020-08-15 00:34:21 +00:00
|
|
|
codegen::generate_for_module "${mod_basename_for_version}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function codegen::verify() {
|
|
|
|
local have_stash=''
|
|
|
|
if [[ "$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')" -ne "0" ]]; then
|
2020-08-18 13:22:44 +00:00
|
|
|
# git stash requires the user.email and user.name to be set. We set these at
|
|
|
|
# a global scope so they don't overwrite the .git/config in the mounted repo
|
|
|
|
# from the host.
|
|
|
|
git config --global user.email "codegen_verify@whatever.com"
|
|
|
|
git config --global user.name "Codegen Verify"
|
2020-08-15 00:34:21 +00:00
|
|
|
git stash --all >/dev/null 2>&1 && have_stash=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
codegen::generate
|
|
|
|
|
2020-08-18 13:22:44 +00:00
|
|
|
failure=0
|
2020-08-15 00:34:21 +00:00
|
|
|
if [[ "$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')" -eq "0" ]]; then
|
|
|
|
echo "Generated code in ${MOD_DIR} up to date."
|
|
|
|
else
|
|
|
|
echo "Generated code in ${MOD_DIR} is out of date."
|
|
|
|
echo "Please run hack/module.sh codegen"
|
|
|
|
git diff "${ROOT}"
|
|
|
|
git checkout "${ROOT}"
|
2020-08-18 13:22:44 +00:00
|
|
|
failure=1
|
2020-08-15 00:34:21 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "${have_stash}" ]]; then
|
|
|
|
git stash pop >/dev/null 2>&1
|
|
|
|
fi
|
2020-08-18 13:22:44 +00:00
|
|
|
|
|
|
|
if [[ "$failure" -eq 1 ]]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-08-13 21:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function codegen::usage() {
|
|
|
|
echo "Error: <codegen command> must be specified"
|
2020-08-18 13:22:44 +00:00
|
|
|
echo " ${BASH_SOURCE[0]} <codegen command> [codegen::generate, codegen::verify]"
|
2020-08-13 21:10:41 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function codegen::main() {
|
|
|
|
local codegen_command="${1}"
|
|
|
|
|
|
|
|
if [[ -n "${CONTAINED:-}" ]]; then
|
|
|
|
"${codegen_command}"
|
|
|
|
else
|
|
|
|
DOCKER_ROOT_DIR="/tmp/${RANDOM}/${BASE_PKG}"
|
2020-08-18 13:22:44 +00:00
|
|
|
DOCKER_MOD_DIR="${DOCKER_ROOT_DIR}/$(realpath "--relative-to=$ROOT" "$MOD_DIR")"
|
2020-08-13 21:10:41 +00:00
|
|
|
|
|
|
|
docker run --rm \
|
|
|
|
--env CONTAINED=1 \
|
|
|
|
--env MOD_DIR="${DOCKER_MOD_DIR}" \
|
|
|
|
--volume "${ROOT}:${DOCKER_ROOT_DIR}" \
|
|
|
|
--workdir "${DOCKER_MOD_DIR}" \
|
|
|
|
"${CODEGEN_IMAGE}" \
|
2020-08-18 15:06:59 +00:00
|
|
|
"${DOCKER_ROOT_DIR}/hack/lib/$(basename "${BASH_SOURCE[0]}")" \
|
2020-08-13 21:10:41 +00:00
|
|
|
"${codegen_command}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
codegen::main "${1:-"codegen::usage"}"
|