Skip to content

Commit c8a1c4a

Browse files
committed
better handling of Go version
Some operations are sensitive to the version of Go that is used. In the past, formatting of source differed depending on the version. Right now it is the content of the vendor directory which changes when switch back and forth between 1.12 and 1.13. We don't want to impose a certain workflow on developers, like forcing all invocations of Go to run inside a container. If developers want that, they can set up their development environment accordingly. But we should warn about this aspect to raise awareness. "make" invocations which involve Go now compare against the projects Go version (specified in travis.yml) once at the beginning. This is only a warning because we don't know which future version will be compatible with the project. Vendor directory handling gets updated, too: verification is now a separate script (became too complex for make) and there is a corresponding "update-vendor.sh". In contrast to verification, updating vendor is not integrated into make and thus itself invokes the go version check.
1 parent 5e773d2 commit c8a1c4a

File tree

4 files changed

+145
-39
lines changed

4 files changed

+145
-39
lines changed

build.make

+11-39
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH))
6262
# Specific packages can be excluded from each of the tests below by setting the *_FILTER_CMD variables
6363
# to something like "| grep -v 'github.com/kubernetes-csi/project/pkg/foobar'". See usage below.
6464

65-
build-%:
65+
build-%: check-go-version-go
6666
mkdir -p bin
6767
CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-X main.version=$(REV) -extldflags "-static"' -o ./bin/$* ./cmd/$*
6868
if [ "$$ARCH" = "amd64" ]; then \
@@ -97,7 +97,7 @@ push: $(CMDS:%=push-%)
9797
clean:
9898
-rm -rf bin
9999

100-
test:
100+
test: check-go-version-go
101101

102102
.PHONY: test-go
103103
test: test-go
@@ -154,43 +154,7 @@ test-fmt:
154154
test: test-vendor
155155
test-vendor:
156156
@ echo; echo "### $@:"
157-
@ if [ -f Gopkg.toml ]; then \
158-
echo "Repo uses 'dep' for vendoring."; \
159-
case "$$(dep version 2>/dev/null | grep 'version *:')" in \
160-
*v0.[56789]*) dep check && echo "vendor up-to-date" || false;; \
161-
*) echo "skipping check, dep >= 0.5 required";; \
162-
esac; \
163-
elif [ -f go.mod ]; then \
164-
echo "Repo uses 'go mod'."; \
165-
if [ "$${JOB_NAME}" ] && \
166-
( [ "$${JOB_TYPE}" != "presubmit" ] || \
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 \
172-
echo "ERROR: vendor check failed."; \
173-
false; \
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; \
177-
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."; \
192-
fi; \
193-
fi
157+
@ ./release-tools/verify-vendor.sh
194158

195159
.PHONY: test-subtree
196160
test: test-subtree
@@ -216,3 +180,11 @@ test-shellcheck:
216180
./release-tools/verify-shellcheck.sh "$$dir" || ret=1; \
217181
done; \
218182
exit $$ret
183+
184+
# Targets in the makefile can depend on check-go-version-<path to go binary>
185+
# to trigger a warning if the x.y version of that binary does not match
186+
# what the project uses. Make ensures that this is only checked once per
187+
# invocation.
188+
.PHONY: check-go-version-%
189+
check-go-version-%:
190+
./release-tools/verify-go-version.sh "$*"

update-vendor.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
if [ -f Gopkg.toml ]; then
18+
echo "Repo uses 'dep' for vendoring."
19+
(set -x; dep ensure)
20+
elif [ -f go.mod ]; then
21+
release-tools/verify-go-version.sh "go"
22+
(set -x; env GO111MODULE=on go mod tidy && env GO111MODULE=on go mod vendor)
23+
fi

verify-go-version.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
GO="$1"
18+
19+
if [ ! "$GO" ]; then
20+
echo >&2 "usage: $0 <path to go binary>"
21+
exit 1
22+
fi
23+
24+
die () {
25+
echo "ERROR: $*"
26+
exit 1
27+
}
28+
29+
version=$("$GO" version) || die "determining version of $GO failed"
30+
# shellcheck disable=SC2001
31+
majorminor=$(echo "$version" | sed -e 's/.*go\([0-9]*\)\.\([0-9]*\).*/\1.\2/')
32+
# shellcheck disable=SC2001
33+
expected=$(grep "^ *- go:" "release-tools/travis.yml" | sed -e 's/.*go: *\([0-9]*\)\.\([0-9]*\).*/\1.\2/')
34+
35+
if [ "$majorminor" != "$expected" ]; then
36+
cat >&2 <<EOF
37+
38+
======================================================
39+
WARNING
40+
41+
This projects is tested with Go v$expected.
42+
Your current Go version is v$majorminor.
43+
This may or may not be close enough.
44+
45+
In particular test-gofmt and test-vendor
46+
are known to be sensitive to the version of
47+
Go.
48+
======================================================
49+
50+
EOF
51+
fi

verify-vendor.sh

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
if [ -f Gopkg.toml ]; then
18+
echo "Repo uses 'dep' for vendoring."
19+
case "$(dep version 2>/dev/null | grep 'version *:')" in
20+
*v0.[56789]*)
21+
if dep check; then
22+
echo "vendor up-to-date"
23+
else
24+
exit 1
25+
fi
26+
;;
27+
*) echo "skipping check, dep >= 0.5 required";;
28+
esac
29+
elif [ -f go.mod ]; then
30+
echo "Repo uses 'go mod'."
31+
# shellcheck disable=SC2235
32+
if [ "${JOB_NAME}" ] &&
33+
( [ "${JOB_TYPE}" != "presubmit" ] ||
34+
[ "$( (git diff "${PULL_BASE_SHA}..HEAD" -- go.mod go.sum vendor release-tools;
35+
git diff "${PULL_BASE_SHA}..HEAD" | grep -e '^@@.*@@ import (' -e '^[+-]import') |
36+
wc -l)" -eq 0 ] ); then
37+
echo "Skipping vendor check because the Prow pre-submit job does not affect dependencies."
38+
elif ! (set -x; env GO111MODULE=on go mod tidy); then
39+
echo "ERROR: vendor check failed."
40+
exit 1
41+
elif [ "$(git status --porcelain -- go.mod go.sum | wc -l)" -gt 0 ]; then
42+
echo "ERROR: go module files *not* up-to-date, they did get modified by 'GO111MODULE=on go mod tidy':";
43+
git diff -- go.mod go.sum
44+
exit 1
45+
elif [ -d vendor ]; then
46+
if ! (set -x; env GO111MODULE=on go mod vendor); then
47+
echo "ERROR: vendor check failed."
48+
exit 1
49+
elif [ "$(git status --porcelain -- vendor | wc -l)" -gt 0 ]; then
50+
echo "ERROR: vendor directory *not* up-to-date, it did get modified by 'GO111MODULE=on go mod vendor':"
51+
git status -- vendor
52+
git diff -- vendor
53+
exit 1
54+
else
55+
echo "Go dependencies and vendor directory up-to-date."
56+
fi
57+
else
58+
echo "Go dependencies up-to-date."
59+
fi
60+
fi

0 commit comments

Comments
 (0)