Skip to content

Commit 53bc396

Browse files
committed
c8d/build: Log image tag event when image was built with Buildkit
Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 1506bbc commit 53bc396

3 files changed

Lines changed: 73 additions & 31 deletions

File tree

cmd/dockerd/daemon.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ func newRouterOptions(ctx context.Context, config *config.Config, d *daemon.Daem
450450
ContainerdNamespace: config.ContainerdNamespace,
451451
Callbacks: exporter.BuildkitCallbacks{
452452
Exported: d.ImageExportedByBuildkit,
453+
Named: d.ImageNamedByBuildkit,
453454
},
454455
})
455456
if err != nil {

daemon/build.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package daemon
33
import (
44
"context"
55

6+
"github.com/distribution/reference"
7+
"github.com/docker/docker/api/types/events"
68
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
79
)
810

@@ -11,5 +13,14 @@ import (
1113
// When no tag is given, buildkit doesn't call the image service so it has no
1214
// way of knowing the image was created.
1315
func (daemon *Daemon) ImageExportedByBuildkit(ctx context.Context, id string, desc ocispec.Descriptor) {
14-
daemon.imageService.LogImageEvent(id, id, "create")
16+
daemon.imageService.LogImageEvent(id, id, events.ActionCreate)
17+
}
18+
19+
// ImageNamedByBuildkit is a callback that is called when an image is tagged by buildkit.
20+
// Note: It is only called if the buildkit didn't call the image service itself to perform the tagging.
21+
// Currently this only happens when the containerd image store is used.
22+
func (daemon *Daemon) ImageNamedByBuildkit(ctx context.Context, ref reference.NamedTagged, desc ocispec.Descriptor) {
23+
id := desc.Digest.String()
24+
name := reference.FamiliarString(ref)
25+
daemon.imageService.LogImageEvent(id, name, events.ActionTag)
1526
}

integration-cli/docker_cli_build_test.go

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6193,40 +6193,70 @@ func (s *DockerCLIBuildSuite) TestBuildIidFileCleanupOnFail(c *testing.T) {
61936193
assert.Equal(c, os.IsNotExist(err), true)
61946194
}
61956195

6196-
func (s *DockerCLIBuildSuite) TestBuildEmitsImageCreateEvent(t *testing.T) {
6197-
for _, tc := range []struct {
6196+
func (s *DockerCLIBuildSuite) TestBuildEmitsEvents(t *testing.T) {
6197+
for _, builder := range []struct {
61986198
buildkit bool
61996199
}{
62006200
{buildkit: false},
62016201
{buildkit: true},
62026202
} {
6203-
tc := tc
6204-
t.Run(fmt.Sprintf("buildkit=%v", tc.buildkit), func(t *testing.T) {
6205-
skip.If(t, DaemonIsWindows, "Buildkit is not supported on Windows")
6206-
6207-
before := time.Now()
6208-
6209-
b := cli.Docker(cli.Args("build"),
6210-
build.WithoutCache,
6211-
build.WithDockerfile("FROM busybox\nRUN echo hi >/hello"),
6212-
build.WithBuildkit(tc.buildkit),
6213-
)
6214-
b.Assert(t, icmd.Success)
6215-
t.Log(b.Stdout())
6216-
t.Log(b.Stderr())
6217-
6218-
cmd := cli.Docker(
6219-
cli.Args("events",
6220-
"--filter", "action=create,type=image",
6221-
"--since", before.Format(time.RFC3339),
6222-
),
6223-
cli.WithTimeout(time.Millisecond*300),
6224-
cli.WithEnvironmentVariables("DOCKER_API_VERSION=v1.46"), // FIXME(thaJeztah): integration-cli runs docker CLI 17.06; we're "upgrading" the API version to a version it doesn't support here ;)
6225-
)
6226-
6227-
t.Log(cmd.Stdout())
6228-
6229-
assert.Check(t, is.Contains(cmd.Stdout(), "image create"))
6230-
})
6203+
builder := builder
6204+
for _, tc := range []struct {
6205+
name string
6206+
args []string
6207+
check func(t *testing.T, stdout string)
6208+
}{
6209+
{
6210+
name: "no tag",
6211+
args: []string{},
6212+
check: func(t *testing.T, stdout string) {
6213+
assert.Check(t, is.Contains(stdout, "image create"))
6214+
assert.Check(t, !strings.Contains(stdout, "image tag"))
6215+
},
6216+
},
6217+
{
6218+
name: "with tag",
6219+
args: []string{"-t", "testbuildemitsimagetagevent"},
6220+
check: func(t *testing.T, stdout string) {
6221+
assert.Check(t, is.Contains(stdout, "image create"))
6222+
assert.Check(t, is.Contains(stdout, "image tag"))
6223+
assert.Check(t, is.Contains(stdout, "testbuildemitsimagetagevent"))
6224+
},
6225+
},
6226+
} {
6227+
tc := tc
6228+
t.Run(fmt.Sprintf("buildkit=%v/%s", builder.buildkit, tc.name), func(t *testing.T) {
6229+
skip.If(t, DaemonIsWindows, "Buildkit is not supported on Windows")
6230+
6231+
time.Sleep(time.Second)
6232+
before := time.Now()
6233+
6234+
args := []string{"build"}
6235+
args = append(args, tc.args...)
6236+
6237+
b := cli.Docker(cli.Args(args...),
6238+
build.WithoutCache,
6239+
build.WithDockerfile("FROM busybox\nRUN echo hi >/hello"),
6240+
build.WithBuildkit(builder.buildkit),
6241+
)
6242+
b.Assert(t, icmd.Success)
6243+
t.Log(b.Stdout())
6244+
t.Log(b.Stderr())
6245+
6246+
cmd := cli.Docker(
6247+
cli.Args("events",
6248+
"--filter", "type=image",
6249+
"--since", before.Format(time.RFC3339),
6250+
),
6251+
cli.WithTimeout(time.Millisecond*300),
6252+
cli.WithEnvironmentVariables("DOCKER_API_VERSION=v1.46"), // FIXME(thaJeztah): integration-cli runs docker CLI 17.06; we're "upgrading" the API version to a version it doesn't support here ;)
6253+
)
6254+
6255+
stdout := cmd.Stdout()
6256+
t.Log(stdout)
6257+
6258+
tc.check(t, stdout)
6259+
})
6260+
}
62316261
}
62326262
}

0 commit comments

Comments
 (0)