Skip to content

Commit a2f3c6a

Browse files
committed
imagetools: metadata-file flag
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 0a62a9e commit a2f3c6a

File tree

3 files changed

+94
-15
lines changed

3 files changed

+94
-15
lines changed

commands/imagetools/create.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"os"
8+
"sort"
89
"strings"
910

1011
"github.com/containerd/containerd/v2/core/remotes"
@@ -16,7 +17,9 @@ import (
1617
"github.com/docker/buildx/util/imagetools"
1718
"github.com/docker/buildx/util/progress"
1819
"github.com/docker/cli/cli/command"
20+
"github.com/moby/buildkit/exporter/containerimage/exptypes"
1921
"github.com/moby/buildkit/util/progress/progressui"
22+
"github.com/moby/sys/atomicwriter"
2023
"github.com/opencontainers/go-digest"
2124
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
2225
"github.com/pkg/errors"
@@ -34,6 +37,7 @@ type createOptions struct {
3437
progress string
3538
preferIndex bool
3639
platforms []string
40+
metadataFile string
3741
}
3842

3943
func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, args []string) error {
@@ -76,11 +80,16 @@ func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, arg
7680
}
7781

7882
repos := map[string]struct{}{}
79-
8083
for _, t := range tags {
8184
repos[t.Name()] = struct{}{}
8285
}
8386

87+
repoNames := make([]string, 0, len(repos))
88+
for repo := range repos {
89+
repoNames = append(repoNames, repo)
90+
}
91+
sort.Strings(repoNames)
92+
8493
sourceRefs := false
8594
for _, s := range srcs {
8695
if s.Ref != nil {
@@ -241,6 +250,15 @@ func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, arg
241250
err = err1
242251
}
243252

253+
if err == nil && len(in.metadataFile) > 0 {
254+
if err := writeMetadataFile(in.metadataFile, map[string]any{
255+
exptypes.ExporterImageDescriptorKey: desc,
256+
exptypes.ExporterImageNameKey: strings.Join(repoNames, ","),
257+
}); err != nil {
258+
return err
259+
}
260+
}
261+
244262
return err
245263
}
246264

@@ -348,6 +366,7 @@ func createCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
348366
flags.StringArrayVarP(&options.annotations, "annotation", "", []string{}, "Add annotation to the image")
349367
flags.BoolVar(&options.preferIndex, "prefer-index", true, "When only a single source is specified, prefer outputting an image index or manifest list instead of performing a carbon copy")
350368
flags.StringArrayVarP(&options.platforms, "platform", "p", []string{}, "Filter specified platforms of target image")
369+
flags.StringVar(&options.metadataFile, "metadata-file", "", "Write create result metadata to a file")
351370

352371
return cmd
353372
}
@@ -367,3 +386,11 @@ func mergeDesc(d1, d2 ocispecs.Descriptor) (ocispecs.Descriptor, error) {
367386
}
368387
return d1, nil
369388
}
389+
390+
func writeMetadataFile(filename string, dt any) error {
391+
b, err := json.MarshalIndent(dt, "", " ")
392+
if err != nil {
393+
return err
394+
}
395+
return atomicwriter.WriteFile(filename, b, 0o644)
396+
}

docs/reference/buildx_imagetools_create.md

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ Create a new image based on source images
99

1010
### Options
1111

