Skip to content

Commit a72c0e9

Browse files
authored
refactor codegen scripts to make it easier to generate two clients (#3589)
* refactor codegen scripts to make it easier to generate two clients * add some newlines * fix find invocation use -maxdepth and not -depth * include requested comment
1 parent 64c8500 commit a72c0e9

File tree

9 files changed

+244
-217
lines changed

9 files changed

+244
-217
lines changed

cmd/modelschema/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"os"
2525
"strings"
2626

27-
openapi "sigs.k8s.io/gateway-api/pkg/generated/openapi"
27+
stable "sigs.k8s.io/gateway-api/apis/openapi"
2828

2929
"k8s.io/kube-openapi/pkg/common"
3030
"k8s.io/kube-openapi/pkg/validation/spec"
@@ -43,7 +43,7 @@ func output() error {
4343
refFunc := func(name string) spec.Ref {
4444
return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s", friendlyName(name)))
4545
}
46-
defs := openapi.GetOpenAPIDefinitions(refFunc)
46+
defs := stable.GetOpenAPIDefinitions(refFunc)
4747
schemaDefs := make(map[string]spec.Schema, len(defs))
4848
for k, v := range defs {
4949
// Replace top-level schema with v2 if a v2 schema is embedded

hack/update-clientset.sh

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# https://github.com/kubernetes/code-generator provides generator code to generate a custom typed
18+
# and versioned client for custom API types similar to what https://github.com/kubernetes/client-go
19+
# provides for core types. This script generates such a client for the Gateway API types, in service of any
20+
# projects that need them.
21+
22+
set -o errexit
23+
set -o nounset
24+
set -o pipefail
25+
26+
27+
if [[ "${1:-stable}" == "experimental" ]]; then
28+
readonly API_PATH="apisx"
29+
else
30+
readonly API_PATH="apis"
31+
fi
32+
33+
readonly SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE}")"/.. && pwd)"
34+
35+
if [[ "${VERIFY_CODEGEN:-}" == "true" ]]; then
36+
echo "Running in verification mode"
37+
readonly VERIFY_FLAG="--verify-only"
38+
fi
39+
40+
readonly COMMON_FLAGS="${VERIFY_FLAG:-} --go-header-file ${SCRIPT_ROOT}/hack/boilerplate/boilerplate.generatego.txt"
41+
42+
readonly APIS_PKG=sigs.k8s.io/gateway-api
43+
readonly CLIENTSET_NAME=versioned
44+
readonly CLIENTSET_PKG_NAME=clientset
45+
readonly VERSIONS=($(find ./${API_PATH} -maxdepth 1 -name "v*" -exec bash -c 'basename {}' \; | xargs))
46+
47+
if [[ "${1:-stable}" == "experimental" ]]; then
48+
readonly OUTPUT_DIR=pkg/clientx
49+
readonly OUTPUT_PKG=sigs.k8s.io/gateway-api/pkg/clientx
50+
else
51+
readonly OUTPUT_DIR=pkg/client
52+
readonly OUTPUT_PKG=sigs.k8s.io/gateway-api/pkg/client
53+
fi
54+
55+
GATEWAY_INPUT_DIRS_SPACE=""
56+
GATEWAY_INPUT_DIRS_COMMA=""
57+
for VERSION in "${VERSIONS[@]}"
58+
do
59+
GATEWAY_INPUT_DIRS_SPACE+="${APIS_PKG}/${API_PATH}/${VERSION} "
60+
GATEWAY_INPUT_DIRS_COMMA+="${APIS_PKG}/${API_PATH}/${VERSION},"
61+
done
62+
GATEWAY_INPUT_DIRS_SPACE="${GATEWAY_INPUT_DIRS_SPACE%,}" # drop trailing space
63+
GATEWAY_INPUT_DIRS_COMMA="${GATEWAY_INPUT_DIRS_COMMA%,}" # drop trailing comma
64+
65+
# throw away
66+
new_report="$(mktemp -t "$(basename "$0").api_violations.XXXXXX")"
67+
68+
echo "Generating openapi schema"
69+
go run k8s.io/kube-openapi/cmd/openapi-gen \
70+
--output-file zz_generated.openapi.go \
71+
--report-filename "${new_report}" \
72+
--output-dir "${API_PATH}/openapi" \
73+
--output-pkg "sigs.k8s.io/gateway-api/${API_PATH}/openapi" \
74+
${COMMON_FLAGS} \
75+
$GATEWAY_INPUT_DIRS_SPACE \
76+
k8s.io/apimachinery/pkg/apis/meta/v1 \
77+
k8s.io/apimachinery/pkg/runtime \
78+
k8s.io/apimachinery/pkg/version
79+
80+
81+
echo "Generating apply configuration"
82+
go run k8s.io/code-generator/cmd/applyconfiguration-gen \
83+
--openapi-schema <(go run ${SCRIPT_ROOT}/cmd/modelschema) \
84+
--output-dir "${API_PATH}/applyconfiguration" \
85+
--output-pkg "${APIS_PKG}/${API_PATH}/applyconfiguration" \
86+
${COMMON_FLAGS} \
87+
${GATEWAY_INPUT_DIRS_SPACE}
88+
89+
echo "Generating clientset at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}"
90+
go run k8s.io/code-generator/cmd/client-gen \
91+
--clientset-name "${CLIENTSET_NAME}" \
92+
--input-base "${APIS_PKG}" \
93+
--input "${GATEWAY_INPUT_DIRS_COMMA//${APIS_PKG}/}" \
94+
--output-dir "${OUTPUT_DIR}/${CLIENTSET_PKG_NAME}" \
95+
--output-pkg "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}" \
96+
--apply-configuration-package "${APIS_PKG}/${API_PATH}/applyconfiguration" \
97+
${COMMON_FLAGS}
98+
99+
echo "Generating listers at ${OUTPUT_PKG}/listers"
100+
go run k8s.io/code-generator/cmd/lister-gen \
101+
--output-dir "${OUTPUT_DIR}/listers" \
102+
--output-pkg "${OUTPUT_PKG}/listers" \
103+
${COMMON_FLAGS} \
104+
${GATEWAY_INPUT_DIRS_SPACE}
105+
106+
echo "Generating informers at ${OUTPUT_PKG}/informers"
107+
go run k8s.io/code-generator/cmd/informer-gen \
108+
--versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}/${CLIENTSET_NAME}" \
109+
--listers-package "${OUTPUT_PKG}/listers" \
110+
--output-dir "${OUTPUT_DIR}/informers" \
111+
--output-pkg "${OUTPUT_PKG}/informers" \
112+
${COMMON_FLAGS} \
113+
${GATEWAY_INPUT_DIRS_SPACE}
114+
115+
echo "Generating ${VERSION} register at ${APIS_PKG}/${API_PATH}/${VERSION}"
116+
go run k8s.io/code-generator/cmd/register-gen \
117+
--output-file zz_generated.register.go \
118+
${COMMON_FLAGS} \
119+
${GATEWAY_INPUT_DIRS_SPACE}
120+
121+
for VERSION in "${VERSIONS[@]}"
122+
do
123+
echo "Generating ${VERSION} deepcopy at ${APIS_PKG}/${API_PATH}/${VERSION}"
124+
go run sigs.k8s.io/controller-tools/cmd/controller-gen \
125+
object:headerFile=${SCRIPT_ROOT}/hack/boilerplate/boilerplate.generatego.txt \
126+
paths="${APIS_PKG}/${API_PATH}/${VERSION}"
127+
done

