Skip to content

Commit c1078a6

Browse files
committed
go-get-kubernetes.sh: automate Kubernetes dependency handling
This script handles the necessary "replace" statements and determines which packages need to be updated in lockstep.
1 parent 194289a commit c1078a6

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,23 @@ verify that it's content is up-to-date.
140140
The `vendor` directory is optional. It is still present in projects
141141
because it avoids downloading sources during CI builds. If this is no
142142
longer deemed necessary, then a project can also remove the directory.
143+
144+
When using packages that are part of the Kubernetes source code, the
145+
commands above are not enough because the [lack of semantic
146+
versioning](https://github.com/kubernetes/kubernetes/issues/72638)
147+
prevents `go mod` from finding newer releases. Importing directly from
148+
`kubernetes/kubernetes` also needs `replace` statements to override
149+
the fake `v0.0.0` versions
150+
(https://github.com/kubernetes/kubernetes/issues/79384). The
151+
`go-get-kubernetes.sh` script can be used to update all packages in
152+
lockstep to a different Kubernetes version. It takes a single version
153+
number like "1.16.0".
154+
155+
Conversion of a repository that uses `dep` to `go mod` can be done with:
156+
157+
GO111MODULE=on go mod init
158+
release-tools/go-get-kubernetes.sh <current Kubernetes version from Gopkg.toml>
159+
GO111MODULE=on go mod tidy
160+
GO111MODULE=on go mod vendor
161+
git rm -f Gopkg.toml Gopkg.lock
162+
git add go.mod go.sum vendor

go-get-kubernetes.sh

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)