12-
| Name | Type | Default | Description |
13-
|:---------------------------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------------------------|
14-
| [`--annotation`](#annotation) | `stringArray` | | Add annotation to the image |
15-
| [`--append`](#append) | `bool` | | Append to existing manifest |
16-
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
17-
| `-D`, `--debug` | `bool` | | Enable debug logging |
18-
| [`--dry-run`](#dry-run) | `bool` | | Show final image instead of pushing |
19-
| [`-f`](#file), [`--file`](#file) | `stringArray` | | Read source descriptor from file |
20-
| `-p`, `--platform` | `stringArray` | | Filter specified platforms of target image |
21-
| `--prefer-index` | `bool` | `true` | When only a single source is specified, prefer outputting an image index or manifest list instead of performing a carbon copy |
22-
| `--progress` | `string` | `auto` | Set type of progress output (`auto`, `none`, `plain`, `rawjson`, `tty`). Use plain to show container output |
23-
| [`-t`](#tag), [`--tag`](#tag) | `stringArray` | | Set reference for new image |
12+
| Name | Type | Default | Description |
13+
|:------------------------------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------------------------|
14+
| [`--annotation`](#annotation) | `stringArray` | | Add annotation to the image |
15+
| [`--append`](#append) | `bool` | | Append to existing manifest |
16+
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
17+
| `-D`, `--debug` | `bool` | | Enable debug logging |
18+
| [`--dry-run`](#dry-run) | `bool` | | Show final image instead of pushing |
19+
| [`-f`](#file), [`--file`](#file) | `stringArray` | | Read source descriptor from file |
20+
| [`--metadata-file`](#metadata-file) | `string` | | Write create result metadata to a file |
21+
| `-p`, `--platform` | `stringArray` | | Filter specified platforms of target image |
22+
| `--prefer-index` | `bool` | `true` | When only a single source is specified, prefer outputting an image index or manifest list instead of performing a carbon copy |
23+
| `--progress` | `string` | `auto` | Set type of progress output (`auto`, `none`, `plain`, `rawjson`, `tty`). Use plain to show container output |
24+
| [`-t`](#tag), [`--tag`](#tag) | `stringArray` | | Set reference for new image |
2425

2526

2627
<!---MARKER_GEN_END-->
@@ -100,6 +101,28 @@ The descriptor in the file is merged with existing descriptor in the registry if
100101

101102
The supported fields for the descriptor are defined in [OCI spec](https://github.com/opencontainers/image-spec/blob/master/descriptor.md#properties) .
102103

104+
### <a name="metadata-file"></a> Write create result metadata to a file (--metadata-file)
105+
106+
To output metadata such as the image digest, pass the `--metadata-file` flag.
107+
The metadata will be written as a JSON object to the specified file. The
108+
directory of the specified file must already exist and be writable.
109+
110+
```console
111+
$ docker buildx imagetools create -t user/app:latest -f image1 -f image2 --metadata-file metadata.json
112+
$ cat metadata.json
113+
```
114+
115+
```json
116+
{
117+
"containerimage.descriptor": {
118+
"mediaType": "application/vnd.oci.image.index.v1+json",
119+
"digest": "sha256:19ffeab6f8bc9293ac2c3fdf94ebe28396254c993aea0b5a542cfb02e0883fa3",
120+
"size": 4654
121+
},
122+
"image.name": "docker.io/user/app"
123+
}
124+
```
125+
103126
### <a name="tag"></a> Set reference for new image (-t, --tag)
104127

105128
```text

tests/imagetools.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package tests
22

33
import (
44
"encoding/json"
5+
"os"
56
"os/exec"
7+
"path"
8+
"path/filepath"
69
"testing"
710

811
"github.com/containerd/containerd/v2/core/images"
@@ -50,10 +53,23 @@ func testImagetoolsCopyManifest(t *testing.T, sb integration.Sandbox) {
5053
require.NoError(t, err)
5154
target2 := registry2 + "/buildx/imtools2-manifest:latest"
5255

53-
cmd = buildxCmd(sb, withArgs("imagetools", "create", "-t", target2, target))
56+
cmd = buildxCmd(sb, withArgs("imagetools", "create", "--metadata-file", path.Join(dir, "md.json"), "-t", target2, target))
5457
dt, err = cmd.CombinedOutput()
5558
require.NoError(t, err, string(dt))
5659

60+
mddt, err := os.ReadFile(filepath.Join(dir, "md.json"))
61+
require.NoError(t, err)
62+
63+
type mdT struct {
64+
ImageDescriptor ocispecs.Descriptor `json:"containerimage.descriptor"`
65+
ImageName string `json:"image.name"`
66+
}
67+
var md mdT
68+
err = json.Unmarshal(mddt, &md)
69+
require.NoError(t, err)
70+
require.NotEmpty(t, md.ImageDescriptor)
71+
require.Equal(t, registry2+"/buildx/imtools2-manifest", md.ImageName)
72+
5773
cmd = buildxCmd(sb, withArgs("imagetools", "inspect", target2, "--raw"))
5874
dt, err = cmd.CombinedOutput()
5975
require.NoError(t, err, string(dt))
@@ -123,10 +139,23 @@ func testImagetoolsCopyIndex(t *testing.T, sb integration.Sandbox) {
123139
require.NoError(t, err)
124140
target2 := registry2 + "/buildx/imtools2:latest"
125141

126-
cmd = buildxCmd(sb, withArgs("imagetools", "create", "-t", target2, target))
142+
cmd = buildxCmd(sb, withArgs("imagetools", "create", "--metadata-file", path.Join(dir, "md.json"), "-t", target2, target))
127143
dt, err = cmd.CombinedOutput()
128144
require.NoError(t, err, string(dt))
129145

146+
mddt, err := os.ReadFile(filepath.Join(dir, "md.json"))
147+
require.NoError(t, err)
148+
149+
type mdT struct {
150+
ImageDescriptor ocispecs.Descriptor `json:"containerimage.descriptor"`
151+
ImageName string `json:"image.name"`
152+
}
153+
var md mdT
154+
err = json.Unmarshal(mddt, &md)
155+
require.NoError(t, err)
156+
require.NotEmpty(t, md.ImageDescriptor)
157+
require.Equal(t, registry2+"/buildx/imtools2", md.ImageName)
158+
130159
cmd = buildxCmd(sb, withArgs("imagetools", "inspect", target2, "--raw"))
131160
dt, err = cmd.CombinedOutput()
132161
require.NoError(t, err, string(dt))

0 commit comments

Comments
 (0)