|
| 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