From c9039139973ef321496a611600ced2a8910fddc9 Mon Sep 17 00:00:00 2001 From: Steven Fairchild Date: Wed, 2 Aug 2023 14:00:41 -0400 Subject: [PATCH] Add timeout for go generate This will prevent excessively long go generate runs from occuring in the Build and Push Binary and Container pipelines. Timeout sends a list of exit codes we can expect if something goes wrong. Here they are used to help provide more information about the failure. go generate doesn't support a timeout value directly. --- Makefile | 2 +- hack/ci-utils/go_generate.sh | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100755 hack/ci-utils/go_generate.sh diff --git a/Makefile b/Makefile index f62467ed10c..0003b3123a4 100644 --- a/Makefile +++ b/Makefile @@ -88,7 +88,7 @@ discoverycache: $(MAKE) generate generate: - go generate ./... + hack/ci-utils/go_generate.sh generate-guardrails: cd pkg/operator/controllers/guardrails/policies && ./scripts/generate.sh > /dev/null diff --git a/hack/ci-utils/go_generate.sh b/hack/ci-utils/go_generate.sh new file mode 100755 index 00000000000..6adac525dd9 --- /dev/null +++ b/hack/ci-utils/go_generate.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +set -o errexit + +# Variables checked for timeout values, defaults used if unset +soft_timeout="${GO_GENERATE_SOFT_TIMEOUT:-2h}" +hard_timeout="${GO_GENERATE_HARD_TIMEOUT:-2h}" + +if [[ -n $CI ]]; then + set -x +fi + + +declare timeout_exitcode go_generate +# go generate doesn't support timeout itself, so --preserve-status wouldn't help us as it would simply be the status timeout sent to go. +go_generate="go generate ./..." +echo "Running ${go_generate} sending SIGTERM after ${soft_timeout} and SIGKILL after ${hard_timeout}" +echo "${go_generate}" +# shellcheck disable=SC2086 +timeout --foreground --kill-after="${hard_timeout}" -v -s SIGTERM "${soft_timeout}" ${go_generate} +timeout_exitcode=$? + +# Capture expected exit codes +case $timeout_exitcode in +124) + echo "Command ${go_generate} timed out." + ;; +125) + echo "The command \"timeout\" failed trying to run ${go_generate}. Note that ${go_generate} did not fail." + ;; +126) + echo "Command found but cannot be invoked." + ;; +127) + echo "Command not found recieved." + ;; +137) + echo "timeout or command recieved kill signal 9) SIGKILL." + ;; +-) + echo "Command go generate ./... exited with exit_code code ${-}." + ;; +0) + echo "Command ${go_generate} completed successfully, exit code ${timeout_exitcode}." + ;; +*) + echo "Unexpected error code received: ${timeout_exitcode}." + ;; +esac + +exit "${timeout_exitcode}"