Skip to content

Conversation

lzap
Copy link
Contributor

@lzap lzap commented Sep 26, 2025

Because I have modified my PATH to include a custom osbuild binary, it
is useful to see which osbuild version is being used by image-builder.
After this change, the output looks like this:

$ go run ./cmd/image-builder --version
image-builder:
  version: unknown
  commit: unknown
  dependencies:
    images: v0.197.0
    osbuild: osbuild 162

A very tiny refactoring to improve readability of the function.

@lzap lzap requested a review from a team as a code owner September 26, 2025 14:08
@lzap lzap requested review from mvo5, achilleas-k and thozza and removed request for a team September 26, 2025 14:08
Copy link
Collaborator

@mvo5 mvo5 left a comment

Choose a reason for hiding this comment

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

I like it! One small questin inline and I wonder if we could add some smoke test to test_container_version_smoke for this? (just a single line)

}

func readVersionFromBinary() *versionDescription {
var osbuildCmd = "osbuild"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Silly question, why is this a var? Given that its not mocked or anything we could as well just hardcode it in line 56?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I was planning to make it a constant. But I am going to delete this as @thozza had a better idea.

thozza
thozza previously requested changes Sep 29, 2025
Copy link
Member

@thozza thozza left a comment

Choose a reason for hiding this comment

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

Except for my inline comment, this LGTM.

@lzap lzap force-pushed the version-osbuild1 branch 2 times, most recently from 9f9870a to 36362cb Compare September 30, 2025 13:21
@lzap
Copy link
Contributor Author

lzap commented Sep 30, 2025

Updated, the output is a bit inconsistent now:

$ go run ./cmd/image-builder --version
image-builder:
  version: unknown
  commit: unknown
  dependencies:
    images: v0.197.0
    osbuild: "158"

Images having the v prefix, osbuild does not have it which also lead to quotes by the YAML marshaler. Therefore I prefixed it also with v so it is nicer:

$ go run ./cmd/image-builder --version
image-builder:
  version: unknown
  commit: unknown
  dependencies:
    images: v0.197.0
    osbuild: v158

commit := "unknown"
images := "unknown"
vd.ImageBuilder.Commit = "unknown"
vd.ImageBuilder.Version = "unknown"
Copy link
Member

@supakeen supakeen Sep 30, 2025

Choose a reason for hiding this comment

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

It seems like you're never actually setting this value to the global version later on anymore (previously on line 48); this seems to mean that the version will always be unknown and can't be set by command-line argument anymore (as done by rpm/makefile).

vd.ImageBuilder.Dependencies.Images = images
osbuildVersion, err := osbuild.OSBuildVersion()
if err != nil {
vd.ImageBuilder.Dependencies.OSBuild = fmt.Sprintf("unknown (%v)", err)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we want the error in here? It's never useful to the user (though useful to the developer, I guess) but will make ingesting this programmatically slightly harder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Parsing is a good point, I guess I will just print a warning to stderr.

if err != nil {
vd.ImageBuilder.Dependencies.OSBuild = fmt.Sprintf("unknown (%v)", err)
}
vd.ImageBuilder.Dependencies.OSBuild = fmt.Sprintf("v%s", osbuildVersion)
Copy link
Member

Choose a reason for hiding this comment

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

I mean I guess we can prefix with v but that's not what the actual osbuild version is: https://github.com/osbuild/osbuild/releases don't really care enough to block on it but it seems a bit unnecessary.

Copy link
Member

Choose a reason for hiding this comment

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

IMHO, we should report the version as reported by the tool

@thozza thozza dismissed their stale review October 1, 2025 09:19

My requested changes were addressed.

lzap added 2 commits October 1, 2025 11:34
This commit refactors the readVersionInfo function to improve
code readability and maintainability.
@lzap lzap force-pushed the version-osbuild1 branch from 36362cb to 62acb9e Compare October 1, 2025 09:34
@lzap
Copy link
Contributor Author

lzap commented Oct 1, 2025

Thanks for the review, updated all three places:

lzap@dev:~/image-builder-cli$ go run ./cmd/image-builder --version
image-builder:
  version: unknown
  commit: unknown
  dependencies:
    images: v0.197.0
    osbuild: "158"

lzap@dev:~/image-builder-cli$ go build -ldflags="-X main.version=42" ./cmd/image-builder/

lzap@dev:~/image-builder-cli$ ./image-builder --version
image-builder:
  version: "42"
  commit: 36362cb6e931b5fdcdfabf90de7446c8421dbf20
  dependencies:
    images: v0.197.0
    osbuild: "158"

Copy link
Member

@supakeen supakeen left a comment

Choose a reason for hiding this comment

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

Tests are failing and since prettyVersion is always called in main the unit tests are always spamming the output for osbuild and it messes with the mock call args for the fakeOsbuildCmd since it's now the second call, not the first. You should update the mock command to include support for --version, but:

This is actually a bit of a thing, it would be really nice if we could not call osbuild on start up every time; it adds a bunch of overhead. Can we get around that?

I'd also appreciate if the test_container_version_smoke test was updated with the new key.

lzap added 3 commits October 2, 2025 09:09
Because I have modified my PATH to include a custom osbuild binary, it
is useful to see which osbuild version is being used by image-builder.
After this change, the output looks like this:

$ go run ./cmd/image-builder --version
image-builder:
  version: unknown
  commit: unknown
  dependencies:
    images: v0.197.0
    osbuild: v158
@lzap lzap force-pushed the version-osbuild1 branch from 62acb9e to c982434 Compare October 2, 2025 07:09
@lzap
Copy link
Contributor Author

lzap commented Oct 2, 2025

Thanks again for the analysis, unfortunately, cobra does not support lazy initialization and out of the several workarounds this one seems the nicest to me. The main command shows help as the default action (as it does nothing), so I deleted the version string (which was causing the problem) and check for it in the Run function. This ensures the function is only called once when the flag is present.

Added the test case, I missed this one when I was searching for the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants