|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2019 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 | +# This script can be used while converting a repo from "dep" to "go mod" |
| 18 | +# by calling it after "go mod init" or to update the Kubernetes packages |
| 19 | +# in a repo that has already been converted. Only packages that are |
| 20 | +# part of kubernetes/kubernetes and thus part of a Kubernetes release |
| 21 | +# are modified. Other k8.io packages (like k8s.io/klog, k8s.io/utils) |
| 22 | +# need to be updated separately. |
| 23 | + |
| 24 | +set -o pipefail |
| 25 | + |
| 26 | +cmd=$0 |
| 27 | + |
| 28 | +function help () { |
| 29 | + echo "$cmd <kubernetes version = x.y.z> - update all components from kubernetes/kubernetes to that version" |
| 30 | +} |
| 31 | + |
| 32 | +if [ $# -ne 1 ]; then |
| 33 | + help |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | +case "$1" in -h|--help|help) help; exit 0;; esac |
| 37 | + |
| 38 | +die () { |
| 39 | + echo >&2 "$@" |
| 40 | + exit 1 |
| 41 | +} |
| 42 | + |
| 43 | +k8s="$1" |
| 44 | + |
| 45 | +# If the repo imports k8s.io/kubernetes (directly or indirectly), then |
| 46 | +# "go mod" will try to find "v0.0.0" versions because |
| 47 | +# k8s.io/kubernetes has those in it's go.mod file |
| 48 | +# (https://github.com/kubernetes/kubernetes/blob/2bd9643cee5b3b3a5ecbd3af49d09018f0773c77/go.mod#L146-L157). |
| 49 | +# (https://github.com/kubernetes/kubernetes/issues/79384). |
| 50 | +# |
| 51 | +# We need to replicate the replace statements to override those fake |
| 52 | +# versions also in our go.mod file (idea and some code from |
| 53 | +# https://github.com/kubernetes/kubernetes/issues/79384#issuecomment-521493597). |
| 54 | +mods=$( (set -x; curl --silent --show-error --fail "https://raw.githubusercontent.com/kubernetes/kubernetes/v${k8s}/go.mod") | |
| 55 | + sed -n 's|.*k8s.io/\(.*\) => ./staging/src/k8s.io/.*|k8s.io/\1|p' |
| 56 | + ) || die "failed to determine Kubernetes staging modules" |
| 57 | +for mod in $mods; do |
| 58 | + # The presence of a potentially incomplete go.mod file affects this command, |
| 59 | + # so move elsewhere. |
| 60 | + modinfo=$(set -x; cd /; env GO111MODULE=on go mod download -json "$mod@kubernetes-${k8s}") || |
| 61 | + die "failed to determine version of $mod: $modinfo" |
| 62 | + v=$(echo "$modinfo" | sed -n 's|.*"Version": "\(.*\)".*|\1|p') |
| 63 | + (set -x; env GO111MODULE=on go mod edit "-replace=$mod=$mod@$v") || die "'go mod edit' failed" |
| 64 | +done |
| 65 | + |
| 66 | +packages= |
| 67 | + |
| 68 | +# Beware that we have to work with packages, not modules (i.e. no -m |
| 69 | +# flag), because some modules trigger a "no Go code except tests" |
| 70 | +# error. Getting their packages works. |
| 71 | +if ! packages=$( (set -x; env GO111MODULE=on go list all) | grep ^k8s.io/ | sed -e 's; *;;'); then |
| 72 | + cat >&2 <<EOF |
| 73 | +
|
| 74 | +Warning: "GO111MODULE=on go list all" failed, trying individual packages instead. |
| 75 | +
|
| 76 | +EOF |
| 77 | + if ! packages=$( (set -x; env GO111MODULE=on go list -f '{{ join .Deps "\n" }}' ./...) | grep ^k8s.io/); then |
| 78 | + cat >&2 <<EOF |
| 79 | +
|
| 80 | +ERROR: could not obtain package list, both of these commands failed: |
| 81 | + GO111MODULE=on go list all |
| 82 | + GO111MODULE=on go list -f '{{ join .Deps "\n" }}' ./pkg/... |
| 83 | +EOF |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | +fi |
| 87 | + |
| 88 | +deps= |
| 89 | +for package in $packages; do |
| 90 | + # Some k8s.io packages do not come from Kubernetes staging and |
| 91 | + # thus have different versioning (or none at all...). We need to |
| 92 | + # skip those. We know what packages are from staging because we |
| 93 | + # now have "replace" statements for them in go.mod. |
| 94 | + # |
| 95 | + # shellcheck disable=SC2001 |
| 96 | + module=$(echo "$package" | sed -e 's;k8s.io/\([^/]*\)/.*;k8s.io/\1;') |
| 97 | + if grep -q -w "$module *=>" go.mod; then |
| 98 | + deps="$deps $(echo "$package" | sed -e "s;\$;@kubernetes-$k8s;" -e 's;^k8s.io/kubernetes\(/.*\)@kubernetes-;k8s.io/kubernetes\1@v;')" |
| 99 | + fi |
| 100 | +done |
| 101 | + |
| 102 | +# shellcheck disable=SC2086 |
| 103 | +(set -x; env GO111MODULE=on go get $deps 2>&1) || die "go get failed" |
| 104 | +echo "SUCCESS" |
0 commit comments