Skip to content

Commit 2c81919

Browse files
authored
Merge pull request kubernetes-csi#34 from pohly/go-mod-tidy
update Go mod support
2 parents 518d6af + c1078a6 commit 2c81919

File tree

3 files changed

+203
-10
lines changed

3 files changed

+203
-10
lines changed

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,57 @@ Kubernetes releases:
106106

107107
CSI_PROW_KUBERNETES_VERSION=1.13.3 ./.prow.sh
108108
CSI_PROW_KUBERNETES_VERSION=latest ./.prow.sh
109+
110+
Dependencies and vendoring
111+
--------------------------
112+
113+
Most projects will (eventually) use `go mod` to manage
114+
dependencies. `dep` is also still supported by `csi-release-tools`,
115+
but not documented here because it's not recommended anymore.
116+
117+
The usual instructions for using [go
118+
modules](https://github.com/golang/go/wiki/Modules) apply. Here's a cheat sheet
119+
for some of the relevant commands:
120+
- list available updates: `GO111MODULE=on go list -u -m all`
121+
- update or add a single dependency: `GO111MODULE=on go get <package>`
122+
- update all dependencies to their next minor or patch release:
123+
`GO111MODULE=on go get ./...` (add `-u=patch` to limit to patch
124+
releases)
125+
- lock onto a specific version: `GO111MODULE=on go get <package>@<version>`
126+
- clean up `go.mod`: `GO111MODULE=on go mod tidy`
127+
- update vendor directory: `GO111MODULE=on go mod vendor`
128+
129+
`GO111MODULE=on` can be left out when using Go >= 1.13 or when the
130+
source is checked out outside of `$GOPATH`.
131+
132+
`go mod tidy` must be used to ensure that the listed dependencies are
133+
really still needed. Changing import statements or a tentative `go
134+
get` can result in stale dependencies.
135+
136+
The `test-vendor` verifies that it was used when run locally or in a
137+
pre-merge CI job. If a `vendor` directory is present, it will also
138+
verify that it's content is up-to-date.
139+
140+
The `vendor` directory is optional. It is still present in projects
141+
because it avoids downloading sources during CI builds. If this is no
142+
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

build.make

+45-10
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ test-fmt:
130130
# - the fabricated merge commit leaves go.mod, go.sum and vendor dir unchanged
131131
# - release-tools also didn't change (changing rules or Go version might lead to
132132
# a different result and thus must be tested)
133+
# - import statements not changed (because if they change, go.mod might have to be updated)
134+
#
135+
# "git diff" is intelligent enough to annotate changes inside the "import" block in
136+
# the start of the diff hunk:
137+
#
138+
# diff --git a/rpc/common.go b/rpc/common.go
139+
# index bb4a5c4..5fa4271 100644
140+
# --- a/rpc/common.go
141+
# +++ b/rpc/common.go
142+
# @@ -21,7 +21,6 @@ import (
143+
# "fmt"
144+
# "time"
145+
#
146+
# - "google.golang.org/grpc"
147+
# "google.golang.org/grpc/codes"
148+
# "google.golang.org/grpc/status"
149+
#
150+
# We rely on that to find such changes.
151+
#
152+
# Vendoring is optional when using go.mod.
133153
.PHONY: test-vendor
134154
test: test-vendor
135155
test-vendor:
@@ -140,22 +160,37 @@ test-vendor:
140160
*v0.[56789]*) dep check && echo "vendor up-to-date" || false;; \
141161
*) echo "skipping check, dep >= 0.5 required";; \
142162
esac; \
143-
else \
144-
echo "Repo uses 'go mod' for vendoring."; \
163+
elif [ -f go.mod ]; then \
164+
echo "Repo uses 'go mod'."; \
145165
if [ "$${JOB_NAME}" ] && \
146166
( [ "$${JOB_TYPE}" != "presubmit" ] || \
147-
[ $$(git diff "${PULL_BASE_SHA}..HEAD" -- go.mod go.sum vendor release-tools | wc -l) -eq 0 ] ); then \
148-
echo "Skipping vendor check because the Prow pre-submit job does not change vendoring."; \
149-
elif ! GO111MODULE=on go mod vendor; then \
167+
[ $$( (git diff "${PULL_BASE_SHA}..HEAD" -- go.mod go.sum vendor release-tools; \
168+
git diff "${PULL_BASE_SHA}..HEAD" | grep -e '^@@.*@@ import (' -e '^[+-]import') | \
169+
wc -l) -eq 0 ] ); then \
170+
echo "Skipping vendor check because the Prow pre-submit job does not affect dependencies."; \
171+
elif ! GO111MODULE=on go mod tidy; then \
150172
echo "ERROR: vendor check failed."; \
151173
false; \
152-
elif [ $$(git status --porcelain -- vendor | wc -l) -gt 0 ]; then \
153-
echo "ERROR: vendor directory *not* up-to-date, it did get modified by 'GO111MODULE=on go mod vendor':"; \
154-
git status -- vendor; \
155-
git diff -- vendor; \
174+
elif [ $$(git status --porcelain -- go.mod go.sum | wc -l) -gt 0 ]; then \
175+
echo "ERROR: go module files *not* up-to-date, they did get modified by 'GO111MODULE=on go mod tidy':"; \
176+
git diff -- go.mod go.sum; \
156177
false; \
178+
elif [ -d vendor ]; then \
179+
if ! GO111MODULE=on go mod vendor; then \
180+
echo "ERROR: vendor check failed."; \
181+
false; \
182+
elif [ $$(git status --porcelain -- vendor | wc -l) -gt 0 ]; then \
183+
echo "ERROR: vendor directory *not* up-to-date, it did get modified by 'GO111MODULE=on go mod vendor':"; \
184+
git status -- vendor; \
185+
git diff -- vendor; \
186+
false; \
187+
else \
188+
echo "Go dependencies and vendor directory up-to-date."; \
189+
fi; \
190+
else \
191+
echo "Go dependencies up-to-date."; \
157192
fi; \
158-
fi;
193+
fi
159194

160195
.PHONY: test-subtree
161196
test: test-subtree

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)