|
| 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, relative to the kustomize root (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 | +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" |
| 70 | +repo_root="$(cd "${script_dir}/.." && pwd -P)" |
| 71 | +cd "${repo_root}" |
| 72 | + |
| 73 | +repo="kubernetes/test-infra" |
| 74 | +target_file="config/jobs/kubernetes-sigs/kustomize/kustomize-presubmit-master.yaml" |
| 75 | +image="public.ecr.aws/docker/library/golang:${patch}" |
| 76 | +branch="${branch:-kustomize-update-golang-${patch//./-}}" |
| 77 | +commit_message="Update go version for ${patch} in kustomize" |
| 78 | +pr_body="Update golang version in prow that is used in tests for https://github.com/kubernetes-sigs/kustomize" |
| 79 | + |
| 80 | +replace_presubmit_image() { |
| 81 | + local file="$1" |
| 82 | + local tmp |
| 83 | + |
| 84 | + tmp="$(mktemp "${file}.XXXXXX")" |
| 85 | + cp -p "${file}" "${tmp}" |
| 86 | + if ! awk -v image="${image}" ' |
| 87 | + /image: public\.ecr\.aws\/docker\/library\/golang:1\.[0-9]+\.[0-9]+([-[:alnum:].]+)?$/ { |
| 88 | + sub(/image: public\.ecr\.aws\/docker\/library\/golang:1\.[0-9]+\.[0-9]+([-[:alnum:].]+)?$/, "image: " image) |
| 89 | + } |
| 90 | + { print } |
| 91 | + ' "${file}" >"${tmp}"; then |
| 92 | + rm -f "${tmp}" |
| 93 | + return 1 |
| 94 | + fi |
| 95 | + mv "${tmp}" "${file}" |
| 96 | +} |
| 97 | + |
| 98 | +is_generated_image_change() { |
| 99 | + [[ "$(git branch --show-current)" == kustomize-update-golang-* ]] || return 1 |
| 100 | + [[ "$(git status --porcelain --untracked-files=all)" == " M ${target_file}" ]] || return 1 |
| 101 | + git diff --cached --quiet -- "${target_file}" || return 1 |
| 102 | + |
| 103 | + git diff --unified=0 -- "${target_file}" | awk ' |
| 104 | + /^diff --git / || /^index / || /^--- / || /^\+\+\+ / || /^@@ / { next } |
| 105 | + /^-/ { |
| 106 | + removed++ |
| 107 | + if ($0 !~ /^-[[:space:]]+- image: public\.ecr\.aws\/docker\/library\/golang:1\.[0-9]+\.[0-9]+([-[:alnum:].]+)?$/) { |
| 108 | + invalid = 1 |
| 109 | + } |
| 110 | + next |
| 111 | + } |
| 112 | + /^\+/ { |
| 113 | + added++ |
| 114 | + if ($0 !~ /^\+[[:space:]]+- image: public\.ecr\.aws\/docker\/library\/golang:1\.[0-9]+\.[0-9]+([-[:alnum:].]+)?$/) { |
| 115 | + invalid = 1 |
| 116 | + } |
| 117 | + next |
| 118 | + } |
| 119 | + NF { invalid = 1 } |
| 120 | + END { exit !(removed == 1 && added == 1 && !invalid) } |
| 121 | + ' |
| 122 | +} |
| 123 | + |
| 124 | +if [[ ! -d "${checkout_dir}/.git" ]]; then |
| 125 | + mkdir -p "$(dirname "${checkout_dir}")" |
| 126 | + gh repo clone "${repo}" "${checkout_dir}" -- --depth=1 --branch master |
| 127 | +fi |
| 128 | + |
| 129 | +cd "${checkout_dir}" |
| 130 | +checkout_dir="$(pwd -P)" |
| 131 | + |
| 132 | +git fetch --depth=1 origin master |
| 133 | + |
| 134 | +if [[ -n "$(git status --porcelain)" ]]; then |
| 135 | + if is_generated_image_change; then |
| 136 | + echo "Discarding the Go image change generated by the previous run." |
| 137 | + git restore --source=origin/master --worktree -- "${target_file}" |
| 138 | + else |
| 139 | + echo "${checkout_dir} has unrelated uncommitted changes; please commit or stash them first." >&2 |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | +fi |
| 143 | + |
| 144 | +git switch -C "${branch}" origin/master |
| 145 | + |
| 146 | +if [[ ! -f "${target_file}" ]]; then |
| 147 | + echo "missing expected file: ${target_file}" >&2 |
| 148 | + exit 1 |
| 149 | +fi |
| 150 | + |
| 151 | +replace_presubmit_image "${target_file}" |
| 152 | + |
| 153 | +if git diff --quiet -- "${target_file}"; then |
| 154 | + echo "${target_file} already uses ${image}" |
| 155 | + exit 0 |
| 156 | +fi |
| 157 | + |
| 158 | +git diff --check -- "${target_file}" |
| 159 | +git diff -- "${target_file}" |
| 160 | + |
| 161 | +github_user="$(gh api user --jq .login 2>/dev/null || echo '<your-github-user>')" |
| 162 | +fork_repo="${github_user}/test-infra" |
| 163 | +fork_remote="kustomize-go-bump" |
| 164 | + |
| 165 | +echo |
| 166 | +echo "Updated ${target_file} to ${image}" |
| 167 | +echo |
| 168 | +echo "Next steps:" |
| 169 | +echo " cd ${checkout_dir}" |
| 170 | +echo " gh repo view ${fork_repo} >/dev/null 2>&1 || gh repo fork ${repo} --clone=false" |
| 171 | +echo " git remote remove ${fork_remote} >/dev/null 2>&1 || true" |
| 172 | +echo " git remote add ${fork_remote} https://github.com/${fork_repo}.git" |
| 173 | +echo " git commit -am '${commit_message}'" |
| 174 | +echo " git push -u ${fork_remote} ${branch}" |
| 175 | +echo " gh pr create --repo ${repo} --base master --head ${github_user}:${branch} --title '${commit_message}' --body '${pr_body}'" |
0 commit comments