hack/update-codegen.sh

+2-196
Original file line numberDiff line numberDiff line change
@@ -46,202 +46,8 @@ export GOMODCACHE GO111MODULE GOFLAGS GOPATH
4646
mkdir -p "$GOPATH/src/sigs.k8s.io"
4747
ln -s "${SCRIPT_ROOT}" "$GOPATH/src/sigs.k8s.io/gateway-api"
4848

49-
readonly OUTPUT_PKG=sigs.k8s.io/gateway-api/pkg/client
50-
readonly APIS_PKG=sigs.k8s.io/gateway-api
51-
readonly CLIENTSET_NAME=versioned
52-
readonly CLIENTSET_PKG_NAME=clientset
53-
readonly VERSIONS=(v1alpha2 v1alpha3 v1beta1 v1)
54-
55-
GATEWAY_INPUT_DIRS_SPACE=""
56-
GATEWAY_INPUT_DIRS_COMMA=""
57-
for VERSION in "${VERSIONS[@]}"
58-
do
59-
GATEWAY_INPUT_DIRS_SPACE+="${APIS_PKG}/apis/${VERSION} "
60-
GATEWAY_INPUT_DIRS_COMMA+="${APIS_PKG}/apis/${VERSION},"
61-
done
62-
GATEWAY_INPUT_DIRS_SPACE="${GATEWAY_INPUT_DIRS_SPACE%,}" # drop trailing space
63-
GATEWAY_INPUT_DIRS_COMMA="${GATEWAY_INPUT_DIRS_COMMA%,}" # drop trailing comma
64-
65-
66-
if [[ "${VERIFY_CODEGEN:-}" == "true" ]]; then
67-
echo "Running in verification mode"
68-
readonly VERIFY_FLAG="--verify-only"
69-
fi
70-
71-
readonly COMMON_FLAGS="${VERIFY_FLAG:-} --go-header-file ${SCRIPT_ROOT}/hack/boilerplate/boilerplate.generatego.txt"
72-
7349
echo "Generating CRDs"
7450
go run ./pkg/generator
7551

