Skip to content

Commit 3afe949

Browse files
authored
Merge pull request #18017 from MadhavJivrajani/go-version-setup-35
[release-3.5] Setup a way to consistently manage go versions across scripts and go.mods
2 parents 297130d + 3c990bc commit 3afe949

File tree

17 files changed

+162
-0
lines changed

17 files changed

+162
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: Static Analysis
3+
on: [push, pull_request]
4+
permissions: read-all
5+
jobs:
6+
run:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
10+
- id: goversion
11+
run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT"
12+
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
13+
with:
14+
go-version: ${{ steps.goversion.outputs.goversion }}
15+
- run: |
16+
set -euo pipefail
17+
18+
make verify
19+
- run: |
20+
set -euo pipefail
21+
22+
make fix
23+
24+
DIFF=$(git status --porcelain)
25+
26+
if [ -n "$DIFF" ]; then
27+
echo "These files were modified:"
28+
echo
29+
echo "$DIFF"
30+
echo
31+
exit 1
32+
fi

Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -580,3 +580,18 @@ gofail-enable: install-gofail
580580
.PHONY: gofail-disable
581581
gofail-disable: install-gofail
582582
PASSES="toggle_failpoints" ./test.sh
583+
584+
.PHONY: verify
585+
verify: verify-go-versions
586+
587+
.PHONY: verify-go-versions
588+
verify-go-versions:
589+
./scripts/verify_go_versions.sh
590+
591+
.PHONY: fix
592+
fix: sync-toolchain-directive
593+
./scripts/fix.sh
594+
595+
.PHONY: sync-toolchain-directive
596+
sync-toolchain-directive:
597+
./scripts/sync_go_toolchain_directive.sh

api/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/api/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/coreos/go-semver v0.3.0
79
github.com/gogo/protobuf v1.3.2

client/pkg/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/client/pkg/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/coreos/go-systemd/v22 v22.3.2
79
github.com/stretchr/testify v1.8.4

client/v2/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/client/v2
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/json-iterator/go v1.1.11
79
github.com/modern-go/reflect2 v1.0.1

client/v3/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/client/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/dustin/go-humanize v1.0.0
79
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0

etcdctl/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/etcdctl/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/bgentry/speakeasy v0.1.0
79
github.com/dustin/go-humanize v1.0.0

