-
Notifications
You must be signed in to change notification settings - Fork 1.6k
✨ Improve version command output: add runtime fallbacks and unit tests. #4898
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
base: master
Are you sure you want to change the base?
✨ Improve version command output: add runtime fallbacks and unit tests. #4898
Conversation
Welcome @Thedarkmatter10! |
Hi @Thedarkmatter10. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Thedarkmatter10 The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
// versionJSON returns version as JSON string | ||
func versionJSON() string { | ||
v := getVersionInfo() | ||
b, _ := json.MarshalIndent(v, "", " ") |
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.
Will it change how we retrieve and store the data in the PROJECT file?
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.
Thanks for the question! 🙌
No, this change does not affect how we retrieve or store data in the PROJECT file.
The versionJSON() and related functions strictly rely on:
-
Build-time ldflags (e.g., gitCommit, buildDate)
-
runtime.GOOS / GOARCH
-
debug.ReadBuildInfo() (from Go module metadata)
There's no read or write interaction with the PROJECT file — this change only impacts version metadata exposed at runtime via the CLI.
@camilamacedo86
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 we use this information to add the version in https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4/PROJECT#L5
So, we might need to test out to TDB:
We need to build the bin locally with goreleaser and then scaffold a new project with.
Like done here: #4760 (comment) to ensure that we will still be outputting only the version there.
https://github.com/kubernetes-sigs/kubebuilder/actions/runs/16007968823/job/45252541919?pr=4898
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.
Also, see: https://github.com/kubernetes-sigs/kubebuilder/actions/runs/16007968823/job/45252541919?pr=4898. You need to rebase with the master branch.
The testdata ci job will re-generate all samples under testdata and docs.
It is failing because it is not rebased.
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.
Indeed we need a CI test for that, so if that passes, we are OK with it, and we do not need to remember what exactly can impact it anymore.
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.
Thank you for the explanation and the reference to the release.yml workflow!
I’d be happy to help draft a CI enhancement that:
-
Simulates the release by building the CLI (without publishing).
-
Runs kubebuilder init with the built binary.
-
Validates that the cliVersion appears in the scaffolded PROJECT file and follows the expected format.
Let me know if you'd like me to proceed — I can open a follow-up PR for that! 😊
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.
(because of the scenario explained above and to ensure that it will not impact the alpha update command, which is WIP)
I will need to find some time to properly review these changes and let you know more about
So, it might take a while, but I will get back to you
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.
Thanks a lot , i will wait for your confirmation and the next step 😀.
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.
See: https://github.com/kubernetes-sigs/kubebuilder/pull/4939/files
For we add the tests
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.
Thanks for setting this up, Camila! 🙌
This CI workflow perfectly validates what I had in mind ensuring that changes like the one I submitted don't unintentionally modify the cliVersion written to the PROJECT file.
Great to see this automated check in place now really appreciate you putting it together! 😊
3a825c1
to
0610694
Compare
/ok-to-test |
0610694
to
63173f7
Compare
See that the testdata job will be fixed with: #4905 By last since it will produce a change for end users it should end up in our release notes We need to use like ✨ and add a text that clarifies what was done such as :
Could you please update those things? |
63173f7
to
df6f0c9
Compare
df6f0c9
to
5649294
Compare
Hi @camilamacedo86 👋 I tried squashing the commits into one as suggested, but I ran into some issues with rebase and merge conflicts that I wasn't able to resolve cleanly. As a result, the PR still shows extra commits. Apologies for the inconvenience caused 🙏 . I understand the importance of keeping the commit history clean and will make sure not to repeat this in future contributions. Please let me know if you'd prefer that I close this PR and open a fresh one with a clean history, or if there's another way you'd like to proceed. Thanks again for your guidance and patience! 😊 |
/test pull-kubebuilder-e2e-k8s-1-32-0 |
// versionString returns a human-friendly string version | ||
func versionString() string { | ||
v := getVersionInfo() | ||
return fmt.Sprintf(`KubeBuilder Version: %s | ||
Kubernetes Vendor: %s | ||
Git Commit: %s | ||
Build Date: %s | ||
Go OS/Arch: %s/%s`, | ||
v.KubeBuilderVersion, | ||
v.KubernetesVendor, | ||
v.GitCommit, | ||
v.BuildDate, | ||
v.GoOS, | ||
v.GoArch, | ||
) | ||
} |
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.
@camilamacedo86 @Thedarkmatter10 could we move the output formatting to cli/version.go
?
I think that separating the data gathering logic from the presentation logic could give us more flexibility to deal with output formatting, add flags, and leave room for other tools/commands to define how they want to display the version. The same goes for the JSON output. We could turn that into a flag in cli/version.go
.
This separation was discussed briefly in a previous PR conversation (before I messed up and closed the PR).
WDYT?
🔧 Refactor version logic: expose functions, add fallbacks, remove duplication, and add tests
Summary
This PR improves the internal versioning logic by making it more maintainable, testable, and robust.
✨ What’s Changed
Publicly exposed version helpers:
Removed duplication (DRY):
Centralized resolution logic using debug.ReadBuildInfo() into a helper
Eliminated repeated fallback code for resolving build info
Added runtime fallbacks:
Uses runtime.GOOS and runtime.GOARCH when not set via ldflags
Ensures gitCommit is validated and not left as$Format:%H$
Improved output formatting:
versionString() provides clean, human-readable output
versionJSON() supports structured, machine-readable output (useful in CI)
Renamed function Name from
getKubebuilderVersion()
togetKubeBuilderVersion()
.Added unit tests (version_test.go):
Validate output includes expected fields
Confirm versionJSON() is parsable and includes required keys
Ensure GetVersionInfo() returns non-empty, resolved values
🧠 Why It Matters
Ensures test coverage for a previously untested code path
Helps catch missing ldflags values (e.g. empty Git commit) early
Lays the foundation for long-term maintainability of CLI version reporting
📈 Impact
Exposes version info for programmatic and CLI use
Adds meaningful tests that reduce risk of silent version resolution failures
Improves developer and CI experience with cleaner output and stronger guarantees