-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,10 @@ | |
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"runtime/debug" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const unknown = "unknown" | ||
|
@@ -27,12 +30,11 @@ | |
// information in the release process | ||
var ( | ||
kubeBuilderVersion = unknown | ||
kubernetesVendorVersion = unknown | ||
kubernetesVendorVersion = "1.31.0" | ||
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') | ||
) | ||
|
||
// version contains all the information related to the CLI version | ||
|
@@ -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 != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, "-") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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" | ||
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{ | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4517