Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Infer version information for go-install #4516

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @migueleliasweb and @dmvolod

Could we ensure that this one is rebased with master?
Also, is this one good to fly or what would be missing?
Is possible ensure that we have in the description an example of test executed for we ensure that all is fine and make easier the review/validation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, @camilamacedo86 I can't help with rebase this PR as can't owns it.
Just fixed suggesting and it can be applied who can owns repo or PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry all, I've got stuck on a crazy hectic week and a bit at work. Once this is off by back, I'll come back to this if that's okay. 🙏 I didn't forget this PR!

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

Expand All @@ -27,38 +28,69 @@ const unknown = "unknown"
// information in the release process
var (
kubeBuilderVersion = unknown
kubernetesVendorVersion = unknown
goos = unknown
goarch = unknown
gitCommit = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
kubernetesVendorVersion = "1.32.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to share, if you want to work on this one.

We can do that as a follow up, but we need a target to be called when we run amke generate that will update the k8s version here and in the goreleaser based on the k8s/api (like https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4/Makefile#L179) so, that we can ensure that it is in sync always.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: #4588

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought of that too while I was working on this file. I'll give it go.

Copy link
Contributor Author

@migueleliasweb migueleliasweb Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops, it has been assigned already. All good.

goVersion = unknown
goOs = unknown
goArch = unknown
gitCommit = unknown // "$Format:%H$" sha1 from git, output of $(git rev-parse HEAD)

buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
// "1970-01-01T00:00:00Z" build date in ISO8601 format
// Output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
buildDate = unknown
)

// version contains all the information related to the CLI version
type version struct {
KubeBuilderVersion string `json:"kubeBuilderVersion"`
KubernetesVendor string `json:"kubernetesVendor"`
GitCommit string `json:"gitCommit"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
GoOs string `json:"goOs"`
GoArch string `json:"goArch"`
GitCommit string `json:"gitCommit"`
BuildDate string `json:"buildDate"`
}

// versionString returns the CLI version
func versionString() string {
if kubeBuilderVersion == unknown {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
kubeBuilderVersion = info.Main.Version
if goVersion == unknown {
goVersion = runtime.Version()
}

if goOs == unknown {
goOs = runtime.GOOS
}

if goArch == unknown {
goArch = runtime.GOARCH
}

info, ok := debug.ReadBuildInfo()

if ok {
if info.Main.Version != "" {
if kubeBuilderVersion == unknown {
kubeBuilderVersion = info.Main.Version
}
}

for _, setting := range info.Settings {
if buildDate == unknown && setting.Key == "vcs.revision" {
Copy link
Member

@dmvolod dmvolod Mar 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if buildDate == unknown && setting.Key == "vcs.revision" {
if buildDate == unknown && setting.Key == "vcs.time" {

This should be a vcs.time getting buildDate, not vcs.revision

Also would be nice to have a minimal test for this case, as I described before mocking debug.ReadBuildInfo()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/hold until @dmvolod give LGTM
and the questions be answered

buildDate = setting.Value
}

if gitCommit == unknown && setting.Key == "vcs.revision" {
gitCommit = setting.Value
}
}
}

return fmt.Sprintf("Version: %#v", version{
kubeBuilderVersion,
kubernetesVendorVersion,
goVersion,
goOs,
goArch,
gitCommit,
buildDate,
goos,
goarch,
})
}
Loading