Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hack: add go-version-check.sh script #315

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM docker.io/golang:1.19 as builder
FROM docker.io/golang:1.20 as builder
ARG GIT_VERSION="(unset)"
ARG COMMIT_ID="(unset)"
ARG ARCH=""
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ bundle-build:
@false
# See vcs history for how this could be restored or adapted in the future.

.PHONY: check check-revive check-golangci-lint check-format check-yaml check-gosec
.PHONY: check check-revive check-golangci-lint check-format check-yaml check-gosec check-dockerfile-go-version

check: check-revive check-golangci-lint check-format vet check-yaml check-gosec
check: check-revive check-golangci-lint check-format vet check-yaml check-gosec check-dockerfile-go-version

check-format:
! $(GOFMT_CMD) $(CHECK_GOFMT_FLAGS) . | sed 's,^,formatting error: ,' | grep 'go$$'
Expand All @@ -227,6 +227,10 @@ check-yaml:
check-gosec: gosec
$(GOSEC) -quiet -exclude=G101 -fmt json ./...

check-dockerfile-go-version:
# use go-version-check.sh --show to list vaild golang builder images
$(CURDIR)/hack/go-version-check.sh --check

check-gitlint: gitlint
$(GITLINT) -C .gitlint --commits origin/master.. lint

Expand Down
113 changes: 113 additions & 0 deletions hack/go-version-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash
#
# List or check that the golang version embedded in files (specifically our
# Dockerfile) is using a supported version of Go.
# Run with --show to list the images the script thinks are current.
# Run with --check (the default) to verify the Dockerfile contains a current
# image & version tag.
#

set -e
SCRIPT_DIR="$(readlink -f "$(dirname "${0}")")"
PROJECT_DIR="$(readlink -f "${SCRIPT_DIR}/..")"
WORKDIR="$(mktemp -d)"
anoopcs9 marked this conversation as resolved.
Show resolved Hide resolved
CONTAINER_FILES=("${PROJECT_DIR}/Dockerfile")
GOVERSURL='https://go.dev/dl/?mode=json'
ACTION=check


fetch_versions() {
local url="$1"
anoopcs9 marked this conversation as resolved.
Show resolved Hide resolved
local out="$2"
if [ -f "${out}" ]; then
return
fi
echo "Fetching Go versions..." >&2
curl --fail -sL -o "${out}" "${url}"
}

extract_versions() {
local jsonfile="$1"
jq -r '.[].version' < "${jsonfile}" | \
cut -d. -f1,2 | sort -u | sed -e 's,^go,,'
}

to_images() {
for a in "$@"; do
echo "docker.io/golang:${a}"
done
}

cleanup() {
# only used in trap, ignoe unreachable error
# shellcheck disable=SC2317
rm -rf "${WORKDIR}"
}
trap cleanup EXIT


opts=$(getopt --name "$0" \
-o "csf:" -l "check,show,file:,url:,versionsfile:" -- "$@")
anoopcs9 marked this conversation as resolved.
Show resolved Hide resolved
eval set -- "$opts"

cli_cfiles=()
while true ; do
case "$1" in
-c|--check)
ACTION=check
shift
;;
-s|--show)
ACTION=show
shift
;;
-f|--file)
cli_cfiles+=("$2")
shift 2
;;
--url)
GOVERSURL="$2"
shift 2
;;
--versionsfile)
GOVERSIONSFILE="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "unexpected option: $1" >&2
exit 2
;;
esac
done

if [ "${#cli_cfiles}" -gt 0 ]; then
CONTAINER_FILES=("${cli_cfiles[@]}")
fi

GV="${GOVERSIONSFILE:-${WORKDIR}/go-versions.json}"
fetch_versions "${GOVERSURL}" "${GV}"
mapfile -t go_major_vers < <(extract_versions "${GV}")

echo "Found Go versions:" "${go_major_vers[@]}" >&2
if [ "${ACTION}" = show ]; then
to_images "${go_major_vers[@]}"
exit 1
fi

mapfile -t valid_images < <(to_images "${go_major_vers[@]}")
errors=0
for cf in "${CONTAINER_FILES[@]}"; do
echo "Checking $cf ..." >&2
if grep -q -e "${valid_images[0]}" -e "${valid_images[1]}" "${cf}" ; then
echo "${cf}: OK" >&2
else
echo "${cf}: no current golang image found" >&2
errors=1
fi
done

exit "${errors}"