etcdutl/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/etcdutl/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
replace (
68
go.etcd.io/etcd/api/v3 => ../api
79
go.etcd.io/etcd/client/pkg/v3 => ../client/pkg

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
replace (
68
go.etcd.io/etcd/api/v3 => ./api
79
go.etcd.io/etcd/client/pkg/v3 => ./client/pkg

pkg/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/pkg/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/creack/pty v1.1.11
79
github.com/dustin/go-humanize v1.0.0

raft/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/raft/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/cockroachdb/datadriven v1.0.2
79
github.com/gogo/protobuf v1.3.2
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
# This script looks at the version present in the .go-version file and treats
4+
# that to be the value of the toolchain directive that go should use. It then
5+
# updates the toolchain directives of all go.mod files to reflect this version.
6+
#
7+
# We do this to ensure that .go-version acts as the source of truth for go versions.
8+
9+
set -euo pipefail
10+
11+
source ./scripts/test_lib.sh
12+
13+
TARGET_GO_VERSION="${TARGET_GO_VERSION:-"$(cat "${ETCD_ROOT_DIR}/.go-version")"}"
14+
find . -name 'go.mod' -exec go mod edit -toolchain=go"${TARGET_GO_VERSION}" {} \;

scripts/test_lib.sh

+24
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,27 @@ function git_assert_branch_in_sync {
384384
log_warning "Cannot verify consistency with the origin, as git is on detached branch."
385385
fi
386386
}
387+
388+
# The version present in the .go-verion is the default version that test and build scripts will use.
389+
# However, it is possible to control the version that should be used with the help of env vars:
390+
# - FORCE_HOST_GO: if set to a non-empty value, use the version of go installed in system's $PATH.
391+
# - GO_VERSION: desired version of go to be used, might differ from what is present in .go-version.
392+
# If empty, the value defaults to the version in .go-version.
393+
function determine_go_version {
394+
# Borrowing from how Kubernetes does this:
395+
# https://github.com/kubernetes/kubernetes/blob/17854f0e0a153b06f9d0db096e2cd8ab2fa89c11/hack/lib/golang.sh#L510-L520
396+
#
397+
# default GO_VERSION to content of .go-version
398+
GO_VERSION="${GO_VERSION:-"$(cat "${ETCD_ROOT_DIR}/.go-version")"}"
399+
if [ "${GOTOOLCHAIN:-auto}" != 'auto' ]; then
400+
# no-op, just respect GOTOOLCHAIN
401+
:
402+
elif [ -n "${FORCE_HOST_GO:-}" ]; then
403+
export GOTOOLCHAIN='local'
404+
else
405+
GOTOOLCHAIN="go${GO_VERSION}"
406+
export GOTOOLCHAIN
407+
fi
408+
}
409+
410+
determine_go_version

scripts/verify_go_versions.sh

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
# This script verifies that the value of the toolchain directive in the
4+
# go.mod files always match that of the .go-version file to ensure that
5+
# we accidentally don't test and release with differing versions of Go.
6+
7+
set -euo pipefail
8+
9+
source ./scripts/test_lib.sh
10+
11+
target_go_version="${target_go_version:-"$(cat "${ETCD_ROOT_DIR}/.go-version")"}"
12+
log_info "expected go toolchain directive: go${target_go_version}"
13+
log_info
14+
15+
toolchain_out_of_sync="false"
16+
go_line_violation="false"
17+
18+
# verify_go_versions takes a go.mod filepath as an argument
19+
# and checks if:
20+
# (1) go directive <= version in .go-version
21+
# (2) toolchain directive == version in .go-version
22+
function verify_go_versions() {
23+
# shellcheck disable=SC2086
24+
toolchain_version="$(go mod edit -json $1 | jq -r .Toolchain)"
25+
# shellcheck disable=SC2086
26+
go_line_version="$(go mod edit -json $1 | jq -r .Go)"
27+
if [[ "go${target_go_version}" != "${toolchain_version}" ]]; then
28+
log_error "go toolchain directive out of sync for $1, got: ${toolchain_version}"
29+
toolchain_out_of_sync="true"
30+
fi
31+
if ! printf '%s\n' "${go_line_version}" "${target_go_version}" | sort --check=silent --version-sort; then
32+
log_error "go directive in $1 is greater than maximum allowed: go${target_go_version}"
33+
go_line_violation="true"
34+
fi
35+
}
36+
37+
while read -r mod; do
38+
verify_go_versions "${mod}";
39+
done < <(find . -name 'go.mod')
40+
41+
if [[ "${toolchain_out_of_sync}" == "true" ]]; then
42+
log_error
43+
log_error "Please run scripts/sync_go_toolchain_directive.sh or update .go-version to rectify this error"
44+
fi
45+
46+
if [[ "${go_line_violation}" == "true" ]]; then
47+
log_error
48+
log_error "Please update .go-version to rectify this error, any go directive should be <= .go-version"
49+
fi
50+
51+
if [[ "${go_line_violation}" == "true" ]] || [[ "${toolchain_out_of_sync}" == "true" ]]; then
52+
exit 1
53+
fi

server/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/server/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/coreos/go-semver v0.3.0
79
github.com/coreos/go-systemd/v22 v22.3.2

tests/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/tests/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
replace (
68
go.etcd.io/etcd/api/v3 => ../api
79
go.etcd.io/etcd/client/pkg/v3 => ../client/pkg

tools/mod/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module go.etcd.io/etcd/tools/v3
22

33
go 1.21
44

5+
toolchain go1.21.10
6+
57
require (
68
github.com/alexkohler/nakedret v1.0.0
79
github.com/chzchzchz/goword v0.0.0-20170907005317-a9744cb52b03

0 commit comments

Comments
 (0)