@@ -4,26 +4,38 @@ import (
44 "context"
55 "strings"
66
7+ "github.com/containerd/log"
8+ "github.com/distribution/reference"
79 "github.com/docker/docker/builder/builder-next/exporter/overrides"
810 "github.com/moby/buildkit/exporter"
911 "github.com/moby/buildkit/exporter/containerimage/exptypes"
1012
1113 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1214)
1315
14- type ImageExportedByBuildkit = func (ctx context.Context , id string , desc ocispec.Descriptor )
16+ type BuildkitCallbacks struct {
17+ // Exported is a Called when an image is exported by buildkit.
18+ Exported func (ctx context.Context , id string , desc ocispec.Descriptor )
19+
20+ // Named is a callback that is called when an image is created in the
21+ // containerd image store by buildkit.
22+ Named func (ctx context.Context , ref reference.NamedTagged , desc ocispec.Descriptor )
23+ }
1524
1625// Wraps the containerimage exporter's Resolve method to apply moby-specific
1726// overrides to the exporter attributes.
1827type imageExporterMobyWrapper struct {
19- exp exporter.Exporter
20- callback ImageExportedByBuildkit
28+ exp exporter.Exporter
29+ callbacks BuildkitCallbacks
2130}
2231
2332// NewWrapper returns an exporter wrapper that applies moby specific attributes
2433// and hooks the export process.
25- func NewWrapper (exp exporter.Exporter , callback ImageExportedByBuildkit ) (exporter.Exporter , error ) {
26- return & imageExporterMobyWrapper {exp : exp , callback : callback }, nil
34+ func NewWrapper (exp exporter.Exporter , callbacks BuildkitCallbacks ) (exporter.Exporter , error ) {
35+ return & imageExporterMobyWrapper {
36+ exp : exp ,
37+ callbacks : callbacks ,
38+ }, nil
2739}
2840
2941// Resolve applies moby specific attributes to the request.
@@ -46,12 +58,15 @@ func (e *imageExporterMobyWrapper) Resolve(ctx context.Context, id int, exporter
4658 return nil , err
4759 }
4860
49- return & imageExporterInstanceWrapper {ExporterInstance : inst , callback : e .callback }, nil
61+ return & imageExporterInstanceWrapper {
62+ ExporterInstance : inst ,
63+ callbacks : e .callbacks ,
64+ }, nil
5065}
5166
5267type imageExporterInstanceWrapper struct {
5368 exporter.ExporterInstance
54- callback ImageExportedByBuildkit
69+ callbacks BuildkitCallbacks
5570}
5671
5772func (i * imageExporterInstanceWrapper ) Export (ctx context.Context , src * exporter.Source , inlineCache exptypes.InlineCache , sessionID string ) (map [string ]string , exporter.DescriptorReference , error ) {
@@ -62,8 +77,26 @@ func (i *imageExporterInstanceWrapper) Export(ctx context.Context, src *exporter
6277
6378 desc := ref .Descriptor ()
6479 imageID := out [exptypes .ExporterImageDigestKey ]
65- if i .callback != nil {
66- i .callback (ctx , imageID , desc )
80+ if i .callbacks .Exported != nil {
81+ i .callbacks .Exported (ctx , imageID , desc )
82+ }
83+
84+ if i .callbacks .Named != nil {
85+ for _ , name := range strings .Split (out [string (exptypes .OptKeyName )], "," ) {
86+ ref , err := reference .ParseNormalizedNamed (name )
87+ if err != nil {
88+ // Shouldn't happen, but log if it does and continue.
89+ log .G (ctx ).WithFields (log.Fields {
90+ "name" : name ,
91+ "error" : err ,
92+ }).Warn ("image named with invalid reference produced by buildkit" )
93+ continue
94+ }
95+
96+ namedTagged := reference .TagNameOnly (ref ).(reference.NamedTagged )
97+ i .callbacks .Named (ctx , namedTagged , desc )
98+ }
6799 }
100+
68101 return out , ref , nil
69102}
0 commit comments