Skip to content

Commit

Permalink
Add go buildinfo to velero to assist with #8397
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Nov 12, 2024
1 parent 8e23752 commit 35f9f85
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/8398-kaovilai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add go buildinfo to velero to identify toolchain used to build velero
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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 (
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"
}

Check warning on line 60 in pkg/buildinfo/buildinfo.go

View check run for this annotation

Codecov / codecov/patch

pkg/buildinfo/buildinfo.go#L54-L60

Added lines #L54 - L60 were not covered by tests
}
return goBuildInfo.GoVersion

Check warning on line 62 in pkg/buildinfo/buildinfo.go

View check run for this annotation

Codecov / codecov/patch

pkg/buildinfo/buildinfo.go#L62

Added line #L62 was not covered by tests
}
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/cli/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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())

Check warning on line 112 in pkg/cmd/server/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/server/server.go#L112

Added line #L112 was not covered by tests
if len(features.All()) > 0 {
logger.Infof("%d feature flags enabled %s", len(features.All()), features.All())
} else {
Expand Down

0 comments on commit 35f9f85

Please sign in to comment.