diff --git a/hack/lib/util.sh b/hack/lib/util.sh index 02cf4e3aa7d..ce89e5f5104 100755 --- a/hack/lib/util.sh +++ b/hack/lib/util.sh @@ -759,6 +759,27 @@ function kube::util::ensure-gnu-sed { kube::util::sourced_variable "${SED}" } +# kube::util::ensure-gnu-date +# Determines which date binary is gnu-date on linux/darwin +# +# Sets: +# DATE: The name of the gnu-date binary +# +function kube::util::ensure-gnu-date { + # NOTE: the echo below is a workaround to ensure date is executed before the grep. + # see: https://github.com/kubernetes/kubernetes/issues/87251 + date_help="$(LANG=C date --help 2>&1 || true)" + if echo "${date_help}" | grep -q "GNU\|BusyBox"; then + DATE="date" + elif command -v gdate &>/dev/null; then + DATE="gdate" + else + kube::log::error "Failed to find GNU date as date or gdate. If you are on Mac: brew install coreutils." >&2 + return 1 + fi + kube::util::sourced_variable "${DATE}" +} + # kube::util::check-file-in-alphabetical-order # Check that the file is in alphabetical order # diff --git a/hack/lib/version.sh b/hack/lib/version.sh index 8387144206d..f8dcc7b1ba8 100644 --- a/hack/lib/version.sh +++ b/hack/lib/version.sh @@ -163,7 +163,9 @@ kube::version::ldflags() { ) } - add_ldflag "buildDate" "$(date ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ')" + kube::util::ensure-gnu-date + + add_ldflag "buildDate" "$(${DATE} ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ')" if [[ -n ${KUBE_GIT_COMMIT-} ]]; then add_ldflag "gitCommit" "${KUBE_GIT_COMMIT}" add_ldflag "gitTreeState" "${KUBE_GIT_TREE_STATE}"