Merge pull request #972 from crazy-max/imagetools-inspect-order

imagetools inspect: keep platform order
pull/991/head
Tõnis Tiigi 3 years ago committed by GitHub
commit 1416bc1d83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -178,6 +178,8 @@ $ docker buildx imagetools inspect moby/buildkit:master --format "{{json .Manife
{ {
"schemaVersion": 2, "schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json", "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
"digest": "sha256:79d97f205e2799d99a3a8ae2a1ef17acb331e11784262c3faada847dc6972c52",
"size": 2010,
"manifests": [ "manifests": [
{ {
"mediaType": "application/vnd.docker.distribution.manifest.v2+json", "mediaType": "application/vnd.docker.distribution.manifest.v2+json",

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"sort"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"text/template" "text/template"
@ -15,8 +16,10 @@ import (
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
binfotypes "github.com/moby/buildkit/util/buildinfo/types" binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1" ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors" "github.com/pkg/errors"
"golang.org/x/sync/errgroup"
) )
const defaultPfx = " " const defaultPfx = " "
@ -109,18 +112,28 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
imageconfigs := make(map[string]*ocispecs.Image) imageconfigs := make(map[string]*ocispecs.Image)
buildinfos := make(map[string]*binfotypes.BuildInfo) buildinfos := make(map[string]*binfotypes.BuildInfo)
for _, pform := range p.platforms {
img, dtic, err := p.getImageConfig(&pform) 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 { if err != nil {
return err return err
} else if img != nil { } else if img != nil {
imageconfigs[platforms.Format(pform)] = img imageconfigs[platforms.Format(platform)] = img
} }
if bi, err := p.getBuildInfo(dtic); err != nil { if bi, err := p.getBuildInfo(dtic); err != nil {
return err return err
} else if bi != nil { } else if bi != nil {
buildinfos[platforms.Format(pform)] = bi buildinfos[platforms.Format(platform)] = bi
}
return nil
})
}(platform)
} }
if err := eg.Wait(); err != nil {
return err
} }
format := tpl.Root.String() format := tpl.Root.String()
@ -130,7 +143,21 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
case images.MediaTypeDockerSchema2Manifest, ocispecs.MediaTypeImageManifest: case images.MediaTypeDockerSchema2Manifest, ocispecs.MediaTypeImageManifest:
manifest = p.manifest manifest = p.manifest
case images.MediaTypeDockerSchema2ManifestList, ocispecs.MediaTypeImageIndex: 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 { switch {
@ -164,13 +191,13 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
BuildInfo: buildinfos, BuildInfo: buildinfos,
}) })
} }
var imageconfig *ocispecs.Image var ic *ocispecs.Image
for _, ic := range imageconfigs { for _, v := range imageconfigs {
imageconfig = ic ic = v
} }
var buildinfo *binfotypes.BuildInfo var bi *binfotypes.BuildInfo
for _, bi := range buildinfos { for _, v := range buildinfos {
buildinfo = bi bi = v
} }
return tpl.Execute(out, struct { return tpl.Execute(out, struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
@ -180,8 +207,8 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
}{ }{
Name: p.name, Name: p.name,
Manifest: manifest, Manifest: manifest,
Image: imageconfig, Image: ic,
BuildInfo: buildinfo, BuildInfo: bi,
}) })
} }
@ -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 { 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 { for _, bi := range bis {
return p.printBuildInfo(bi, "", out) 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) 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() _ = w.Flush()
if err := p.printBuildInfo(bi, "", out); err != nil { if err := p.printBuildInfo(bi, "", out); err != nil {
return err return err

Loading…
Cancel
Save