Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen: Fix the hack/update-codegen.sh template #564

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions changelog/v0.36.2/fix-update-codegen-template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
changelog:
- type: FIX
issueLink: https://github.com/solo-io/gloo/issues/9683
resolvesIssue: false
description: >-
Fix the hack/update-codegen.sh bash script to work with Kubernetes 1.31
code generation. Previously, consumers were unable to run the generated
hack/update-codegen.sh script due to an invalid "source" command. Additionally,
the script has been adapted to run each k8s.io/code-generator binary directly
instead of interacting with the new bash script.
1 change: 1 addition & 0 deletions ci/oss_compliance/osa_provided.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Name|Version|License
[k8s.io/apimachinery](https://k8s.io/apimachinery)|v0.31.0|Apache License 2.0
[k8s.io/client-go](https://k8s.io/client-go)|v0.31.0|Apache License 2.0
[k8s.io/code-generator](https://k8s.io/code-generator)|v0.31.0|Apache License 2.0
[gengo/v2](https://k8s.io/gengo/v2)|v2.0.0-20240228010128-51d4e06bde70|Apache License 2.0
[k8s.io/utils](https://k8s.io/utils)|v0.0.0-20240711033017-18e509b52bc8|Apache License 2.0
[structured-merge-diff/v4](https://sigs.k8s.io/structured-merge-diff/v4)|v4.4.1|Apache License 2.0
[sigs.k8s.io/yaml](https://sigs.k8s.io/yaml)|v1.4.0|MIT License
Expand Down
1 change: 1 addition & 0 deletions ci/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ import (
_ "github.com/solo-io/protoc-gen-ext"
_ "github.com/solo-io/protoc-gen-openapi"
_ "k8s.io/code-generator"
_ "k8s.io/gengo/v2"
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (
k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0
k8s.io/code-generator v0.31.0
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
sigs.k8s.io/structured-merge-diff/v4 v4.4.1
sigs.k8s.io/yaml v1.4.0
Expand Down Expand Up @@ -129,7 +130,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/square/go-jose.v2 v2.3.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
sigs.k8s.io/controller-tools v0.14.0 // indirect
Expand Down
79 changes: 46 additions & 33 deletions pkg/code-generator/codegen/templates/kube/generate_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,60 @@ set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
ROOT_PKG={{ .ProjectConfig.GoPackage }}
CLIENT_PKG=${ROOT_PKG}/kube/client
APIS_PKG=${ROOT_PKG}/kube/apis
# Heavily inspired by <https://github.com/kubernetes-sigs/gateway-api/blob/main/hack/update-codegen.sh>.

# Grab code-generator version from go.sum.
CODEGEN_PKG=$(go list -f {{` + `"'{{ .Dir }}'"` + `}} -m k8s.io/code-generator)
GENGO_PKG=$(go list -f {{` + `"'{{ .Dir }}'"` + `}} -m k8s.io/gengo/v2)
readonly SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE}")"/.. && pwd)"
readonly OUTPUT_DIR="${SCRIPT_ROOT}/client"

echo ">> Using ${CODEGEN_PKG}"
readonly GOPATH="$(mktemp -d)"
mkdir -p $GOPATH/src/github.com/solo-io/gloo
ln -s $SCRIPT_ROOT $GOPATH/src/github.com/solo-io/gloo

# code-generator does work with go.mod but makes assumptions about
# the project living in $GOPATH/src. To work around this and support
# any location; create a temporary directory, use this as an output
# base, and copy everything back once generated.
TEMP_DIR=$(mktemp -d)
cleanup() {
echo ">> Removing ${TEMP_DIR}"
rm -rf ${TEMP_DIR}
}
trap "cleanup" EXIT SIGINT
readonly CODEGEN_PKG=$(go list -f {{` + `"'{{ .Dir }}'"` + `}} -m k8s.io/code-generator)
readonly GENGO_PKG=$(go list -f {{` + `"'{{ .Dir }}'"` + `}} -m k8s.io/gengo/v2)

echo ">> Temporary output directory ${TEMP_DIR}"
readonly ROOT_PKG={{ .ProjectConfig.GoPackage }}
readonly OUTPUT_PKG=${ROOT_PKG}/kube/client
readonly APIS_PKG=${ROOT_PKG}/kube/apis

mkdir -p "${TEMP_DIR}/${ROOT_PKG}/pkg/client/informers" \
"${TEMP_DIR}/${ROOT_PKG}/pkg/client/listers" \
"${TEMP_DIR}/${ROOT_PKG}/pkg/client/clientset"
readonly CLIENTSET_NAME=versioned
readonly CLIENTSET_PKG_NAME=clientset
readonly VERSIONS=( v1 )

# Ensure we can execute.
chmod +x ${CODEGEN_PKG}/kube_codegen.sh
PROJECT_INPUT_DIRS_SPACE=""
PROJECT_INPUT_DIRS_COMMA=""
for VERSION in "${VERSIONS[@]}"
do
PROJECT_INPUT_DIRS_SPACE+="${APIS_PKG}/{{ .ProjectConfig.Name }}/{{ .ProjectConfig.Version }} "
PROJECT_INPUT_DIRS_COMMA+="${APIS_PKG}/{{ .ProjectConfig.Name }}/{{ .ProjectConfig.Version }},"
done
PROJECT_INPUT_DIRS_SPACE="${PROJECT_INPUT_DIRS_SPACE%,}" # drop trailing space
PROJECT_INPUT_DIRS_COMMA="${PROJECT_INPUT_DIRS_COMMA%,}" # drop trailing comma

source ${CODEGEN_PKG}/kube_codegen.sh kube::codegen::gen_client \
--output-dir "${TEMP_DIR}" \
--output-pkg "${CLIENT_PKG}" \
--with-watch \
--boilerplate "${GENGO_PKG}/boilerplate/boilerplate.go.txt" \
${APIS_PKG}
readonly COMMON_FLAGS="--go-header-file ${GENGO_PKG}/boilerplate/boilerplate.go.txt"

ls -lha $TEMP_DIR
echo "Generating clientset at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}"
go run $CODEGEN_PKG/cmd/client-gen \
--clientset-name "${CLIENTSET_NAME}" \
--input-base "${APIS_PKG}" \
--input "${PROJECT_INPUT_DIRS_COMMA//${APIS_PKG}/}" \
--output-dir "$OUTPUT_DIR/${CLIENTSET_PKG_NAME}" \
--output-pkg "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}" \
${COMMON_FLAGS}

# Copy everything back.
cp -r "${TEMP_DIR}/${ROOT_PKG}/." "${SCRIPT_ROOT}/"
echo "Generating listers at ${OUTPUT_PKG}/listers"
go run $CODEGEN_PKG/cmd/lister-gen \
--output-dir "$OUTPUT_DIR/listers" \
--output-pkg "${OUTPUT_PKG}/listers" \
${COMMON_FLAGS} \
${PROJECT_INPUT_DIRS_COMMA}

echo "Generating informers at ${OUTPUT_PKG}/informers"
go run $CODEGEN_PKG/cmd/informer-gen \
--versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}/${CLIENTSET_NAME}" \
--listers-package "${OUTPUT_PKG}/listers" \
--output-dir "$OUTPUT_DIR/informers" \
--output-pkg "${OUTPUT_PKG}/informers" \
${COMMON_FLAGS} \
${PROJECT_INPUT_DIRS_COMMA}
`))

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading