From 0741a60c5652d3b596354207a4f5e3dffeee4f5c Mon Sep 17 00:00:00 2001 From: Todd Short Date: Wed, 11 Sep 2024 14:37:56 -0400 Subject: [PATCH] Add golang version check to verify This makes sure that we are not exceeding our golang version Supported golang version is 1.22.5 Signed-off-by: Todd Short --- Makefile | 9 ++++- hack/tools/check-go-version.sh | 67 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100755 hack/tools/check-go-version.sh diff --git a/Makefile b/Makefile index f36df3f91..8ac993aa7 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ SHELL := /usr/bin/env bash -o pipefail .SHELLFLAGS := -ec export ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) +GOLANG_VERSION := 1.22.5 # Image URL to use all building/pushing image targets ifeq ($(origin IMAGE_REPO), undefined) IMAGE_REPO := quay.io/operator-framework/operator-controller @@ -95,7 +96,7 @@ lint: $(GOLANGCI_LINT) #HELP Run golangci linter. .PHONY: tidy tidy: #HELP Update dependencies. - $(Q)go mod tidy + $(Q)go mod tidy -go=$(GOLANG_VERSION) .PHONY: manifests manifests: $(CONTROLLER_GEN) #EXHELP Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. @@ -105,8 +106,12 @@ manifests: $(CONTROLLER_GEN) #EXHELP Generate WebhookConfiguration, ClusterRole generate: $(CONTROLLER_GEN) #EXHELP Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." +.PHONY: go-verify +go-verify: + hack/tools/check-go-version.sh $(GOLANG_VERSION) + .PHONY: verify -verify: tidy fmt vet generate manifests crd-ref-docs #HELP Verify all generated code is up-to-date. +verify: tidy fmt vet generate manifests crd-ref-docs go-verify #HELP Verify all generated code is up-to-date. git diff --exit-code .PHONY: fix-lint diff --git a/hack/tools/check-go-version.sh b/hack/tools/check-go-version.sh new file mode 100755 index 000000000..49fa9a2c2 --- /dev/null +++ b/hack/tools/check-go-version.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +GO_VER=$1 +OLDIFS="${IFS}" +IFS='.' INPUT_VERS=(${GO_VER}) +IFS="${OLDIFS}" + +if [ ${#INPUT_VERS[*]} -ne 3 -a ${#INPUT_VERS[*]} -ne 2 ]; then + echo "Invalid go version: ${GO_VER}" + exit 1 +fi + +GO_MAJOR=${INPUT_VERS[0]} +GO_MINOR=${INPUT_VERS[1]} +GO_PATCH=${INPUT_VERS[2]} + +check_version () { + whole=$1 + file=$2 + OLDIFS="${IFS}" + IFS='.' ver=(${whole}) + IFS="${OLDIFS}" + + if [ ${#ver[*]} -eq 2 ] ; then + if [ ${ver[0]} -gt ${GO_MAJOR} ] ; then + echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)" + exit 1 + fi + if [ ${ver[1]} -gt ${GO_MINOR} ] ; then + echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)" + exit 1 + fi + echo "Version ${whole} in ${file} is good" + return + fi + if [ ${#INPUT_VERS[*]} -eq 2 ]; then + echo "Bad golang version ${whole} in ${file} (expecting only major.minor version)" + exit 1 + fi + if [ ${#ver[*]} -ne 3 ] ; then + echo "Badly formatted golang version ${whole} in ${file}" + exit 1 + fi + + if [ ${ver[0]} -gt ${GO_MAJOR} ]; then + echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)" + exit 1 + fi + if [ ${ver[1]} -gt ${GO_MINOR} ]; then + echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)" + exit 1 + fi + if [ ${ver[1]} -eq ${GO_MINOR} -a ${ver[2]} -gt ${GO_PATCH} ]; then + echo "Bad golang version ${whole} in ${file} (expected ${GO_VER} or less)" + exit 1 + fi + echo "Version ${whole} in ${file} is good" +} + +for f in $(find . -name "*.mod"); do + v=$(sed -En 's/^go (.*)$/\1/p' ${f}) + if [ -z ${v} ]; then + echo "Skipping ${f}: no version found" + else + check_version ${v} ${f} + fi +done