Skip to content

Commit 41b44a7

Browse files
authored
[release-1.15] Upgrade the hack lib to fix the patch release failure (#1966)
1 parent 049379e commit 41b44a7

11 files changed

+436
-136
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ require (
2121
k8s.io/client-go v0.29.2
2222
k8s.io/code-generator v0.29.2
2323
knative.dev/caching v0.0.0-20240716132144-989f54c83776
24-
knative.dev/eventing v0.42.3
25-
knative.dev/hack v0.0.0-20240814135050-f9643117f6a1
24+
knative.dev/eventing v0.42.5
25+
knative.dev/hack v0.0.0-20250117112405-6cb0feb3ac46
2626
knative.dev/pkg v0.0.0-20240716082220-4355f0c73608
2727
knative.dev/serving v0.42.2
2828
sigs.k8s.io/yaml v1.4.0

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -1361,10 +1361,10 @@ k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCf
13611361
k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
13621362
knative.dev/caching v0.0.0-20240716132144-989f54c83776 h1:2nINnWuXtb9e2nG/EJxSCeghcmu6qmvmomJ7woiP5Is=
13631363
knative.dev/caching v0.0.0-20240716132144-989f54c83776/go.mod h1:Uj74eO9rLiK1eb8wmDBED1hJBZQ7MJ9cvq/d8Ktsm3c=
1364-
knative.dev/eventing v0.42.3 h1:ubG0XApByn8xnT40XFIUlhVfbREWWG4SAnq9CiVtGLc=
1365-
knative.dev/eventing v0.42.3/go.mod h1:hW5BMYcihtCelT9pqaMtK8gmNOo1ybxcigjBY+/fU+k=
1366-
knative.dev/hack v0.0.0-20240814135050-f9643117f6a1 h1:3l9x2gly9549Mn+HFWlyf+r/mBYXkcEpBQGaWz6rRJM=
1367-
knative.dev/hack v0.0.0-20240814135050-f9643117f6a1/go.mod h1:yk2OjGDsbEnQjfxdm0/HJKS2WqTLEFg/N6nUs6Rqx3Q=
1364+
knative.dev/eventing v0.42.5 h1:pHqRxNLV6f4d0M5ZCG598xOivI0mAJk6+5DMVP1NEwE=
1365+
knative.dev/eventing v0.42.5/go.mod h1:hW5BMYcihtCelT9pqaMtK8gmNOo1ybxcigjBY+/fU+k=
1366+
knative.dev/hack v0.0.0-20250117112405-6cb0feb3ac46 h1:UWX4qXgehoigmCH/db5ZldJ9V2pLVKA88Qes7Q0+XSI=
1367+
knative.dev/hack v0.0.0-20250117112405-6cb0feb3ac46/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY=
13681368
knative.dev/networking v0.0.0-20240716111826-bab7f2a3e556 h1:9OTyJkrjiFh/burZiti3WucGv8Qtt91VJTnXfO5dC2g=
13691369
knative.dev/networking v0.0.0-20240716111826-bab7f2a3e556/go.mod h1:1PosUDkXqoHNzYxtLIwa7LFqSsIXBShHOseAb6XBeEU=
13701370
knative.dev/pkg v0.0.0-20240716082220-4355f0c73608 h1:BOiRzcnRS9Z5ruxlCiS/K1/Hb5bUN0X4W3xCegdcYQE=
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Copyright #{YEAR} The Knative 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+
*/

vendor/knative.dev/hack/codegen-library.sh

+149-12
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,158 @@
1616

1717
# Setup the env for doing Knative style codegen.
1818

19-
kn_hack_library=${kn_hack_library:-"$(dirname $0)/../vendor/knative.dev/hack/library.sh"}
19+
# Store Bash options
20+
oldstate="$(set +o)"
21+
22+
set -Eeuo pipefail
23+
24+
export repodir kn_hack_dir kn_hack_library \
25+
MODULE_NAME CODEGEN_TMP_GOPATH CODEGEN_ORIGINAL_GOPATH GOPATH GOBIN \
26+
CODEGEN_PKG KNATIVE_CODEGEN_PKG
27+
28+
kn_hack_dir="$(realpath "$(dirname "${BASH_SOURCE[0]:-$0}")")"
29+
kn_hack_library=${kn_hack_library:-"${kn_hack_dir}/library.sh"}
2030

2131
if [[ -f "$kn_hack_library" ]]; then
22-
source $kn_hack_library
32+
# shellcheck disable=SC1090
33+
source "$kn_hack_library"
2334
else
24-
echo "this file is intended to be imported from a golang project that vendors knative.dev/hack"
25-
exit
35+
echo "The \$kn_hack_library points to a non-existent file: $kn_hack_library" >&2
36+
exit 42
2637
fi
2738

28-
export MODULE_NAME=$(go_mod_module_name)
29-
export GOPATH=$(go_mod_gopath_hack)
30-
export GOBIN=${GOPATH}/bin # Set GOBIN explicitly as deepcopy-gen is installed by go install.
31-
export CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${REPO_ROOT_DIR}; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}
32-
export KNATIVE_CODEGEN_PKG=${KNATIVE_CODEGEN_PKG:-$(cd ${REPO_ROOT_DIR}; ls -d -1 ./vendor/knative.dev/pkg 2>/dev/null || echo "${REPO_ROOT_DIR}")}
39+
repodir="$(go_run knative.dev/toolbox/modscope@latest current --path)"
40+
41+
function go-resolve-pkg-dir() {
42+
local pkg="${1:?Pass the package name}"
43+
local pkgdir
44+
if [ -d "${repodir}/vendor" ]; then
45+
pkgdir="${repodir}/vendor/${pkg}"
46+
if [ -d "${pkgdir}" ]; then
47+
echo "${pkgdir}"
48+
return 0
49+
else
50+
return 1
51+
fi
52+
else
53+
go mod download -x > /dev/stderr
54+
go list -m -f '{{.Dir}}' "${pkg}" 2>/dev/null
55+
return $?
56+
fi
57+
}
58+
59+
# Change dir to the original executing script's directory, not the current source!
60+
pushd "$(dirname "$(realpath "$0")")" > /dev/null
61+
62+
if ! CODEGEN_PKG="${CODEGEN_PKG:-"$(go-resolve-pkg-dir k8s.io/code-generator)"}"; then
63+
warning "Failed to determine the k8s.io/code-generator package"
64+
fi
65+
if ! KNATIVE_CODEGEN_PKG="${KNATIVE_CODEGEN_PKG:-"$(go-resolve-pkg-dir knative.dev/pkg)"}"; then
66+
warning "Failed to determine the knative.dev/pkg package"
67+
fi
68+
69+
popd > /dev/null
70+
71+
CODEGEN_ORIGINAL_GOPATH="$(go env GOPATH)"
72+
CODEGEN_TMP_GOPATH=$(go_mod_gopath_hack)
73+
GOPATH="${CODEGEN_TMP_GOPATH}"
74+
GOBIN="${GOPATH}/bin" # Set GOBIN explicitly as k8s-gen' are installed by go install.
75+
76+
if [[ -n "${CODEGEN_PKG}" ]] && ! [ -x "${CODEGEN_PKG}/generate-groups.sh" ]; then
77+
chmod +x "${CODEGEN_PKG}/generate-groups.sh"
78+
chmod +x "${CODEGEN_PKG}/generate-internal-groups.sh"
79+
fi
80+
if [[ -n "${KNATIVE_CODEGEN_PKG}" ]] && ! [ -x "${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" ]; then
81+
chmod +x "${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh"
82+
fi
83+
84+
# Generate boilerplate file with the current year
85+
function boilerplate() {
86+
local go_header_file="${kn_hack_dir}/boilerplate.go.txt"
87+
local current_boilerplate_file="${TMPDIR}/boilerplate.go.txt"
88+
# Replace #{YEAR} with the current year
89+
sed "s/#{YEAR}/$(date +%Y)/" \
90+
< "${go_header_file}" \
91+
> "${current_boilerplate_file}"
92+
echo "${current_boilerplate_file}"
93+
}
94+
95+
# Generate K8s' groups codegen
96+
function generate-groups() {
97+
if [[ -z "${CODEGEN_PKG}" ]]; then
98+
abort "CODEGEN_PKG is not set"
99+
fi
100+
"${CODEGEN_PKG}"/generate-groups.sh \
101+
"$@" \
102+
--go-header-file "$(boilerplate)"
103+
}
104+
105+
# Generate K8s' internal groups codegen
106+
function generate-internal-groups() {
107+
if [[ -z "${CODEGEN_PKG}" ]]; then
108+
abort "CODEGEN_PKG is not set"
109+
fi
110+
"${CODEGEN_PKG}"/generate-internal-groups.sh \
111+
"$@" \
112+
--go-header-file "$(boilerplate)"
113+
}
114+
115+
# Generate Knative style codegen
116+
function generate-knative() {
117+
if [[ -z "${KNATIVE_CODEGEN_PKG}" ]]; then
118+
abort "KNATIVE_CODEGEN_PKG is not set"
119+
fi
120+
"${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" \
121+
"$@" \
122+
--go-header-file "$(boilerplate)"
123+
}
124+
125+
# Cleanup after generating code
126+
function cleanup-codegen() {
127+
restore-changes-if-its-copyright-year-only
128+
restore-gopath
129+
}
130+
131+
# Restore changes if the file contains only the change in the copyright year
132+
function restore-changes-if-its-copyright-year-only() {
133+
local difflist
134+
log "Cleaning up generated code"
135+
difflist="$(mktemp)"
136+
if ! git diff --exit-code --name-only > /dev/null; then
137+
# list git changes and skip those which differ only in the boilerplate year
138+
git diff --name-only > "$difflist"
139+
while read -r file; do
140+
# check if the file contains just the change in the boilerplate year
141+
if [ "$(LANG=C git diff --exit-code --shortstat -- "$file")" = ' 1 file changed, 1 insertion(+), 1 deletion(-)' ] && \
142+
[[ "$(git diff --exit-code -U1 -- "$file" | grep -Ec '^[+-]\s*[*#]?\s*Copyright 2[0-9]{3}')" -eq 2 ]]; then
143+
# restore changes to that file
144+
git checkout -- "$file"
145+
fi
146+
done < "$difflist"
147+
fi
148+
rm -f "$difflist"
149+
}
150+
151+
# Restore the GOPATH and clean up the temporary directory
152+
function restore-gopath() {
153+
# Skip this if the directory is already checked out onto the GOPATH.
154+
if __is_checkout_onto_gopath; then
155+
return
156+
fi
157+
if [ -d "$CODEGEN_TMP_GOPATH" ]; then
158+
chmod -R u+w "${CODEGEN_TMP_GOPATH}"
159+
rm -rf "${CODEGEN_TMP_GOPATH}"
160+
unset CODEGEN_TMP_GOPATH
161+
fi
162+
unset GOPATH GOBIN
163+
# Restore the original GOPATH, if it was different from the default one.
164+
if [ "$CODEGEN_ORIGINAL_GOPATH" != "$(go env GOPATH)" ]; then
165+
export GOPATH="$CODEGEN_ORIGINAL_GOPATH"
166+
fi
167+
unset CODEGEN_ORIGINAL_GOPATH
168+
}
169+
170+
add_trap cleanup-codegen EXIT
33171

34-
[ -x ${CODEGEN_PKG}/generate-groups.sh ] || chmod +x ${CODEGEN_PKG}/generate-groups.sh
35-
[ -x ${CODEGEN_PKG}/generate-internal-groups.sh ] || chmod +x ${CODEGEN_PKG}/generate-internal-groups.sh
36-
[ -x ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh ] || chmod +x ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh
172+
# Restore Bash options
173+
eval "$oldstate"

vendor/knative.dev/hack/embed.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ import (
2020
"embed"
2121
)
2222

23-
//go:embed *.sh
23+
//go:embed *.sh *.go.txt
2424
var Scripts embed.FS

vendor/knative.dev/hack/go.work

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
go 1.18
1+
go 1.21
22

33
use (
44
.

0 commit comments

Comments
 (0)