docker / docker/buildx

Proposal: platform access in `imagetools inspect`

Open
#1,527 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/imagetools
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 the linux/amd64 platform, in the same form as a single platform
  • platforms
    • {{ range platform, data := platforms }} iterates over all platforms, where platform takes the value of the platform string, and data takes 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 .Name and .Manifest the same as today's behavior
  • We could ensure that .Image always returns the current platform's image data (and never maps)
  • We could ensure that .Provenance/.SBOM always 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.