From 35f9f85b5defabf01b1cb41d6da8d19063b868ba Mon Sep 17 00:00:00 2001 From: Tiger Kaovilai Date: Tue, 12 Nov 2024 16:00:13 -0500 Subject: [PATCH] Add go buildinfo to velero to assist with #8397 Signed-off-by: Tiger Kaovilai --- changelogs/unreleased/8398-kaovilai | 1 + go.mod | 5 +++++ pkg/buildinfo/buildinfo.go | 19 ++++++++++++++++++- pkg/cmd/cli/version/version.go | 1 + pkg/cmd/cli/version/version_test.go | 2 +- pkg/cmd/server/server.go | 2 +- 6 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/8398-kaovilai diff --git a/changelogs/unreleased/8398-kaovilai b/changelogs/unreleased/8398-kaovilai new file mode 100644 index 0000000000..ff67ec2a03 --- /dev/null +++ b/changelogs/unreleased/8398-kaovilai @@ -0,0 +1 @@ +Add go buildinfo to velero to identify toolchain used to build velero diff --git a/go.mod b/go.mod index a72b539c59..4253037f1d 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,10 @@ module github.com/vmware-tanzu/velero +// Do not pin patch version here. Leave patch at X.Y.0 +// Unset GOTOOLCHAIN to assume GOTOOLCHAIN=local where go cli version in path is used. +// Use env GOTOOLCHAIN=auto to allow go to decide whichever is newer from go.mod or cli in path. +// 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 ( diff --git a/pkg/buildinfo/buildinfo.go b/pkg/buildinfo/buildinfo.go index b0533d0dbf..acbe0e75df 100644 --- a/pkg/buildinfo/buildinfo.go +++ b/pkg/buildinfo/buildinfo.go @@ -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 @@ -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 +} diff --git a/pkg/cmd/cli/version/version.go b/pkg/cmd/cli/version/version.go index f8d2611161..a208bcc1d9 100644 --- a/pkg/cmd/cli/version/version.go +++ b/pkg/cmd/cli/version/version.go @@ -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 diff --git a/pkg/cmd/cli/version/version_test.go b/pkg/cmd/cli/version/version_test.go index 355626802f..939102e235 100644 --- a/pkg/cmd/cli/version/version_test.go +++ b/pkg/cmd/cli/version/version_test.go @@ -49,7 +49,7 @@ func TestPrintVersion(t *testing.T) { buildinfo.GitSHA = "somegitsha" buildinfo.GitTreeState = "dirty" - clientVersion := fmt.Sprintf("Client:\n\tVersion: %s\n\tGit commit: %s\n", buildinfo.Version, buildinfo.FormattedGitSHA()) + clientVersion := fmt.Sprintf("Client:\n\tVersion: %s\n\tGit commit: %s\n\tGo version: %s\n", buildinfo.Version, buildinfo.FormattedGitSHA(), buildinfo.GoVersion()) tests := []struct { name string diff --git a/pkg/cmd/server/server.go b/pkg/cmd/server/server.go index 776502f093..33fa0b3e78 100644 --- a/pkg/cmd/server/server.go +++ b/pkg/cmd/server/server.go @@ -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 {