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

✨ [Improvement] 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
58 changes: 52 additions & 6 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

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

const unknown = "unknown"
Expand All @@ -27,12 +30,11 @@
// information in the release process
var (
kubeBuilderVersion = unknown
kubernetesVendorVersion = unknown
kubernetesVendorVersion = "1.31.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took the liberty of making this the default value originally from https://github.com/kubernetes-sigs/kubebuilder/blob/master/build/.goreleaser.yml. Potentially, this value could be tracked solely here to avoid having to update 2 places every time.

If it is preferred to keep the value as unknown, let me know.

Copy link
Member

Choose a reason for hiding this comment

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

I think it is fine, but we need to bump 1.32.0

Copy link
Member

Choose a reason for hiding this comment

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

goos = unknown
goarch = unknown
gitCommit = "$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')
gitCommit = unknown // "$Format:%H$" sha1 from git, output of $(git rev-parse HEAD)
buildDate = unknown // "1970-01-01T00:00:00Z" build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')

Check failure on line 37 in cmd/version.go

View workflow job for this annotation

GitHub Actions / golangci-lint

The line is 133 characters long, which exceeds the maximum of 120 characters. (lll)
)

// version contains all the information related to the CLI version
Expand All @@ -47,10 +49,54 @@

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

if ok && info.Main.Version != "" {
Copy link
Contributor

@dmvolod dmvolod Jan 22, 2025

Choose a reason for hiding this comment

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

Since the code becomes more complex than it was before, it would be nice to be able to write tests, mocking debug.ReadBuildInfo()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll definitely be interesting creating mock for it. I'll give it a shot.

if kubeBuilderVersion == unknown {
kubeBuilderVersion = info.Main.Version
}

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

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

if gitCommit == unknown && info.Main.Version != "" {
mainVersionSplit := strings.Split(info.Main.Version, "-")
Copy link
Contributor

@dmvolod dmvolod Jan 22, 2025

Choose a reason for hiding this comment

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

@migueleliasweb wdyt about to utilize a semver parsing library who supports pre-release segments, i.e. https://github.com/Masterminds/semver instead of custom code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not against it if it's compatible. I'd have to check. Also, adding a new dependency for such a small usecase kinda feels a bit unnecessary.

I'll give it a shot.


// For released semvers like "v4.5.0"
// Result: info.Main.Version == "semver"
if len(mainVersionSplit) == 1 {
gitCommit = info.Main.Version
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be validated, but probably much easy to get commit hash (vcs.revision) and time (vcs.time) from the info.Settings like

for _, setting := range info.Settings {
		if setting.Key == "vcs.revision" {
			gitCommit = setting.Value
		}
	}

}

// For unreleased refs like "<commit-hash>"
// Result (go install): info.Main.Version == "<semver>-<build-date>-<commit-hash>" E.g "v4.5.1-0.20250121092837-7ee23df2b97c"

Check failure on line 77 in cmd/version.go

View workflow job for this annotation

GitHub Actions / golangci-lint

The line is 128 characters long, which exceeds the maximum of 120 characters. (lll)
if len(mainVersionSplit) == 3 {
gitCommit = mainVersionSplit[2]

buildDateFromVersion := func() string {
buildDatesplit := strings.Split(
mainVersionSplit[1],
".",
)

if len(buildDatesplit) == 2 {
return buildDatesplit[1]
}

return buildDatesplit[0]
}()

// format build date
if t, err := time.Parse("20060102150405", buildDateFromVersion); err == nil {
buildDate = t.Format(time.RFC3339)
}
}
}
}

return fmt.Sprintf("Version: %#v", version{
Expand Down
Loading