Skip to content

Commit b01eb69

Browse files
.*: add presubmit check for verifying go directive changes
This commit adds a script that checks the changed version in the go.mod file with a certain maximum version that the go directive can have. We set the maximum version of the go directive as 1.20 here because the oldest go directive that exists on our supported release branches in k/k is 1.20. This commit additionally changes the requisite GH action to incorporate this check. Signed-off-by: Madhav Jivrajani <[email protected]>
1 parent e7106e6 commit b01eb69

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

.github/workflows/test.yml

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ jobs:
3737
- name: Test
3838
run: |
3939
make test
40+
verify-go-directive:
41+
strategy:
42+
matrix:
43+
go-version: [1.20.x, 1.21.x]
44+
platform: [ubuntu-latest, macos-latest]
45+
runs-on: ${{ matrix.platform }}
46+
steps:
47+
- name: Install Go
48+
uses: actions/setup-go@v3
49+
with:
50+
go-version: ${{ matrix.go-version }}
51+
- name: Checkout code
52+
uses: actions/checkout@v2
53+
- name: Verify go directive
54+
run: |
55+
make verify-go-directive
4056
lint:
4157
runs-on: ubuntu-latest
4258
steps:

Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ vet:
3838
.PHONY: update-fmt
3939
update-fmt:
4040
gofmt -s -w .
41+
42+
# We set the maximum version of the go directive as 1.20 here
43+
# because the oldest go directive that exists on our supported
44+
# release branches in k/k is 1.20.
45+
.PHONY: verify-go-directive
46+
verify-go-directive:
47+
./hack/verify-go-directive.sh -g 1.20

hack/verify-go-directive.sh

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2024 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+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
function usage {
22+
local script="$(basename $0)"
23+
24+
echo >&2 "Usage: ${script} [-g <maximum go directive>]
25+
26+
This script should be run at the root of a module.
27+
28+
-g <maximum go directive>
29+
Compare the go directive in the local working copy's go.mod
30+
to the specified maximum version it can be. Versions provided
31+
here are of the form 1.x.y, without the 'go' prefix.
32+
33+
Examples:
34+
${script} -g 1.20
35+
${script} -g 1.21.6
36+
"
37+
exit 1
38+
}
39+
40+
max=""
41+
while getopts g: o
42+
do case "$o" in
43+
g) max="$OPTARG";;
44+
[?]) usage;;
45+
esac
46+
done
47+
48+
# If max is empty, print usage and error
49+
if [[ -z "${max}" ]]; then
50+
usage;
51+
fi
52+
53+
# Don't specify the version with the go prefix, just 1.x.y will do.
54+
if ! printf '%s\n' "${max}" | grep -v -q "^go"; then
55+
usage;
56+
fi
57+
58+
current=$(grep '^go [1-9]*' go.mod | cut -d ' ' -f2)
59+
if [[ -z "${current}" ]]; then
60+
echo >&2 "FAIL: could not get value of go directive from go.mod"
61+
exit 1
62+
fi
63+
64+
if ! printf '%s\n' "${current}" "${max}" | sort --check=silent --version-sort; then
65+
echo >&2 "FAIL: current Go directive ${current} is greater than ${max}"
66+
exit 1
67+
fi

0 commit comments

Comments
 (0)