Proposal: platform access in `imagetools inspect`
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 4.5k
- Forks
- 682
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 29
Description
Context
To get information about images, users can use docker buildx imagetools inspect. Like the rest of most of the docker cli printing tools, it supports the --format flag for specifying go templates.
Problem
The data parameter passed during parameter execution (which takes on the value of {{ . }} in the top-level template) is different between multi-platform images and single-platform images.
The Image, Provenance and SBOM fields are different, being a simple struct in a single-platform image, and a map of those simple structs in a multi-platform image.
This means that writing a single --format template that works irrespective of whether the image is single-platform (with a manifest) or multi-platform (with an index) is quite tricky.
Proposed solution
When users are analyzing images for information, usually we'd expect users to be interested in one of a few scenarios:
- Inspecting the image for the current platform
- Inspecting the image for a specified platform
- Inspecting the image for all platforms
To resolve this, we can create new functions for the template:
platform{{ platform nil }}returns the data for the current platform, in the same form as a single platform{{ platform "linux/amd64" }}returns the data for thelinux/amd64platform, in the same form as a single platform
platforms{{ range platform, data := platforms }}iterates over all platforms, whereplatformtakes the value of the platform string, anddatatakes the form as a single platform
The data struct returned from all of these functions only needs to be a simple struct, no need for maps that are sometimes present and sometimes not.
Backwards compatibility
Since this only adds new functions, and doesn't remove any functionality --format strings that previously worked won't break.
However, since imagetools is marked as experimental, we could consider breaking some behavior:
- We could keep
.Nameand.Manifestthe same as today's behavior - We could ensure that
.Imagealways returns the current platform's image data (and never maps) - We could ensure that
.Provenance/.SBOMalways return the current platform's provenance and sbom data (and never maps)
Effectively, this means that {{ . }} = {{ platform nil }}.
This makes the base case of "inspecting the image for the current platform" much easier -- then for iterating, or getting specified platforms, the platforms and platform functions are easily available.
cc @crazy-max @tonistiigi
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in util/imagetools/printers.go, particularly the single-platform and multi-platform data paths linked in the issue, and trace how --format templates receive their data. Review the Go template function integration needed for platform and platforms. Done means templates can inspect the current, specified, or all platforms using consistent single-platform data while preserving existing behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100