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

imagetools inspect: keep platform order #972

Merged
merged 2 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/buildx_imagetools_inspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ $ docker buildx imagetools inspect moby/buildkit:master --format "{{json .Manife
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
"digest": "sha256:79d97f205e2799d99a3a8ae2a1ef17acb331e11784262c3faada847dc6972c52",
"size": 2010,
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
Expand Down
83 changes: 59 additions & 24 deletions util/imagetools/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
"text/template"
Expand All @@ -15,8 +16,10 @@ import (
"github.com/containerd/containerd/platforms"
"github.com/docker/distribution/reference"
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

const defaultPfx = " "
Expand Down Expand Up @@ -109,18 +112,28 @@ func (p *Printer) Print(raw bool, out io.Writer) error {

imageconfigs := make(map[string]*ocispecs.Image)
buildinfos := make(map[string]*binfotypes.BuildInfo)
for _, pform := range p.platforms {
img, dtic, err := p.getImageConfig(&pform)
if err != nil {
return err
} else if img != nil {
imageconfigs[platforms.Format(pform)] = img
}
if bi, err := p.getBuildInfo(dtic); err != nil {
return err
} else if bi != nil {
buildinfos[platforms.Format(pform)] = bi
}

eg, _ := errgroup.WithContext(p.ctx)
for _, platform := range p.platforms {
func(platform ocispecs.Platform) {
eg.Go(func() error {
img, dtic, err := p.getImageConfig(&platform)
if err != nil {
return err
} else if img != nil {
imageconfigs[platforms.Format(platform)] = img
}
if bi, err := p.getBuildInfo(dtic); err != nil {
return err
} else if bi != nil {
buildinfos[platforms.Format(platform)] = bi
}
return nil
})
}(platform)
}
if err := eg.Wait(); err != nil {
return err
}

format := tpl.Root.String()
Expand All @@ -130,7 +143,21 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
case images.MediaTypeDockerSchema2Manifest, ocispecs.MediaTypeImageManifest:
manifest = p.manifest
case images.MediaTypeDockerSchema2ManifestList, ocispecs.MediaTypeImageIndex:
manifest = p.index
manifest = struct {
SchemaVersion int `json:"schemaVersion"`
MediaType string `json:"mediaType,omitempty"`
Digest digest.Digest `json:"digest"`
Size int64 `json:"size"`
Manifests []ocispecs.Descriptor `json:"manifests"`
Annotations map[string]string `json:"annotations,omitempty"`
}{
SchemaVersion: p.index.Versioned.SchemaVersion,
MediaType: p.index.MediaType,
Digest: p.manifest.Digest,
Size: p.manifest.Size,
Manifests: p.index.Manifests,
Annotations: p.index.Annotations,
}
}

switch {
Expand Down Expand Up @@ -164,13 +191,13 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
BuildInfo: buildinfos,
})
}
var imageconfig *ocispecs.Image
for _, ic := range imageconfigs {
imageconfig = ic
var ic *ocispecs.Image
for _, v := range imageconfigs {
ic = v
}
var buildinfo *binfotypes.BuildInfo
for _, bi := range buildinfos {
buildinfo = bi
var bi *binfotypes.BuildInfo
for _, v := range buildinfos {
bi = v
}
return tpl.Execute(out, struct {
Name string `json:"name,omitempty"`
Expand All @@ -180,8 +207,8 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
}{
Name: p.name,
Manifest: manifest,
Image: imageconfig,
BuildInfo: buildinfo,
Image: ic,
BuildInfo: bi,
})
}

Expand Down Expand Up @@ -230,14 +257,22 @@ func (p *Printer) printManifestList(out io.Writer) error {
}

func (p *Printer) printBuildInfos(bis map[string]*binfotypes.BuildInfo, out io.Writer) error {
if len(bis) == 1 {
if len(bis) == 0 {
return nil
} else if len(bis) == 1 {
for _, bi := range bis {
return p.printBuildInfo(bi, "", out)
}
}
for pform, bi := range bis {
var pkeys []string
for _, pform := range p.platforms {
pkeys = append(pkeys, platforms.Format(pform))
}
sort.Strings(pkeys)
for _, platform := range pkeys {
bi := bis[platform]
w := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0)
_, _ = fmt.Fprintf(w, "\t\nPlatform:\t%s\t\n", pform)
_, _ = fmt.Fprintf(w, "\t\nPlatform:\t%s\t\n", platform)
_ = w.Flush()
if err := p.printBuildInfo(bi, "", out); err != nil {
return err
Expand Down