Skip to content

Commit 3f7d3c2

Browse files
committed
add dry-run support for pull command
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 9cc1613 commit 3f7d3c2

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

pkg/api/dryrunclient.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import (
2525
"strings"
2626
"sync"
2727

28+
"github.com/docker/buildx/builder"
29+
"github.com/docker/buildx/util/imagetools"
30+
"github.com/docker/cli/cli/command"
31+
2832
"github.com/distribution/distribution/v3/uuid"
2933
moby "github.com/docker/docker/api/types"
3034
containerType "github.com/docker/docker/api/types/container"
@@ -52,6 +56,7 @@ type DryRunKey struct{}
5256
type DryRunClient struct {
5357
apiClient client.APIClient
5458
execs sync.Map
59+
resolver *imagetools.Resolver
5560
}
5661

5762
type execDetails struct {
@@ -60,11 +65,20 @@ type execDetails struct {
6065
}
6166

6267
// NewDryRunClient produces a DryRunClient
63-
func NewDryRunClient(apiClient client.APIClient) *DryRunClient {
68+
func NewDryRunClient(apiClient client.APIClient, cli *command.DockerCli) (*DryRunClient, error) {
69+
b, err := builder.New(cli, builder.WithSkippedValidation())
70+
if err != nil {
71+
return nil, err
72+
}
73+
configFile, err := b.ImageOpt()
74+
if err != nil {
75+
return nil, err
76+
}
6477
return &DryRunClient{
6578
apiClient: apiClient,
6679
execs: sync.Map{},
67-
}
80+
resolver: imagetools.New(configFile),
81+
}, nil
6882
}
6983

7084
// All methods and functions which need to be overridden for dry run.
@@ -129,8 +143,16 @@ func (d *DryRunClient) ImageBuild(ctx context.Context, reader io.Reader, options
129143
return moby.ImageBuildResponse{}, ErrNotImplemented
130144
}
131145

146+
func (d *DryRunClient) ImageInspectWithRaw(ctx context.Context, imageName string) (moby.ImageInspect, []byte, error) {
147+
return moby.ImageInspect{ID: "dryRunId"}, nil, nil
148+
}
149+
132150
func (d *DryRunClient) ImagePull(ctx context.Context, ref string, options moby.ImagePullOptions) (io.ReadCloser, error) {
133-
return nil, ErrNotImplemented
151+
if _, _, err := d.resolver.Resolve(ctx, ref); err != nil {
152+
return nil, err
153+
}
154+
rc := io.NopCloser(strings.NewReader(""))
155+
return rc, nil
134156
}
135157

136158
func (d *DryRunClient) ImagePush(ctx context.Context, ref string, options moby.ImagePushOptions) (io.ReadCloser, error) {
@@ -304,10 +326,6 @@ func (d *DryRunClient) ImageImport(ctx context.Context, source moby.ImageImportS
304326
return d.apiClient.ImageImport(ctx, source, ref, options)
305327
}
306328

307-
func (d *DryRunClient) ImageInspectWithRaw(ctx context.Context, imageName string) (moby.ImageInspect, []byte, error) {
308-
return d.apiClient.ImageInspectWithRaw(ctx, imageName)
309-
}
310-
311329
func (d *DryRunClient) ImageList(ctx context.Context, options moby.ImageListOptions) ([]moby.ImageSummary, error) {
312330
return d.apiClient.ImageList(ctx, options)
313331
}

pkg/compose/compose.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ func (s *composeService) DryRunMode(ctx context.Context, dryRun bool) (context.C
7575
return ctx, err
7676
}
7777
err = cli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) {
78-
dryRunClient := api.NewDryRunClient(s.apiClient())
79-
return dryRunClient, nil
78+
return api.NewDryRunClient(s.apiClient(), cli)
8079
}))
8180
if err != nil {
8281
return ctx, err

pkg/compose/pull.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
130130
mustBuild = append(mustBuild, service.Name)
131131
}
132132
if !opts.IgnoreFailures && service.Build == nil {
133+
if s.dryRun {
134+
w.Event(progress.Event{
135+
ID: service.Name,
136+
Status: progress.Error,
137+
Text: fmt.Sprintf(" - Pull error for image: %s", service.Image),
138+
})
139+
}
133140
// fail fast if image can't be pulled nor built
134141
return err
135142
}

0 commit comments

Comments
 (0)