76-
# throw away
77-
new_report="$(mktemp -t "$(basename "$0").api_violations.XXXXXX")"
78-
79-
echo "Generating openapi schema"
80-
go run k8s.io/kube-openapi/cmd/openapi-gen \
81-
--output-file zz_generated.openapi.go \
82-
--report-filename "${new_report}" \
83-
--output-dir "pkg/generated/openapi" \
84-
--output-pkg "sigs.k8s.io/gateway-api/pkg/generated/openapi" \
85-
${COMMON_FLAGS} \
86-
$GATEWAY_INPUT_DIRS_SPACE \
87-
k8s.io/apimachinery/pkg/apis/meta/v1 \
88-
k8s.io/apimachinery/pkg/runtime \
89-
k8s.io/apimachinery/pkg/version
90-
91-
92-
echo "Generating apply configuration"
93-
go run k8s.io/code-generator/cmd/applyconfiguration-gen \
94-
--openapi-schema <(go run ${SCRIPT_ROOT}/cmd/modelschema) \
95-
--output-dir "apis/applyconfiguration" \
96-
--output-pkg "${APIS_PKG}/apis/applyconfiguration" \
97-
${COMMON_FLAGS} \
98-
${GATEWAY_INPUT_DIRS_SPACE}
99-
100-
101-
# Temporary hack until https://github.com/kubernetes/kubernetes/pull/124371 is released
102-
function fix_applyconfiguration() {
103-
local package="$1"
104-
local version="$(basename $1)"
105-
106-
echo $package
107-
echo $version
108-
pushd $package > /dev/null
109-
110-
# Replace import
111-
for filename in *.go; do
112-
import_line=$(grep "$package" "$filename")
113-
if [[ -z "$import_line" ]]; then
114-
continue
115-
fi
116-
import_prefix=$(echo "$import_line" | awk '{print $1}')
117-
sed -i'.bak' -e "s,${import_prefix} \"sigs.k8s.io/gateway-api/${package}\",,g" "$filename"
118-
sed -i'.bak' -e "s,\[\]${import_prefix}\.,\[\],g" "$filename"
119-
sed -i'.bak' -e "s,&${import_prefix}\.,&,g" "$filename"
120-
sed -i'.bak' -e "s,*${import_prefix}\.,*,g" "$filename"
121-
sed -i'.bak' -e "s,^\t${import_prefix}\.,,g" "$filename"
122-
done
123-
124-
rm *.bak
125-
go fmt .
126-
find . -type f -name "*.go" -exec sed -i'.bak' -e "s,import (),,g" {} \;
127-
rm *.bak
128-
go fmt .
129-
130-
popd > /dev/null
131-
}
132-
133-
export -f fix_applyconfiguration
134-
find apis/applyconfiguration/apis -name "v*" -type d -exec bash -c 'fix_applyconfiguration $0' {} \;
135-
136-
echo "Generating clientset at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}"
137-
go run k8s.io/code-generator/cmd/client-gen \
138-
--clientset-name "${CLIENTSET_NAME}" \
139-
--input-base "${APIS_PKG}" \
140-
--input "${GATEWAY_INPUT_DIRS_COMMA//${APIS_PKG}/}" \
141-
--output-dir "pkg/client/${CLIENTSET_PKG_NAME}" \
142-
--output-pkg "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}" \
143-
--apply-configuration-package "${APIS_PKG}/apis/applyconfiguration" \
144-
${COMMON_FLAGS}
145-
146-
echo "Generating listers at ${OUTPUT_PKG}/listers"
147-
go run k8s.io/code-generator/cmd/lister-gen \
148-
--output-dir "pkg/client/listers" \
149-
--output-pkg "${OUTPUT_PKG}/listers" \
150-
${COMMON_FLAGS} \
151-
${GATEWAY_INPUT_DIRS_SPACE}
152-
153-
echo "Generating informers at ${OUTPUT_PKG}/informers"
154-
go run k8s.io/code-generator/cmd/informer-gen \
155-
--versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}/${CLIENTSET_NAME}" \
156-
--listers-package "${OUTPUT_PKG}/listers" \
157-
--output-dir "pkg/client/informers" \
158-
--output-pkg "${OUTPUT_PKG}/informers" \
159-
${COMMON_FLAGS} \
160-
${GATEWAY_INPUT_DIRS_SPACE}
161-
162-
echo "Generating ${VERSION} register at ${APIS_PKG}/apis/${VERSION}"
163-
go run k8s.io/code-generator/cmd/register-gen \
164-
--output-file zz_generated.register.go \
165-
${COMMON_FLAGS} \
166-
${GATEWAY_INPUT_DIRS_SPACE}
167-
168-
for VERSION in "${VERSIONS[@]}"
169-
do
170-
echo "Generating ${VERSION} deepcopy at ${APIS_PKG}/apis/${VERSION}"
171-
go run sigs.k8s.io/controller-tools/cmd/controller-gen \
172-
object:headerFile=${SCRIPT_ROOT}/hack/boilerplate/boilerplate.generatego.txt \
173-
paths="${APIS_PKG}/apis/${VERSION}"
174-
done
175-
176-
echo "Generating gRPC/Protobuf code"
177-
178-
readonly PROTOC_CACHE_DIR="/tmp/protoc.cache"
179-
readonly PROTOC_BINARY="${PROTOC_CACHE_DIR}/bin/protoc"
180-
readonly PROTOC_VERSION="22.2"
181-
readonly PROTOC_REPO="https://github.com/protocolbuffers/protobuf"
182-
183-
readonly PROTOC_LINUX_X86_URL="${PROTOC_REPO}/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip"
184-
readonly PROTOC_LINUX_X86_CHECKSUM="4805ba56594556402a6c327a8d885a47640ee363 ${PROTOC_BINARY}"
185-
186-
readonly PROTOC_LINUX_ARM64_URL="${PROTOC_REPO}/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-aarch_64.zip"
187-
readonly PROTOC_LINUX_ARM64_CHECKSUM="47285b2386f990da319e9eef92cadec2dfa28733 ${PROTOC_BINARY}"
188-
189-
readonly PROTOC_MAC_UNIVERSAL_URL="${PROTOC_REPO}/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-osx-universal_binary.zip"
190-
readonly PROTOC_MAC_UNIVERSAL_CHECKSUM="2a79d0eb235c808eca8de893762072b94dc6144c ${PROTOC_BINARY}"
191-
192-
PROTOC_URL=""
193-
PROTOC_CHECKSUM=""
194-
195-
ARCH=$(uname -m)
196-
OS=$(uname)
197-
198-
if [[ "${OS}" != "Linux" ]] && [[ "${OS}" != "Darwin" ]]; then
199-
echo "Unsupported operating system ${OS}" >/dev/stderr
200-
exit 1
201-
fi
202-
203-
if [[ "${OS}" == "Linux" ]]; then
204-
if [[ "$ARCH" == "x86_64" ]]; then
205-
PROTOC_URL="$PROTOC_LINUX_X86_URL"
206-
PROTOC_CHECKSUM="$PROTOC_LINUX_X86_CHECKSUM"
207-
elif [[ "$ARCH" == "arm64" ]]; then
208-
PROTOC_URL="$PROTOC_LINUX_ARM64_URL"
209-
PROTOC_CHECKSUM="$PROTOC_LINUX_ARM64_CHECKSUM"
210-
elif [[ "$ARCH" == "aarch64" ]]; then
211-
PROTOC_URL="$PROTOC_LINUX_ARM64_URL"
212-
PROTOC_CHECKSUM="$PROTOC_LINUX_ARM64_CHECKSUM"
213-
else
214-
echo "Architecture ${ARCH} is not supported on OS ${OS}." >/dev/stderr
215-
exit 1
216-
fi
217-
elif [[ "${OS}" == "Darwin" ]]; then
218-
PROTOC_URL="$PROTOC_MAC_UNIVERSAL_URL"
219-
PROTOC_CHECKSUM="$PROTOC_MAC_UNIVERSAL_CHECKSUM"
220-
fi
221-
222-
function verify_protoc {
223-
if ! echo "${PROTOC_CHECKSUM}" | shasum -c >/dev/null; then
224-
echo "Downloaded protoc binary failed checksum." >/dev/stderr
225-
exit 1
226-
fi
227-
}
228-
229-
function ensure_protoc {
230-
mkdir -p "${PROTOC_CACHE_DIR}"
231-
if [ ! -f "${PROTOC_BINARY}" ]; then
232-
curl -sL -o "${PROTOC_CACHE_DIR}/protoc.zip" "${PROTOC_URL}"
233-
unzip -d "${PROTOC_CACHE_DIR}" "${PROTOC_CACHE_DIR}/protoc.zip"
234-
fi
235-
verify_protoc
236-
}
237-
238-
ensure_protoc
239-
go install google.golang.org/protobuf/cmd/[email protected]
240-
go install google.golang.org/grpc/cmd/[email protected]
241-
242-
(cd conformance/echo-basic && \
243-
export PATH="$PATH:$GOPATH/bin" && \
244-
"${PROTOC_BINARY}" --go_out=grpcechoserver --go_opt=paths=source_relative \
245-
--go-grpc_out=grpcechoserver --go-grpc_opt=paths=source_relative \
246-
grpcecho.proto
247-
)
52+
./hack/update-clientset.sh
53+
./hack/update-protos.sh

0 commit comments

Comments
 (0)