Skip to content

Commit

Permalink
Add timeout for go generate
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
s-fairchild committed Sep 13, 2023
1 parent 5b94eb4 commit c903913
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions hack/ci-utils/go_generate.sh
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit c903913

Please sign in to comment.