Skip to content

Commit 07ab845

Browse files
committed
chore: add a script to auto-bump go runtime version
1 parent 313aace commit 07ab845

2 files changed

Lines changed: 209 additions & 0 deletions

File tree

hack/update-go-version.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2019 The Kubernetes Authors.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
set -euo pipefail
6+
7+
patch="${1:?usage: $0 <go patch version: 1.N.M>}"
8+
9+
if [[ ! "${patch}" =~ ^1\.[0-9]+\.[0-9]+$ ]]; then
10+
echo "patch must look like 1.N.M, e.g. 1.25.10" >&2
11+
exit 2
12+
fi
13+
14+
minor="${patch%.*}"
15+
go_directive="${minor}.0"
16+
toolchain="go${patch}"
17+
18+
echo "go directive : ${go_directive}"
19+
echo "toolchain / Docker : ${patch}"
20+
21+
replace_golang_image_versions() {
22+
local file="$1"
23+
local tmp
24+
25+
tmp="$(mktemp "${file}.XXXXXX")"
26+
cp -p "${file}" "${tmp}"
27+
if ! awk -v patch="${patch}" '{
28+
gsub(/golang:1\.[0-9]+\.[0-9]+/, "golang:" patch)
29+
print
30+
}' "${file}" >"${tmp}"; then
31+
rm -f "${tmp}"
32+
return 1
33+
fi
34+
mv "${tmp}" "${file}"
35+
}
36+
37+
# 1. Keep all go.mod files and go.mod templates at the downstream-visible minimum version.
38+
find . \
39+
\( -name go.mod -o -name go.mod.src \) \
40+
-not -path './.git/*' \
41+
-not -path './vendor/*' \
42+
-print0 |
43+
while IFS= read -r -d '' modfile; do
44+
go mod edit -go="${go_directive}" "${modfile}"
45+
go mod edit -fmt "${modfile}"
46+
done
47+
48+
# 2. Keep go.work at the minimum version, plus the actual toolchain version.
49+
if [[ -f go.work ]]; then
50+
go work edit -go="${go_directive}" -toolchain="${toolchain}" go.work
51+
go work edit -fmt go.work
52+
fi
53+
54+
# 3. Update golang:1.x.y references in Dockerfiles, docs, workflows, and generated source.
55+
git --no-pager grep -zlE 'golang:1\.[0-9]+\.[0-9]+' -- \
56+
'*Dockerfile' \
57+
'*.Dockerfile' \
58+
'*.go' \
59+
'*.md' \
60+
'*.yaml' \
61+
'*.yml' |
62+
while IFS= read -r -d '' file; do
63+
replace_golang_image_versions "${file}"
64+
done
65+
66+
echo
67+
echo "Go version references after update:"
68+
git --no-pager grep -nE \
69+
'(^go 1\.[0-9]+(\.[0-9]+)?$|toolchain go1\.[0-9]+\.[0-9]+|golang:1\.[0-9]+\.[0-9]+|go-version:)' \
70+
-- ':!:vendor/**' ':!:hack/update-go-version.sh' || true
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2026 The Kubernetes Authors.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
set -euo pipefail
6+
7+
usage() {
8+
cat <<EOF
9+
usage: $0 <go patch version: 1.N.M> [options]
10+
11+
Options:
12+
--checkout-dir DIR test-infra checkout path (default: ../test-infra)
13+
--branch BRANCH branch name to create (default: kustomize-update-golang-1-N-M)
14+
EOF
15+
}
16+
17+
patch=""
18+
checkout_dir="${TEST_INFRA_CHECKOUT_DIR:-../test-infra}"
19+
branch=""
20+
21+
while [[ $# -gt 0 ]]; do
22+
case "$1" in
23+
--checkout-dir)
24+
checkout_dir="${2:?--checkout-dir requires a value}"
25+
shift 2
26+
;;
27+
--branch)
28+
branch="${2:?--branch requires a value}"
29+
shift 2
30+
;;
31+
-h|--help)
32+
usage
33+
exit 0
34+
;;
35+
-*)
36+
echo "unknown option: $1" >&2
37+
usage >&2
38+
exit 2
39+
;;
40+
*)
41+
if [[ -n "${patch}" ]]; then
42+
echo "unexpected argument: $1" >&2
43+
usage >&2
44+
exit 2
45+
fi
46+
patch="$1"
47+
shift
48+
;;
49+
esac
50+
done
51+
52+
if [[ -z "${patch}" ]]; then
53+
usage >&2
54+
exit 2
55+
fi
56+
57+
if [[ ! "${patch}" =~ ^1\.[0-9]+\.[0-9]+$ ]]; then
58+
echo "patch must look like 1.N.M, e.g. 1.25.10" >&2
59+
exit 2
60+
fi
61+
62+
for tool in gh git awk mktemp cp mv; do
63+
if ! command -v "${tool}" >/dev/null 2>&1; then
64+
echo "missing required command: ${tool}" >&2
65+
exit 1
66+
fi
67+
done
68+
69+
repo="kubernetes/test-infra"
70+
target_file="config/jobs/kubernetes-sigs/kustomize/kustomize-presubmit-master.yaml"
71+
image="public.ecr.aws/docker/library/golang:${patch}"
72+
branch="${branch:-kustomize-update-golang-${patch//./-}}"
73+
commit_message="Update go version for ${patch} in kustomize"
74+
pr_body="Update golang version in prow that is used in tests for https://github.com/kubernetes-sigs/kustomize"
75+
76+
replace_presubmit_image() {
77+
local file="$1"
78+
local tmp
79+
80+
tmp="$(mktemp "${file}.XXXXXX")"
81+
cp -p "${file}" "${tmp}"
82+
if ! awk -v image="${image}" '
83+
/image: public\.ecr\.aws\/docker\/library\/golang:1\.[0-9]+\.[0-9]+([-[:alnum:].]+)?$/ {
84+
sub(/image: public\.ecr\.aws\/docker\/library\/golang:1\.[0-9]+\.[0-9]+([-[:alnum:].]+)?$/, "image: " image)
85+
}
86+
{ print }
87+
' "${file}" >"${tmp}"; then
88+
rm -f "${tmp}"
89+
return 1
90+
fi
91+
mv "${tmp}" "${file}"
92+
}
93+
94+
if [[ ! -d "${checkout_dir}/.git" ]]; then
95+
mkdir -p "$(dirname "${checkout_dir}")"
96+
gh repo clone "${repo}" "${checkout_dir}" -- --depth=1 --branch master
97+
fi
98+
99+
cd "${checkout_dir}"
100+
checkout_dir="$(pwd -P)"
101+
102+
if [[ -n "$(git status --porcelain)" ]]; then
103+
echo "${checkout_dir} has uncommitted changes; please commit or stash them first." >&2
104+
exit 1
105+
fi
106+
107+
git fetch --depth=1 origin master
108+
git switch -C "${branch}" origin/master
109+
110+
if [[ ! -f "${target_file}" ]]; then
111+
echo "missing expected file: ${target_file}" >&2
112+
exit 1
113+
fi
114+
115+
replace_presubmit_image "${target_file}"
116+
117+
if git diff --quiet -- "${target_file}"; then
118+
echo "${target_file} already uses ${image}"
119+
exit 0
120+
fi
121+
122+
git diff --check -- "${target_file}"
123+
git diff -- "${target_file}"
124+
125+
github_user="$(gh api user --jq .login 2>/dev/null || echo '<your-github-user>')"
126+
fork_repo="${github_user}/test-infra"
127+
fork_remote="kustomize-go-bump"
128+
129+
echo
130+
echo "Updated ${target_file} to ${image}"
131+
echo
132+
echo "Next steps:"
133+
echo " cd ${checkout_dir}"
134+
echo " gh repo fork ${repo} --clone=false --remote=false"
135+
echo " git remote remove ${fork_remote} >/dev/null 2>&1 || true"
136+
echo " git remote add ${fork_remote} https://github.com/${fork_repo}.git"
137+
echo " git commit -am '${commit_message}'"
138+
echo " git push -u ${fork_remote} ${branch}"
139+
echo " gh pr create --repo ${repo} --base master --head ${github_user}:${branch} --title '${commit_message}' --body '${pr_body}'"

0 commit comments

Comments
 (0)