From 634477921a391257ee978b43c7da89dc5d37c836 Mon Sep 17 00:00:00 2001 From: oraz Date: Wed, 18 Sep 2024 12:47:17 +0300 Subject: [PATCH] Build manager binary in a modular way Allow overriding CGO_ENABLED value, debug flags as part of ldflags, and exporting GOEXPERIMENT --- hack/build.sh | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/hack/build.sh b/hack/build.sh index 14d78b1d..952b58e0 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -1,5 +1,4 @@ -#!/bin/bash -set -ex +#!/bin/bash -ex GIT_VERSION=$(git describe --always --tags || true) VERSION=${CI_UPSTREAM_VERSION:-${GIT_VERSION}} @@ -9,8 +8,24 @@ BUILD_DATE=$(date --utc -Iseconds) mkdir -p bin -LDFLAGS="-s -w " -LDFLAGS+="-X github.com/medik8s/self-node-remediation/version.Version=${VERSION} " -LDFLAGS+="-X github.com/medik8s/self-node-remediation/version.GitCommit=${COMMIT} " -LDFLAGS+="-X github.com/medik8s/self-node-remediation/version.BuildDate=${BUILD_DATE} " -GOFLAGS=-mod=vendor CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o bin/manager main.go \ No newline at end of file +LDFLAGS_VALUE="-X github.com/medik8s/self-node-remediation/version.Version=${VERSION} " +LDFLAGS_VALUE+="-X github.com/medik8s/self-node-remediation/version.GitCommit=${COMMIT} " +LDFLAGS_VALUE+="-X github.com/medik8s/self-node-remediation/version.BuildDate=${BUILD_DATE} " +# allow override for debugging flags +LDFLAGS_DEBUG="${LDFLAGS_DEBUG:-" -s -w"}" +LDFLAGS_VALUE+="${LDFLAGS_DEBUG}" +# must be single quoted for use in GOFLAGS, and for more options see https://pkg.go.dev/cmd/link +LDFLAGS="'-ldflags=${LDFLAGS_VALUE}'" + +# add ldflags to goflags +export GOFLAGS+=" ${LDFLAGS}" +echo "goflags: ${GOFLAGS}" + +# allow override and use zero by default- static linking +export CGO_ENABLED=${CGO_ENABLED:-0} +echo "cgo: ${CGO_ENABLED}" + +# export in case it was set +export GOEXPERIMENT="${GOEXPERIMENT}" + +GOOS=linux GOARCH=amd64 go build -o bin/manager main.go