diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 6efba441..ceb0a7b6 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -8,7 +8,7 @@ on: jobs: bin: name: Build binary and run unit tests - runs-on: [ubuntu-18.04] + runs-on: [ubuntu-latest] steps: - name: Set up Go 1.16 uses: actions/setup-go@v1 @@ -20,3 +20,16 @@ jobs: run: make bin - name: Run tests run: make test + + check-tidy: + name: Check tidy + runs-on: [ubuntu-latest] + steps: + - name: Set up Go 1.16 + uses: actions/setup-go@v1 + with: + go-version: 1.16 + - name: Check-out code + uses: actions/checkout@v2 + - name: Check tidy + run: make test-tidy diff --git a/Makefile b/Makefile index 2fcae49e..194cd3ed 100644 --- a/Makefile +++ b/Makefile @@ -158,5 +158,15 @@ packagemanifests: kustomize manifests cd config/manager && $(KUSTOMIZE) edit set image antrea-operator=$(IMG) $(KUSTOMIZE) build config/manifests | operator-sdk generate packagemanifests -q --version $(VERSION) $(PKG_MAN_OPTS) +test-tidy: + @echo + @echo "===> Checking go.mod tidiness <===" + ./hack/tidy-check.sh + +.PHONY: tidy +tidy: + rm -f go.sum + go mod tidy + .PHONY: bin bin: manager diff --git a/hack/tidy-check.sh b/hack/tidy-check.sh new file mode 100755 index 00000000..fad6b77a --- /dev/null +++ b/hack/tidy-check.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +# Copyright 2019 Antrea Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set +e +trap cleanup EXIT + +THIS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" + +pushd "$(dirname "$THIS_DIR")" >/dev/null || exit + +PROJECT_RELATIVE_DIR=${1:-.} +PROJECT_DIR=$(dirname "$THIS_DIR")/$PROJECT_RELATIVE_DIR + +MOD_FILE="$PROJECT_DIR/go.mod" +SUM_FILE="$PROJECT_DIR/go.sum" +TMP_DIR="$THIS_DIR/.tmp.tidy-check" +TMP_MOD_FILE="$TMP_DIR/go.mod" +TMP_SUM_FILE="$TMP_DIR/go.sum" + +# if Go environment variable is set, use it as it is, otherwise default to "go" +: "${GO:=go}" +: "${TARGET_GO_VERSION:=1.16}" + +TIDY_COMMAND="cd $PROJECT_RELATIVE_DIR && $GO mod tidy >> /dev/null 2>&1" + +function echoerr { + echo >&2 "$@" +} + +function general_help { + echoerr "Please run the following command to generate a new go.mod & go.sum:" + echoerr " \$ make tidy" +} + +function precheck { + if [ ! -r "$MOD_FILE" ]; then + echoerr "no go.mod found" + general_help + exit 1 + fi + if [ ! -r "$SUM_FILE" ]; then + echoerr "no go.sum found" + general_help + exit 1 + fi + mkdir -p "$TMP_DIR" +} + +function tidy { + cp "$MOD_FILE" "$TMP_MOD_FILE" + mv "$SUM_FILE" "$TMP_SUM_FILE" + + if [ -n "$GO" ]; then + /bin/bash -c "$TIDY_COMMAND" + else + docker run --rm -u "$(id -u):$(id -g)" \ + -e "GOCACHE=/tmp/gocache" \ + -e "GOPATH=/tmp/gopath" \ + -w /usr/src/vmware/antrea-operator-for-kubernetes \ + -v "$(dirname "$THIS_DIR"):/usr/src/vmware/antrea-operator-for-kubernetes" \ + golang:$TARGET_GO_VERSION bash -c "$TIDY_COMMAND" + fi +} + +function cleanup { + if [ -f "$TMP_MOD_FILE" ]; then + mv "$TMP_MOD_FILE" "$MOD_FILE" + fi + if [ -f "$TMP_SUM_FILE" ]; then + mv "$TMP_SUM_FILE" "$SUM_FILE" + fi + if [ -d "$TMP_DIR" ]; then + rm -fr "$TMP_DIR" + fi +} + +function failed { + echoerr "'go mod tidy' failed, there are errors in dependencies" + general_help + exit 1 +} + +function check { + MOD_DIFF=$(diff "$MOD_FILE" "$TMP_MOD_FILE") + SUM_DIFF=$(diff "$SUM_FILE" "$TMP_SUM_FILE") + if [ -n "$MOD_DIFF" ] || [ -n "$SUM_DIFF" ]; then + echo "=== go.mod diff ===" + echo $MOD_DIFF + echo "=== go.sum diff ===" + echo $SUM_DIFF + + echoerr "dependencies are not tidy" + general_help + exit 1 + fi +} + +precheck +if tidy; then + check +else + failed +fi + +popd >/dev/null || exit