Skip to content

Commit

Permalink
Add go buildinfo to velero to assist with vmware-tanzu#8397
Browse files Browse the repository at this point in the history
  • Loading branch information
kaovilai committed Nov 12, 2024
1 parent 61c3332 commit a217d53
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
26 changes: 24 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT

# GOTOOLCHAIN=auto allows the build to use the latest toolchain from the image instead of specified in go.mod
ENV CGO_ENABLED=0 \
GO111MODULE=on \
GOPROXY=${GOPROXY} \
GOOS=${TARGETOS} \
GOARCH=${TARGETARCH} \
GOARM=${TARGETVARIANT} \
GOTOOLCHAIN=auto \
LDFLAGS="-X ${PKG}/pkg/buildinfo.Version=${VERSION} -X ${PKG}/pkg/buildinfo.GitSHA=${GIT_SHA} -X ${PKG}/pkg/buildinfo.GitTreeState=${GIT_TREE_STATE} -X ${PKG}/pkg/buildinfo.ImageRegistry=${REGISTRY}"

WORKDIR /go/src/github.com/vmware-tanzu/velero

# Cache dependency downloads
COPY go.mod /go/src/github.com/vmware-tanzu/velero/go.mod
COPY go.sum /go/src/github.com/vmware-tanzu/velero/go.sum
RUN go mod download

COPY . /go/src/github.com/vmware-tanzu/velero

RUN mkdir -p /output/usr/bin && \
Expand All @@ -45,6 +52,15 @@ RUN mkdir -p /output/usr/bin && \
go build -o /output/velero-helper \
-ldflags "${LDFLAGS}" ${PKG}/cmd/velero-helper && \
go clean -modcache -cache
# check container image go version is used to build rather than toolchain from go.mod
RUN export goCliVersion=$(go version | cut -d ' ' -f 3) && \
export veleroClientGoBuildInfoVersion=$(/output/${BIN} version --client-only) && \
echo "go version: $goCliVersion" && \
echo "velero client go version: $veleroClientGoBuildInfoVersion" && \
if [ $(go version | cut -d " " -f 3) = $(/output/${BIN} version --client-only | grep "Go version" | cut -d " " -f 3) ]; then \
echo "equal"; \
else (echo "not building using latest toolchain from image" >&2 && exit 1); \
fi

# Restic binary build section
FROM --platform=$BUILDPLATFORM golang:1.22-bookworm AS restic-builder
Expand All @@ -61,7 +77,13 @@ ENV CGO_ENABLED=0 \
GOPROXY=${GOPROXY} \
GOOS=${TARGETOS} \
GOARCH=${TARGETARCH} \
GOARM=${TARGETVARIANT}
GOARM=${TARGETVARIANT} \
GOTOOLCHAIN=auto

# Cache dependency downloads
COPY go.mod /go/src/github.com/vmware-tanzu/velero/go.mod
COPY go.sum /go/src/github.com/vmware-tanzu/velero/go.sum
RUN cd /go/src/github.com/vmware-tanzu/velero/ && go mod download

COPY . /go/src/github.com/vmware-tanzu/velero

Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module github.com/vmware-tanzu/velero

// Do not pin patch version here. Leave patch at X.Y.0
// Use env GOTOOLCHAIN=auto to use newer of go cli used to build Velero.
// or GOTOOLCHAIN=goX.Y.Z to use a specific toolchain version
// See: https://go.dev/doc/toolchain#select and https://github.com/vmware-tanzu/velero/issues/8397
go 1.22.0

require (
Expand Down
19 changes: 18 additions & 1 deletion pkg/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ limitations under the License.
// worrying about introducing circular dependencies.
package buildinfo

import "fmt"
import (
"fmt"
"runtime/debug"
)

var (
// Version is the current version of Velero, set by the go linker's -X flag at build time.
Version string

// goVersion is the version of Go that was used to build Velero
goBuildInfo *debug.BuildInfo

// GitSHA is the actual commit that is being built, set by the go linker's -X flag at build time.
GitSHA string

Expand All @@ -44,3 +50,14 @@ func FormattedGitSHA() string {
}
return GitSHA
}

func GoVersion() string {
if goBuildInfo == nil {
var ok bool
goBuildInfo, ok = debug.ReadBuildInfo()
if !ok {
return "cannot read Go BuildInfo"
}
}
return goBuildInfo.GoVersion
}
1 change: 1 addition & 0 deletions pkg/cmd/cli/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func printVersion(w io.Writer, clientOnly bool, kbClient kbclient.Client, server
fmt.Fprintln(w, "Client:")
fmt.Fprintf(w, "\tVersion: %s\n", buildinfo.Version)
fmt.Fprintf(w, "\tGit commit: %s\n", buildinfo.FormattedGitSHA())
fmt.Fprintf(w, "\tGo version: %s\n", buildinfo.GoVersion())

if clientOnly {
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func NewCommand(f client.Factory) *cobra.Command {

logger.Infof("setting log-level to %s", strings.ToUpper(logLevel.String()))

logger.Infof("Starting Velero server %s (%s)", buildinfo.Version, buildinfo.FormattedGitSHA())
logger.Infof("Starting Velero server %s (%s) go-version: %s", buildinfo.Version, buildinfo.FormattedGitSHA(), buildinfo.GoVersion())
if len(features.All()) > 0 {
logger.Infof("%d feature flags enabled %s", len(features.All()), features.All())
} else {
Expand Down

0 comments on commit a217d53

Please sign in to comment.