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

Conversation

migueleliasweb
Copy link
Contributor

Now that kubebuilder is go-installable 🎉, this is a small improvement to fetch more information when dealing with go install installations.

Currently, the output from kubebuilder version looks like this:

$ go install sigs.k8s.io/kubebuilder/v4@7ee23df2b97ccb209df4e05c78830266c7b8d8bb
Version: cmd.version{KubeBuilderVersion:"v4.5.1-0.20250121092837-7ee23df2b97c", KubernetesVendor:"unknown", GitCommit:"$Format:%H$", BuildDate:"1970-01-01T00:00:00Z", GoOs:"unknown", GoArch:"unknown"}

With this PR, we will now infer correctly GitCommit, BuildDate, GoOs and GoArch for normal GoReleaser builds but also go install installations.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: migueleliasweb
Once this PR has been reviewed and has the lgtm label, please assign kavinjsir 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 the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jan 21, 2025
@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 21, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @migueleliasweb. 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.

@@ -27,12 +30,11 @@ const unknown = "unknown"
// 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.

}

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.

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.

// 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
		}
	}

@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 Jan 23, 2025
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/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants