Skip to content

✨ 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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Thedarkmatter10
Copy link

🔧 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:

    • VersionInfo struct (was private version)
  • 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() to getKubeBuilderVersion() .

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

Copy link

linux-foundation-easycla bot commented Jul 1, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Jul 1, 2025
@k8s-ci-robot
Copy link
Contributor

Welcome @Thedarkmatter10!

It looks like this is your first PR to kubernetes-sigs/kubebuilder 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kubebuilder has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 1, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Thedarkmatter10
Once this PR has been reviewed and has the lgtm label, please assign camilamacedo86 for approval. For more information see the Code Review Process.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 1, 2025
@Thedarkmatter10
Copy link
Author

@camilamacedo86 @Kavinjsir

// versionJSON returns version as JSON string
func versionJSON() string {
v := getVersionInfo()
b, _ := json.MarshalIndent(v, "", " ")
Copy link
Member

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?

Copy link
Author

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

Copy link
Member

@camilamacedo86 camilamacedo86 Jul 2, 2025

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

Copy link
Member

@camilamacedo86 camilamacedo86 Jul 2, 2025

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.

Copy link
Member

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.

Copy link
Author

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! 😊

Copy link
Member

@camilamacedo86 camilamacedo86 Jul 4, 2025

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

Copy link
Author

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 😀.

Copy link
Member

@camilamacedo86 camilamacedo86 Jul 19, 2025

Choose a reason for hiding this comment

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

Copy link
Author

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! 😊

@Thedarkmatter10 Thedarkmatter10 changed the title (✨)refactor: remove duplication, add runtime fallbacks and unit tests. 🌱 refactor: remove duplication, add runtime fallbacks and unit tests. Jul 1, 2025
@Thedarkmatter10 Thedarkmatter10 force-pushed the version/refactor-add-tests branch from 3a825c1 to 0610694 Compare July 1, 2025 19:04
@camilamacedo86
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 2, 2025
@Thedarkmatter10 Thedarkmatter10 force-pushed the version/refactor-add-tests branch from 0610694 to 63173f7 Compare July 3, 2025 10:26
@camilamacedo86
Copy link
Member

camilamacedo86 commented Jul 3, 2025

Hi @Thedarkmatter10

See that the testdata job will be fixed with: #4905
Also, we must to have just one commit before merge out, could you please squash it?

By last since it will produce a change for end users it should end up in our release notes
The title of PRs is our release notes ,and so, we cannot use 🌱

We need to use like ✨ and add a text that clarifies what was done such as :

Improve output of version command to...

Could you please update those things?
Thank you a lot for your contribution 🥇

@Thedarkmatter10 Thedarkmatter10 changed the title 🌱 refactor: remove duplication, add runtime fallbacks and unit tests. ✨ Improve version command output: add runtime fallbacks and unit tests. Jul 3, 2025
@Thedarkmatter10 Thedarkmatter10 force-pushed the version/refactor-add-tests branch from 63173f7 to df6f0c9 Compare July 3, 2025 13:09
@Thedarkmatter10 Thedarkmatter10 force-pushed the version/refactor-add-tests branch from df6f0c9 to 5649294 Compare July 3, 2025 13:21
@Thedarkmatter10
Copy link
Author

Hi @Thedarkmatter10

See that the testdata job will be fixed with: #4905 Also, we must to have just one commit before merge out, could you please squash it?

By last since it will produce a change for end users it should end up in our release notes The title of PRs is our release notes ,and so, we cannot use 🌱

We need to use like ✨ and add a text that clarifies what was done such as :

Improve output of version command to...

Could you please update those things? Thank you a lot for your contribution 🥇

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.
and also i renamed title of PR.

Thanks again for your guidance and patience! 😊

@camilamacedo86
Copy link
Member

/test pull-kubebuilder-e2e-k8s-1-32-0

Comment on lines +79 to +94
// 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,
)
}
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants