diff --git a/internal/build/build.go b/internal/build/build.go index 82e29fae93..8da856ac27 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -25,6 +25,7 @@ import ( "github.com/compose-spec/compose-go/v2/types" "github.com/docker/cli/cli/command" + "github.com/moby/moby/client" "github.com/docker/compose/v5/internal/envresolver" "github.com/docker/compose/v5/pkg/api" @@ -33,9 +34,11 @@ import ( // Backend is what the build needs from the engine it acts on. type Backend interface { - // CLI is the Docker CLI Compose runs in: the daemon client, the credentials, - // the terminal output and the buildx plugin all come from it. + // CLI is the Docker CLI Compose runs in: the credentials, the terminal output + // and the buildx plugin come from it. CLI() command.Cli + // ImageBuildAPI builds the images through the Docker daemon. + ImageBuildAPI() client.ImageBuildAPIClient // Events receives the progress of the builds. Events() api.EventProcessor // DryRun tells whether the build only reports what it would do. diff --git a/internal/build/classic.go b/internal/build/classic.go index b5f76c56f6..548bc5ec92 100644 --- a/internal/build/classic.go +++ b/internal/build/classic.go @@ -196,7 +196,7 @@ func (s *Service) doBuildImage(ctx context.Context, project *types.Project, serv ctx, cancel := context.WithCancel(ctx) defer cancel() s.backend.Events().On(composeprogress.Building(imageName)) - response, err := s.backend.CLI().Client().ImageBuild(ctx, body, buildOpts) + response, err := s.backend.ImageBuildAPI().ImageBuild(ctx, body, buildOpts) if err != nil { return "", err } diff --git a/internal/containerops/commit.go b/internal/containerops/commit.go index f9a49fd932..b4251bae2b 100644 --- a/internal/containerops/commit.go +++ b/internal/containerops/commit.go @@ -54,7 +54,7 @@ func (s *Service) Commit(ctx context.Context, projectName string, options api.Co return nil } - response, err := s.backend.APIClient().ContainerCommit(ctx, ctr.ID, client.ContainerCommitOptions{ + response, err := s.backend.ContainerAPI().ContainerCommit(ctx, ctr.ID, client.ContainerCommitOptions{ Reference: options.Reference, Comment: options.Comment, Author: options.Author, diff --git a/internal/containerops/containerops.go b/internal/containerops/containerops.go index ca6e63388b..c41e6605de 100644 --- a/internal/containerops/containerops.go +++ b/internal/containerops/containerops.go @@ -20,6 +20,7 @@ package containerops import ( "github.com/docker/cli/cli/command" + "github.com/moby/moby/client" "github.com/docker/compose/v5/internal/engine" "github.com/docker/compose/v5/pkg/api" @@ -33,6 +34,8 @@ import ( // implementation). type Backend interface { engine.Backend + // ContainerAPI acts on the containers: copy, export, commit. + ContainerAPI() client.ContainerAPIClient // Events receives the progress of the operations. Events() api.EventProcessor // DryRun tells whether the operations only report what they would do. diff --git a/internal/containerops/cp.go b/internal/containerops/cp.go index b78a4e0e33..e3ca5673e0 100644 --- a/internal/containerops/cp.go +++ b/internal/containerops/cp.go @@ -155,7 +155,7 @@ func (s *Service) copyToContainer(ctx context.Context, containerID string, srcPa // Prepare destination copy info by stat-ing the container path. dstInfo := archive.CopyInfo{Path: dstPath} var dstStat container.PathStat - res, err := s.backend.APIClient().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{ + res, err := s.backend.ContainerAPI().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{ Path: dstPath, }) if err == nil { @@ -172,7 +172,7 @@ func (s *Service) copyToContainer(ctx context.Context, containerID string, srcPa } dstInfo.Path = linkTarget - res, err = s.backend.APIClient().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{ + res, err = s.backend.ContainerAPI().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{ Path: linkTarget, }) if err == nil { @@ -244,7 +244,7 @@ func (s *Service) copyToContainer(ctx context.Context, containerID string, srcPa } } - _, err = s.backend.APIClient().CopyToContainer(ctx, containerID, client.CopyToContainerOptions{ + _, err = s.backend.ContainerAPI().CopyToContainer(ctx, containerID, client.CopyToContainerOptions{ DestinationPath: resolvedDstPath, Content: content, AllowOverwriteDirWithFile: false, @@ -271,7 +271,7 @@ func (s *Service) copyFromContainer(ctx context.Context, containerID, srcPath, d var rebaseName string if options.FollowLink { var srcStat container.PathStat - res, err := s.backend.APIClient().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{ + res, err := s.backend.ContainerAPI().ContainerStatPath(ctx, containerID, client.ContainerStatPathOptions{ Path: srcPath, }) if err == nil { @@ -292,7 +292,7 @@ func (s *Service) copyFromContainer(ctx context.Context, containerID, srcPath, d } } - res, err := s.backend.APIClient().CopyFromContainer(ctx, containerID, client.CopyFromContainerOptions{ + res, err := s.backend.ContainerAPI().CopyFromContainer(ctx, containerID, client.CopyFromContainerOptions{ SourcePath: srcPath, }) if err != nil { diff --git a/internal/containerops/cp_test.go b/internal/containerops/cp_test.go index 738ed7b858..8809f340fe 100644 --- a/internal/containerops/cp_test.go +++ b/internal/containerops/cp_test.go @@ -40,11 +40,11 @@ type fakeBackend struct { asked string } -func (f *fakeBackend) APIClient() client.APIClient { return nil } -func (f *fakeBackend) MaxConcurrency() int { return 0 } -func (f *fakeBackend) Events() api.EventProcessor { return nil } -func (f *fakeBackend) DryRun() bool { return false } -func (f *fakeBackend) CLI() command.Cli { return nil } +func (f *fakeBackend) ContainerAPI() client.ContainerAPIClient { return nil } +func (f *fakeBackend) MaxConcurrency() int { return 0 } +func (f *fakeBackend) Events() api.EventProcessor { return nil } +func (f *fakeBackend) DryRun() bool { return false } +func (f *fakeBackend) CLI() command.Cli { return nil } func (f *fakeBackend) Containers(_ context.Context, _ string, oneOff engine.OneOff, all bool, services ...string) ([]container.Summary, error) { f.asked = "Containers" diff --git a/internal/containerops/export.go b/internal/containerops/export.go index c061952127..3119b52611 100644 --- a/internal/containerops/export.go +++ b/internal/containerops/export.go @@ -57,7 +57,7 @@ func (s *Service) Export(ctx context.Context, projectName string, options api.Ex Status: api.Working, }) - responseBody, err := s.backend.APIClient().ContainerExport(ctx, container.ID, client.ContainerExportOptions{}) + responseBody, err := s.backend.ContainerAPI().ContainerExport(ctx, container.ID, client.ContainerExportOptions{}) if err != nil { return err } diff --git a/internal/desktop/client.go b/internal/desktop/client.go index 8097bc9017..44c846011e 100644 --- a/internal/desktop/client.go +++ b/internal/desktop/client.go @@ -168,7 +168,7 @@ func (c *Client) IsFeatureEnabled(ctx context.Context, feature string) (bool, er // given feature flag is enabled. Returns false silently on any failure — the // engine being unreachable, Desktop not being the active engine, or the flag // being off — so callers can use this as a single gating check. -func IsFeatureActive(ctx context.Context, apiClient client.APIClient, feature string) bool { +func IsFeatureActive(ctx context.Context, apiClient client.SystemAPIClient, feature string) bool { endpoint, err := Endpoint(ctx, apiClient) if err != nil || endpoint == "" { return false diff --git a/internal/desktop/proxy.go b/internal/desktop/proxy.go index 2adb6d80cb..c840d29e8e 100644 --- a/internal/desktop/proxy.go +++ b/internal/desktop/proxy.go @@ -40,7 +40,7 @@ const ddProxyHost = "docker-desktop-http-proxy.invalid" // Endpoint returns the Docker Desktop API socket endpoint advertised via the // engine info labels, or "" when the active engine is not Docker Desktop. -func Endpoint(ctx context.Context, apiClient client.APIClient) (string, error) { +func Endpoint(ctx context.Context, apiClient client.SystemAPIClient) (string, error) { res, err := apiClient.Info(ctx, client.InfoOptions{}) if err != nil { return "", err @@ -177,7 +177,7 @@ func isLoopbackHost(host string) bool { // ProxyTransportFor discovers the Docker Desktop endpoint via apiClient and // returns the matching transport, or nil when DD is not active or discovery // fails (so callers fall back to their own default transport). -func ProxyTransportFor(ctx context.Context, apiClient client.APIClient) http.RoundTripper { +func ProxyTransportFor(ctx context.Context, apiClient client.SystemAPIClient) http.RoundTripper { endpoint, err := Endpoint(ctx, apiClient) if err != nil { logrus.Debugf("could not detect Docker Desktop endpoint, deferring to caller's default transport: %v", err) diff --git a/internal/devwatch/watch.go b/internal/devwatch/watch.go index c181f07e99..000a617bb3 100644 --- a/internal/devwatch/watch.go +++ b/internal/devwatch/watch.go @@ -58,6 +58,10 @@ import ( // Backend is what the development loop needs from the engine it drives. type Backend interface { engine.Backend + // ExecAPI runs the sync commands and hooks in the containers. + ExecAPI() client.ExecAPIClient + // ContainerAPI copies the synced files into the containers. + ContainerAPI() client.ContainerAPIClient // CLI is the Docker CLI Compose runs in: it owns the output stream the // sync commands write to, the config the exec hooks read, and runs them. CLI() command.Cli @@ -466,7 +470,7 @@ func (t tarDockerClient) ContainersForService(ctx context.Context, projectName s } func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []string, in io.Reader) error { - execCreateResp, err := t.backend.APIClient().ExecCreate(ctx, containerID, client.ExecCreateOptions{ + execCreateResp, err := t.backend.ExecAPI().ExecCreate(ctx, containerID, client.ExecCreateOptions{ Cmd: cmd, AttachStdout: false, AttachStderr: true, @@ -477,7 +481,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str return err } - conn, err := t.backend.APIClient().ExecAttach(ctx, execCreateResp.ID, client.ExecAttachOptions{ + conn, err := t.backend.ExecAPI().ExecAttach(ctx, execCreateResp.ID, client.ExecAttachOptions{ TTY: false, }) if err != nil { @@ -500,7 +504,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str return err }) - _, err = t.backend.APIClient().ExecStart(ctx, execCreateResp.ID, client.ExecStartOptions{ + _, err = t.backend.ExecAPI().ExecStart(ctx, execCreateResp.ID, client.ExecStartOptions{ TTY: false, Detach: false, }) @@ -515,7 +519,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str return err } - execResult, err := t.backend.APIClient().ExecInspect(ctx, execCreateResp.ID, client.ExecInspectOptions{}) + execResult, err := t.backend.ExecAPI().ExecInspect(ctx, execCreateResp.ID, client.ExecInspectOptions{}) if err != nil { return err } @@ -529,7 +533,7 @@ func (t tarDockerClient) Exec(ctx context.Context, containerID string, cmd []str } func (t tarDockerClient) Untar(ctx context.Context, id string, archive io.ReadCloser) error { - _, err := t.backend.APIClient().CopyToContainer(ctx, id, client.CopyToContainerOptions{ + _, err := t.backend.ContainerAPI().CopyToContainer(ctx, id, client.CopyToContainerOptions{ DestinationPath: "/", Content: archive, CopyUIDGID: true, diff --git a/internal/devwatch/watch_test.go b/internal/devwatch/watch_test.go index 9809dcec8d..3a53f46200 100644 --- a/internal/devwatch/watch_test.go +++ b/internal/devwatch/watch_test.go @@ -49,7 +49,12 @@ type fakeBackend struct { removeDanglingImages func(ctx context.Context, projectName string, keep func(image.Summary) bool) error } -func (f *fakeBackend) APIClient() client.APIClient { +func (f *fakeBackend) ExecAPI() client.ExecAPIClient { + f.t.Errorf("unexpected call to the Docker client") + return nil +} + +func (f *fakeBackend) ContainerAPI() client.ContainerAPIClient { f.t.Errorf("unexpected call to the Docker client") return nil } diff --git a/internal/engine/engine.go b/internal/engine/engine.go index 0ad0045437..31ef7ac788 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -22,7 +22,6 @@ import ( "context" "github.com/moby/moby/api/types/container" - "github.com/moby/moby/client" ) // OneOff tells whether the containers of `compose run` are part of a listing. @@ -35,11 +34,10 @@ const ( OneOffExclude ) -// Backend is what every operation needs from the engine: the Docker daemon, the -// bound on the calls made to it, and the containers of a project. +// Backend is what every operation needs from the engine: the bound on the calls +// made to it, and the containers of a project. The calls themselves come from +// the families of the engine API each operation asks for in its own Backend. type Backend interface { - // APIClient talks to the Docker daemon. - APIClient() client.APIClient // MaxConcurrency bounds the daemon calls made in parallel, 0 meaning no bound. MaxConcurrency() int // Containers lists the containers of a project, narrowed to services when given. diff --git a/internal/images/images.go b/internal/images/images.go index 7065d84aad..0f8fa1e731 100644 --- a/internal/images/images.go +++ b/internal/images/images.go @@ -55,13 +55,15 @@ const ( ) // Backend is what the operations need from the engine they act on. It is -// narrow on purpose: it redeclares APIClient and MaxConcurrency of -// engine.Backend rather than embedding it, which would force every +// narrow on purpose: it redeclares MaxConcurrency of engine.Backend rather +// than embedding it, which would force every // implementation (the fake of the tests included) to provide Containers and // SpecifiedContainer, that these operations never call. type Backend interface { - // APIClient talks to the Docker daemon. - APIClient() client.APIClient + // ImageAPI reads and removes the images of the Docker daemon. + ImageAPI() client.ImageAPIClient + // ContainerAPI lists the containers that use the images. + ContainerAPI() client.ContainerAPIClient // MaxConcurrency bounds the daemon calls made in parallel, 0 meaning no bound. MaxConcurrency() int // RuntimeAPIVersion is the API version negotiated with the daemon. @@ -85,7 +87,7 @@ func New(backend Backend) *Service { // Images lists the images the containers of a project run. func (s *Service) Images(ctx context.Context, projectName string, options api.ImagesOptions) (map[string]api.ImageSummary, error) { projectName = strings.ToLower(projectName) - allContainers, err := s.backend.APIClient().ContainerList(ctx, client.ContainerListOptions{ + allContainers, err := s.backend.ContainerAPI().ContainerList(ctx, client.ContainerListOptions{ All: true, Filters: naming.ProjectFilter(projectName), }) @@ -147,7 +149,7 @@ func (s *Service) containerImageSummary(ctx context.Context, c container.Summary } } - img, err := s.backend.APIClient().ImageInspect(ctx, c.Image) + img, err := s.backend.ImageAPI().ImageInspect(ctx, c.Image) if errdefs.IsNotFound(err) { return api.ImageSummary{ ID: c.ImageID, @@ -161,7 +163,7 @@ func (s *Service) containerImageSummary(ctx context.Context, c container.Summary id := img.ID // platform-specific image ID can't be combined with image tag, see https://github.com/moby/moby/issues/49995 if withPlatform && c.ImageManifestDescriptor != nil && c.ImageManifestDescriptor.Platform != nil { - img, err = s.backend.APIClient().ImageInspect(ctx, c.Image, client.ImageInspectWithPlatform(c.ImageManifestDescriptor.Platform)) + img, err = s.backend.ImageAPI().ImageInspect(ctx, c.Image, client.ImageInspectWithPlatform(c.ImageManifestDescriptor.Platform)) if err != nil { return api.ImageSummary{}, err } @@ -205,7 +207,7 @@ func (s *Service) InspectLocalImages(ctx context.Context, repoTags []string) (ma eg, ctx := concurrency.NewErrgroup(ctx, s.backend.MaxConcurrency()) for _, repoTag := range repoTags { eg.Go(func() error { - inspect, err := s.backend.APIClient().ImageInspect(ctx, repoTag, opts...) + inspect, err := s.backend.ImageAPI().ImageInspect(ctx, repoTag, opts...) if err != nil { if errdefs.IsNotFound(err) { return nil @@ -389,7 +391,7 @@ func (s *Service) InspectLocalContent(ctx context.Context, ref string, platform if err != nil { return "", false, err } - inspected, err := s.backend.APIClient().ImageInspect(ctx, ref, opts...) + inspected, err := s.backend.ImageAPI().ImageInspect(ctx, ref, opts...) if err != nil { return "", false, err } diff --git a/internal/images/images_test.go b/internal/images/images_test.go index f01be5a799..dd1bec3887 100644 --- a/internal/images/images_test.go +++ b/internal/images/images_test.go @@ -46,8 +46,9 @@ type fakeBackend struct { version string } -func (f *fakeBackend) APIClient() client.APIClient { return f.apiClient } -func (f *fakeBackend) MaxConcurrency() int { return 0 } +func (f *fakeBackend) ImageAPI() client.ImageAPIClient { return f.apiClient } +func (f *fakeBackend) ContainerAPI() client.ContainerAPIClient { return f.apiClient } +func (f *fakeBackend) MaxConcurrency() int { return 0 } func (f *fakeBackend) RuntimeAPIVersion(context.Context) (string, error) { return f.version, nil } diff --git a/internal/images/prune.go b/internal/images/prune.go index 85097241d6..cd01bd35f8 100644 --- a/internal/images/prune.go +++ b/internal/images/prune.go @@ -175,7 +175,7 @@ func (s *Service) RemoveImages(ctx context.Context, images []image.Summary, limi // shared errgroup ctx, so fn always returns nil and collects into the // buckets below instead. _ = concurrency.ForEach(ctx, limiter, images, func(ctx context.Context, img image.Summary) error { - if _, err := s.backend.APIClient().ImageRemove(ctx, img.ID, client.ImageRemoveOptions{}); err != nil { + if _, err := s.backend.ImageAPI().ImageRemove(ctx, img.ID, client.ImageRemoveOptions{}); err != nil { mu.Lock() defer mu.Unlock() imgStillInUse, alreadyGone := s.backend.ClassifyRemovalError(err) @@ -206,7 +206,7 @@ func (s *Service) RemoveImages(ctx context.Context, images []image.Summary, limi // itself, to decide whether there's anything to report) and watch --prune, // so the two don't drift apart. func (s *Service) EligibleDanglingImages(ctx context.Context, projectName string, keep func(image.Summary) bool) ([]image.Summary, error) { - res, err := s.backend.APIClient().ImageList(ctx, client.ImageListOptions{ + res, err := s.backend.ImageAPI().ImageList(ctx, client.ImageListOptions{ Filters: naming.ProjectFilter(projectName).Add("dangling", "true"), }) if err != nil { diff --git a/internal/inspect/events.go b/internal/inspect/events.go index 7085bf3bd8..61ed84353e 100644 --- a/internal/inspect/events.go +++ b/internal/inspect/events.go @@ -32,7 +32,7 @@ import ( func (s *Service) Events(ctx context.Context, projectName string, options api.EventsOptions) error { projectName = strings.ToLower(projectName) - res := s.backend.APIClient().Events(ctx, client.EventsListOptions{ + res := s.backend.SystemAPI().Events(ctx, client.EventsListOptions{ Filters: naming.ProjectFilter(projectName), Since: options.Since, Until: options.Until, diff --git a/internal/inspect/inspect.go b/internal/inspect/inspect.go index 7f76256838..5a8f69b262 100644 --- a/internal/inspect/inspect.go +++ b/internal/inspect/inspect.go @@ -19,12 +19,20 @@ package inspect import ( + "github.com/moby/moby/client" + "github.com/docker/compose/v5/internal/engine" ) // Backend is what the operations need from the engine they report on. type Backend interface { engine.Backend + // ContainerAPI lists, inspects and reads the processes of the containers. + ContainerAPI() client.ContainerAPIClient + // VolumeAPI lists the volumes. + VolumeAPI() client.VolumeAPIClient + // SystemAPI streams the events of the daemon. + SystemAPI() client.SystemAPIClient } // Service runs the inspection operations on a Backend. diff --git a/internal/inspect/inspect_test.go b/internal/inspect/inspect_test.go index e52062df98..9adf2b0529 100644 --- a/internal/inspect/inspect_test.go +++ b/internal/inspect/inspect_test.go @@ -41,8 +41,10 @@ type fakeBackend struct { index int } -func (f *fakeBackend) APIClient() client.APIClient { return f.apiClient } -func (f *fakeBackend) MaxConcurrency() int { return 2 } +func (f *fakeBackend) ContainerAPI() client.ContainerAPIClient { return f.apiClient } +func (f *fakeBackend) VolumeAPI() client.VolumeAPIClient { return f.apiClient } +func (f *fakeBackend) SystemAPI() client.SystemAPIClient { return f.apiClient } +func (f *fakeBackend) MaxConcurrency() int { return 2 } func (f *fakeBackend) Containers(_ context.Context, _ string, oneOff engine.OneOff, all bool, services ...string) ([]container.Summary, error) { f.oneOff, f.all, f.services = oneOff, all, services diff --git a/internal/inspect/ls.go b/internal/inspect/ls.go index d08ebbba5f..1ca92bae15 100644 --- a/internal/inspect/ls.go +++ b/internal/inspect/ls.go @@ -31,7 +31,7 @@ import ( ) func (s *Service) List(ctx context.Context, options api.ListOptions) ([]api.Stack, error) { - list, err := s.backend.APIClient().ContainerList(ctx, client.ContainerListOptions{ + list, err := s.backend.ContainerAPI().ContainerList(ctx, client.ContainerListOptions{ Filters: make(client.Filters).Add("label", api.ProjectLabel).Add("label", api.ConfigHashLabel), All: options.All, }) diff --git a/internal/inspect/ps.go b/internal/inspect/ps.go index 00a2030e8a..8c73e309fb 100644 --- a/internal/inspect/ps.go +++ b/internal/inspect/ps.go @@ -59,7 +59,7 @@ func (s *Service) Ps(ctx context.Context, projectName string, options api.PsOpti // containerSummary builds the api summary for a container, inspecting it to // retrieve its health and exit code func (s *Service) containerSummary(ctx context.Context, ctr container.Summary) (api.ContainerSummary, error) { - inspect, err := s.backend.APIClient().ContainerInspect(ctx, ctr.ID, client.ContainerInspectOptions{}) + inspect, err := s.backend.ContainerAPI().ContainerInspect(ctx, ctr.ID, client.ContainerInspectOptions{}) if err != nil { return api.ContainerSummary{}, err } diff --git a/internal/inspect/top.go b/internal/inspect/top.go index 40074e3b0d..652d4880e6 100644 --- a/internal/inspect/top.go +++ b/internal/inspect/top.go @@ -41,7 +41,7 @@ func (s *Service) Top(ctx context.Context, projectName string, services []string eg, ctx := concurrency.NewErrgroup(ctx, s.backend.MaxConcurrency()) for i, ctr := range containers { eg.Go(func() error { - topContent, err := s.backend.APIClient().ContainerTop(ctx, ctr.ID, client.ContainerTopOptions{ + topContent, err := s.backend.ContainerAPI().ContainerTop(ctx, ctr.ID, client.ContainerTopOptions{ Arguments: []string{}, }) if err != nil { diff --git a/internal/inspect/volumes.go b/internal/inspect/volumes.go index 90855abc00..a75f803c04 100644 --- a/internal/inspect/volumes.go +++ b/internal/inspect/volumes.go @@ -27,7 +27,7 @@ import ( ) func (s *Service) Volumes(ctx context.Context, projectName string, options api.VolumesOptions) ([]api.VolumesSummary, error) { - allContainers, err := s.backend.APIClient().ContainerList(ctx, client.ContainerListOptions{ + allContainers, err := s.backend.ContainerAPI().ContainerList(ctx, client.ContainerListOptions{ Filters: naming.ProjectFilter(projectName), }) if err != nil { @@ -42,7 +42,7 @@ func (s *Service) Volumes(ctx context.Context, projectName string, options api.V containers = allContainers.Items } - volumesResponse, err := s.backend.APIClient().VolumeList(ctx, client.VolumeListOptions{ + volumesResponse, err := s.backend.VolumeAPI().VolumeList(ctx, client.VolumeListOptions{ Filters: naming.ProjectFilter(projectName), }) if err != nil { diff --git a/internal/logstream/attach.go b/internal/logstream/attach.go index 9d8dd65239..ec52167696 100644 --- a/internal/logstream/attach.go +++ b/internal/logstream/attach.go @@ -74,7 +74,7 @@ func (s *Service) AttachContainers(ctx context.Context, project *types.Project, // AttachContainer attaches to one container, reporting its output to listener. func (s *Service) AttachContainer(ctx context.Context, service, id, name string, listener api.ContainerEventListener) error { - inspected, err := s.backend.APIClient().ContainerInspect(ctx, id, client.ContainerInspectOptions{}) + inspected, err := s.backend.ContainerAPI().ContainerInspect(ctx, id, client.ContainerInspectOptions{}) if err != nil { return err } @@ -141,7 +141,7 @@ func (s *Service) attachContainerStreams(ctx context.Context, containerID string } func (s *Service) getContainerStreams(ctx context.Context, containerID string) (io.ReadCloser, error) { - attachResponse, err := s.backend.APIClient().ContainerAttach(ctx, containerID, client.ContainerAttachOptions{ + attachResponse, err := s.backend.ContainerAPI().ContainerAttach(ctx, containerID, client.ContainerAttachOptions{ Stream: true, Stdin: false, Stdout: true, @@ -154,7 +154,7 @@ func (s *Service) getContainerStreams(ctx context.Context, containerID string) ( } // Fallback to logs API - logs, err := s.backend.APIClient().ContainerLogs(ctx, containerID, client.ContainerLogsOptions{ + logs, err := s.backend.ContainerAPI().ContainerLogs(ctx, containerID, client.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, Follow: true, diff --git a/internal/logstream/logs.go b/internal/logstream/logs.go index e5e5eed9c3..26299d9132 100644 --- a/internal/logstream/logs.go +++ b/internal/logstream/logs.go @@ -74,7 +74,7 @@ func (s *Service) Logs( if options.Follow { printer := NewPrinter(consumer) - monitor := NewMonitor(s.backend.APIClient(), projectName) + monitor := NewMonitor(s.backend.ContainerAPI(), s.backend.SystemAPI(), projectName) if len(options.Services) > 0 { monitor.WithServices(options.Services) } else if options.Project != nil { @@ -121,7 +121,7 @@ func (s *Service) InspectWithSlot(ctx context.Context, limiter *semaphore.Weight return container.InspectResponse{}, err } defer concurrency.PanicSafeRelease(limiter) - res, err := s.backend.APIClient().ContainerInspect(ctx, id, client.ContainerInspectOptions{}) + res, err := s.backend.ContainerAPI().ContainerInspect(ctx, id, client.ContainerInspectOptions{}) if err != nil { concurrency.Release(limiter) return container.InspectResponse{}, err @@ -298,7 +298,7 @@ func (s *Service) DoLogContainer( // must not re-trigger it and release the same slot twice. r, err := func() (io.ReadCloser, error) { defer concurrency.PanicSafeRelease(limiter) - r, err := s.backend.APIClient().ContainerLogs(ctx, ctr.ID, client.ContainerLogsOptions{ + r, err := s.backend.ContainerAPI().ContainerLogs(ctx, ctr.ID, client.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, Follow: options.Follow, diff --git a/internal/logstream/logstream.go b/internal/logstream/logstream.go index 98e1e9473b..2fe4e39fe2 100644 --- a/internal/logstream/logstream.go +++ b/internal/logstream/logstream.go @@ -20,6 +20,7 @@ package logstream import ( "github.com/docker/cli/cli/command" + "github.com/moby/moby/client" "github.com/docker/compose/v5/internal/engine" "github.com/docker/compose/v5/internal/naming" @@ -32,6 +33,10 @@ import ( // announcement to, and the Docker CLI's own attach implementation. type Backend interface { engine.Backend + // ContainerAPI inspects, attaches to and reads the logs of the containers. + ContainerAPI() client.ContainerAPIClient + // SystemAPI reads the daemon and its events. + SystemAPI() client.SystemAPIClient // CLI is the Docker CLI Compose runs in, and the owner of the terminal streams. CLI() command.Cli } diff --git a/internal/logstream/monitor.go b/internal/logstream/monitor.go index 56f6861e1d..f7a243ea89 100644 --- a/internal/logstream/monitor.go +++ b/internal/logstream/monitor.go @@ -37,8 +37,9 @@ import ( // Monitor follows the events of the containers of a project and reports them to // its listeners, see Start. type Monitor struct { - apiClient client.APIClient - project string + containers client.ContainerAPIClient + system client.SystemAPIClient + project string // services tells us which service to consider and those we can ignore, maybe ran by a concurrent compose command services map[string]bool listeners []api.ContainerEventListener @@ -51,11 +52,12 @@ type Monitor struct { } // NewMonitor creates a Monitor of the containers of a project. -func NewMonitor(apiClient client.APIClient, project string) *Monitor { +func NewMonitor(containers client.ContainerAPIClient, system client.SystemAPIClient, project string) *Monitor { return &Monitor{ - apiClient: apiClient, - project: project, - services: map[string]bool{}, + containers: containers, + system: system, + project: project, + services: map[string]bool{}, } } @@ -178,7 +180,7 @@ func (c *Monitor) Start(ctx context.Context) error { // restarting tracks containers which exited but are configured to restart on exit restarting := utils.Set[string]{} - res := c.apiClient.Events(ctx, client.EventsListOptions{ + res := c.system.Events(ctx, client.EventsListOptions{ Since: c.since, Filters: naming.ProjectFilter(c.project).Add("type", "container").Add("label", oneOffFilter(false)), }) @@ -238,7 +240,7 @@ func (c *Monitor) ReadDaemonTime(ctx context.Context) error { if c.sinceRead { return nil } - res, err := c.apiClient.Info(ctx, client.InfoOptions{}) + res, err := c.system.Info(ctx, client.InfoOptions{}) if err != nil { if ctx.Err() != nil { return fmt.Errorf("reading the daemon time to subscribe to container events: %w", err) @@ -263,7 +265,7 @@ func (c *Monitor) ReadDaemonTime(ctx context.Context) error { // initialContainers collects the application's containers at startup, // restricted to the services this monitor watches func (c *Monitor) initialContainers(ctx context.Context) (utils.Set[string], error) { - initialState, err := c.apiClient.ContainerList(ctx, client.ContainerListOptions{ + initialState, err := c.containers.ContainerList(ctx, client.ContainerListOptions{ All: true, Filters: naming.ProjectFilter(c.project).Add("label", oneOffFilter(false), @@ -405,7 +407,7 @@ func (c *Monitor) onContainerDestroy(ctr *api.ContainerSummary, containers, rest // (see https://github.com/moby/moby/issues/45538). A container already removed won't // come back, which found reports; exitCode is only meaningful when found is true. func (c *Monitor) isRestarting(ctx context.Context, containerID string) (willRestart, found bool, exitCode int, err error) { - inspect, err := c.apiClient.ContainerInspect(ctx, containerID, client.ContainerInspectOptions{}) + inspect, err := c.containers.ContainerInspect(ctx, containerID, client.ContainerInspectOptions{}) if errdefs.IsNotFound(err) { return false, false, 0, nil } diff --git a/internal/projectloader/generate.go b/internal/projectloader/generate.go index ff56353c2e..8d88d1c150 100644 --- a/internal/projectloader/generate.go +++ b/internal/projectloader/generate.go @@ -34,7 +34,7 @@ import ( ) func (s *Service) Generate(ctx context.Context, options api.GenerateOptions) (*types.Project, error) { - res, err := s.backend.APIClient().ContainerList(ctx, client.ContainerListOptions{ + res, err := s.backend.ContainerAPI().ContainerList(ctx, client.ContainerListOptions{ Filters: make(client.Filters).Add("name", options.Containers...), All: true, }) @@ -43,7 +43,7 @@ func (s *Service) Generate(ctx context.Context, options api.GenerateOptions) (*t } containers := res.Items - containersByIds, err := s.backend.APIClient().ContainerList(ctx, client.ContainerListOptions{ + containersByIds, err := s.backend.ContainerAPI().ContainerList(ctx, client.ContainerListOptions{ Filters: make(client.Filters).Add("id", options.Containers...), All: true, }) @@ -103,7 +103,7 @@ func (s *Service) createProjectFromContainers(containers []container.Summary, pr for _, ctr := range containers { serviceLabel, service := ServiceOfContainer(services, ctr) - inspect, err := s.backend.APIClient().ContainerInspect(context.Background(), ctr.ID, client.ContainerInspectOptions{}) + inspect, err := s.backend.ContainerAPI().ContainerInspect(context.Background(), ctr.ID, client.ContainerInspectOptions{}) if err != nil { services[serviceLabel] = service continue @@ -228,7 +228,7 @@ func (s *Service) toComposeNetwork(networks map[string]*network.EndpointSettings serviceNetworkConfigs := make(map[string]*types.ServiceNetworkConfig) for name, net := range networks { - inspect, err := s.backend.APIClient().NetworkInspect(context.Background(), name, client.NetworkInspectOptions{}) + inspect, err := s.backend.NetworkAPI().NetworkInspect(context.Background(), name, client.NetworkInspectOptions{}) if err != nil { networkConfigs[name] = types.NetworkConfig{} } else { diff --git a/internal/projectloader/loader_test.go b/internal/projectloader/loader_test.go index 2dbba5fbce..fd2730e93d 100644 --- a/internal/projectloader/loader_test.go +++ b/internal/projectloader/loader_test.go @@ -316,5 +316,6 @@ func TestLoadProject_MissingComposeFile(t *testing.T) { // noBackend is a Backend with no daemon and no CLI, enough to load local Compose files. type noBackend struct{} -func (noBackend) APIClient() client.APIClient { return nil } -func (noBackend) CLI() command.Cli { return nil } +func (noBackend) ContainerAPI() client.ContainerAPIClient { return nil } +func (noBackend) NetworkAPI() client.NetworkAPIClient { return nil } +func (noBackend) CLI() command.Cli { return nil } diff --git a/internal/projectloader/projectloader.go b/internal/projectloader/projectloader.go index 1c50a03031..ac220b3baf 100644 --- a/internal/projectloader/projectloader.go +++ b/internal/projectloader/projectloader.go @@ -26,8 +26,10 @@ import ( // Backend is what loading or generating a project needs from the engine. type Backend interface { - // APIClient talks to the Docker daemon, to read the containers to generate a project from. - APIClient() client.APIClient + // ContainerAPI reads the containers to generate a project from. + ContainerAPI() client.ContainerAPIClient + // NetworkAPI reads the networks of those containers. + NetworkAPI() client.NetworkAPIClient // CLI is the Docker CLI, which resolves the remote (Git, OCI) Compose files. CLI() command.Cli } diff --git a/internal/provider/backend_test.go b/internal/provider/backend_test.go index f25e39cb36..de8dc303a1 100644 --- a/internal/provider/backend_test.go +++ b/internal/provider/backend_test.go @@ -38,11 +38,13 @@ type testBackend struct { mu sync.Mutex } -func (b *testBackend) APIClient() client.APIClient { return b.apiClient } -func (b *testBackend) Events() api.EventProcessor { return progress.Noop{} } -func (b *testBackend) CLI() command.Cli { return b.cli } -func (b *testBackend) DryRun() bool { return b.dryRun } -func (b *testBackend) ProjectLock() sync.Locker { return &b.mu } +func (b *testBackend) ContainerAPI() client.ContainerAPIClient { return b.apiClient } +func (b *testBackend) NetworkAPI() client.NetworkAPIClient { return b.apiClient } +func (b *testBackend) ImageAPI() client.ImageAPIClient { return b.apiClient } +func (b *testBackend) Events() api.EventProcessor { return progress.Noop{} } +func (b *testBackend) CLI() command.Cli { return b.cli } +func (b *testBackend) DryRun() bool { return b.dryRun } +func (b *testBackend) ProjectLock() sync.Locker { return &b.mu } func (b *testBackend) IsDesktopIntegrationActive(ctx context.Context) (bool, error) { endpoint, err := desktop.Endpoint(ctx, b.apiClient) diff --git a/internal/provider/plugins.go b/internal/provider/plugins.go index 5a89c50347..d534dc6955 100644 --- a/internal/provider/plugins.go +++ b/internal/provider/plugins.go @@ -393,7 +393,7 @@ func (s *Service) streamImageTo(ctx context.Context, w io.Writer, ref, platform } opts = append(opts, client.ImageSaveWithPlatforms(p)) } - tar, err := s.backend.APIClient().ImageSave(ctx, []string{ref}, opts...) + tar, err := s.backend.ImageAPI().ImageSave(ctx, []string{ref}, opts...) if err != nil { if werr := announce(imageStreamAnswer{Type: ImageStreamType, Error: err.Error()}); werr != nil { return &streamAbortedError{werr} diff --git a/internal/publish/publish.go b/internal/publish/publish.go index 71c51d6b3a..113eced36a 100644 --- a/internal/publish/publish.go +++ b/internal/publish/publish.go @@ -54,8 +54,10 @@ import ( // Backend is what publishing needs from the engine it acts on. type Backend interface { - // APIClient talks to the Docker daemon. - APIClient() client.APIClient + // DistributionAPI resolves the digests of the images through the Docker daemon. + DistributionAPI() client.DistributionAPIClient + // SystemAPI reads the daemon, to tell whether Docker Desktop is the engine. + SystemAPI() client.SystemAPIClient // AuthProvider supplies the registry credentials. AuthProvider() registry.AuthProvider // Events receives the progress of the operation. @@ -152,7 +154,7 @@ func (s *Service) pushComposeArtifact(ctx context.Context, project *types.Projec insecureRegistries = append(insecureRegistries, reference.Domain(named)) } - resolver := oci.NewResolver(s.backend.AuthProvider(), desktop.ProxyTransportFor(ctx, s.backend.APIClient()), insecureRegistries...) + resolver := oci.NewResolver(s.backend.AuthProvider(), desktop.ProxyTransportFor(ctx, s.backend.SystemAPI()), insecureRegistries...) descriptor, didFallback, err := oci.PushManifest(ctx, resolver, named, layers, options.OCIVersion) if err != nil { @@ -359,7 +361,7 @@ func processFile(ctx context.Context, file string, project *types.Project, extFi } func (s *Service) generateImageDigestsOverride(ctx context.Context, project *types.Project) ([]byte, error) { - resolver := registry.ImageDigestResolver(ctx, s.backend.AuthProvider(), s.backend.APIClient()) + resolver := registry.ImageDigestResolver(ctx, s.backend.AuthProvider(), s.backend.DistributionAPI()) project, err := project.WithImagesResolved(resolver) if err != nil { return nil, err diff --git a/internal/publish/publish_test.go b/internal/publish/publish_test.go index a523927c9a..246a6d8826 100644 --- a/internal/publish/publish_test.go +++ b/internal/publish/publish_test.go @@ -53,10 +53,11 @@ type fakeBackend struct { auth registry.AuthProvider } -func (f *fakeBackend) APIClient() client.APIClient { return f.apiClient } -func (f *fakeBackend) AuthProvider() registry.AuthProvider { return f.auth } -func (f *fakeBackend) Events() api.EventProcessor { return progress.Noop{} } -func (f *fakeBackend) DryRun() bool { return false } +func (f *fakeBackend) DistributionAPI() client.DistributionAPIClient { return f.apiClient } +func (f *fakeBackend) SystemAPI() client.SystemAPIClient { return f.apiClient } +func (f *fakeBackend) AuthProvider() registry.AuthProvider { return f.auth } +func (f *fakeBackend) Events() api.EventProcessor { return progress.Noop{} } +func (f *fakeBackend) DryRun() bool { return false } func (f *fakeBackend) Prompt(message string, defaultValue bool) (bool, error) { return f.prompt(message, defaultValue) diff --git a/internal/registry/registry.go b/internal/registry/registry.go index b2afcc62e2..4d495cdce7 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -114,7 +114,7 @@ func EncodedAuth(ref reference.Named, cfg AuthProvider) (string, error) { // compare a running container with a fresh build/pull; never funnel this // resolution through the local content-digest producer, and never pin a // published reference with a per-platform digest. -func ImageDigestResolver(ctx context.Context, auth AuthProvider, c client.APIClient) func(named reference.Named) (digest.Digest, error) { +func ImageDigestResolver(ctx context.Context, auth AuthProvider, c client.DistributionAPIClient) func(named reference.Named) (digest.Digest, error) { return func(named reference.Named) (digest.Digest, error) { encodedAuth, err := EncodedAuth(named, auth) if err != nil { diff --git a/internal/relay/relay.go b/internal/relay/relay.go index 5ea575ab33..1094c88b7d 100644 --- a/internal/relay/relay.go +++ b/internal/relay/relay.go @@ -43,8 +43,12 @@ import ( // Backend is what the relay management needs from the engine. type Backend interface { - // APIClient talks to the Docker daemon. - APIClient() client.APIClient + // ContainerAPI manages the containers of the relays. + ContainerAPI() client.ContainerAPIClient + // NetworkAPI manages the networks the relays are linked through. + NetworkAPI() client.NetworkAPIClient + // ImageAPI pulls the image of the relays. + ImageAPI() client.ImageAPIClient // Events receives the progress of the operations. Events() api.EventProcessor // IsDesktopIntegrationActive tells whether Docker Desktop is the engine. @@ -251,7 +255,7 @@ func (s *Service) listRelayLinkNetworks(ctx context.Context, projectName, servic if serviceName != "" { filters = filters.Add("label", naming.ServiceFilter(serviceName)) } - existing, err := s.backend.APIClient().NetworkList(ctx, client.NetworkListOptions{Filters: filters}) + existing, err := s.backend.NetworkAPI().NetworkList(ctx, client.NetworkListOptions{Filters: filters}) if err != nil { if serviceName == "" { return nil, fmt.Errorf("list relay link networks for project %s: %w", projectName, err) @@ -297,7 +301,7 @@ func (s *Service) ensureRelayLinkNetwork(ctx context.Context, project *types.Pro } } - if _, err := s.backend.APIClient().NetworkCreate(ctx, name, client.NetworkCreateOptions{ + if _, err := s.backend.NetworkAPI().NetworkCreate(ctx, name, client.NetworkCreateOptions{ Labels: map[string]string{ api.ProjectLabel: project.Name, api.ServiceLabel: service.Name, @@ -339,7 +343,7 @@ func (s *Service) ensureRelayLinkNetwork(ctx context.Context, project *types.Pro } func (s *Service) relayLinkNetworkGateway(ctx context.Context, idOrName string) (string, error) { - inspected, err := s.backend.APIClient().NetworkInspect(ctx, idOrName, client.NetworkInspectOptions{}) + inspected, err := s.backend.NetworkAPI().NetworkInspect(ctx, idOrName, client.NetworkInspectOptions{}) if err != nil { return "", fmt.Errorf("inspect relay link network %s: %w", idOrName, err) } @@ -378,7 +382,7 @@ func (s *Service) removeRelayLinkNetwork(ctx context.Context, projectName, servi func (s *Service) RemoveLinkNetwork(ctx context.Context, n network.Summary) error { eventName := "Network " + n.Name s.backend.Events().On(progress.Removing(eventName)) - if _, err := s.backend.APIClient().NetworkRemove(ctx, n.ID, client.NetworkRemoveOptions{}); err != nil { + if _, err := s.backend.NetworkAPI().NetworkRemove(ctx, n.ID, client.NetworkRemoveOptions{}); err != nil { switch { case errdefs.IsNotFound(err): s.backend.Events().On(progress.New(eventName, api.Warning, "No resource found to remove")) @@ -456,12 +460,12 @@ func (s *Service) Ensure(ctx context.Context, project *types.Project, service ty case container.StateCreated, container.StateExited: // Routes unchanged: start the existing container rather than // recreating it. - if _, err := s.backend.APIClient().ContainerStart(ctx, existing.ID, client.ContainerStartOptions{}); err != nil { + if _, err := s.backend.ContainerAPI().ContainerStart(ctx, existing.ID, client.ContainerStartOptions{}); err != nil { return fmt.Errorf("start relay for service %s: %w", service.Name, err) } return nil case container.StatePaused: - if _, err := s.backend.APIClient().ContainerUnpause(ctx, existing.ID, client.ContainerUnpauseOptions{}); err != nil { + if _, err := s.backend.ContainerAPI().ContainerUnpause(ctx, existing.ID, client.ContainerUnpauseOptions{}); err != nil { return fmt.Errorf("unpause relay for service %s: %w", service.Name, err) } return nil @@ -474,7 +478,7 @@ func (s *Service) Ensure(ctx context.Context, project *types.Project, service ty if err := s.waitRelayRemoved(ctx, project.Name, service.Name); err != nil { return err } - } else if _, err := s.backend.APIClient().ContainerRemove(ctx, existing.ID, client.ContainerRemoveOptions{Force: true}); err != nil && !errdefs.IsNotFound(err) { + } else if _, err := s.backend.ContainerAPI().ContainerRemove(ctx, existing.ID, client.ContainerRemoveOptions{Force: true}); err != nil && !errdefs.IsNotFound(err) { return fmt.Errorf("remove stale relay for service %s: %w", service.Name, err) } } @@ -484,7 +488,7 @@ func (s *Service) Ensure(ctx context.Context, project *types.Project, service ty if err != nil { return err } - if _, err := s.backend.APIClient().ContainerStart(ctx, id, client.ContainerStartOptions{}); err != nil { + if _, err := s.backend.ContainerAPI().ContainerStart(ctx, id, client.ContainerStartOptions{}); err != nil { return fmt.Errorf("start relay for service %s: %w", service.Name, err) } s.backend.Events().On(progress.Created("Relay " + name)) @@ -512,7 +516,7 @@ func (s *Service) ensureRelayNetworks(ctx context.Context, project *types.Projec if connected[netName] { continue } - if _, err := s.backend.APIClient().NetworkConnect(ctx, netName, client.NetworkConnectOptions{ + if _, err := s.backend.NetworkAPI().NetworkConnect(ctx, netName, client.NetworkConnectOptions{ Container: existing.ID, EndpointConfig: &network.EndpointSettings{Aliases: []string{service.Name}}, }); err != nil && !errdefs.IsConflict(err) { @@ -524,7 +528,7 @@ func (s *Service) ensureRelayNetworks(ctx context.Context, project *types.Projec } } if linkNetwork != "" && !connected[linkNetwork] { - if _, err := s.backend.APIClient().NetworkConnect(ctx, linkNetwork, client.NetworkConnectOptions{ + if _, err := s.backend.NetworkAPI().NetworkConnect(ctx, linkNetwork, client.NetworkConnectOptions{ Container: existing.ID, }); err != nil && !errdefs.IsConflict(err) { return fmt.Errorf("connect relay for service %s to its relay link network: %w", service.Name, err) @@ -592,7 +596,7 @@ func (s *Service) Remove(ctx context.Context, projectName, serviceName string) e if err := s.waitRelayRemoved(ctx, projectName, serviceName); err != nil { return err } - } else if _, err := s.backend.APIClient().ContainerRemove(ctx, existing.ID, client.ContainerRemoveOptions{Force: true}); err != nil && !errdefs.IsNotFound(err) { + } else if _, err := s.backend.ContainerAPI().ContainerRemove(ctx, existing.ID, client.ContainerRemoveOptions{Force: true}); err != nil && !errdefs.IsNotFound(err) { return fmt.Errorf("remove stale relay for service %s: %w", serviceName, err) } s.backend.Events().On(progress.Removed(eventID)) @@ -612,7 +616,7 @@ func (s *Service) findRelayContainer(ctx context.Context, projectName, serviceNa f := naming.ProjectFilter(projectName) f.Add("label", naming.ServiceFilter(serviceName)) f.Add("label", api.RelayLabel) - result, err := s.backend.APIClient().ContainerList(ctx, client.ContainerListOptions{ + result, err := s.backend.ContainerAPI().ContainerList(ctx, client.ContainerListOptions{ All: true, Filters: f, }) @@ -677,7 +681,7 @@ func (s *Service) createRelayContainer(ctx context.Context, project *types.Proje }, } - created, err := s.backend.APIClient().ContainerCreate(ctx, client.ContainerCreateOptions{ + created, err := s.backend.ContainerAPI().ContainerCreate(ctx, client.ContainerCreateOptions{ Name: name, Config: config, HostConfig: hostConfig, @@ -687,7 +691,7 @@ func (s *Service) createRelayContainer(ctx context.Context, project *types.Proje if err := s.pullRelayImage(ctx); err != nil { return "", err } - created, err = s.backend.APIClient().ContainerCreate(ctx, client.ContainerCreateOptions{ + created, err = s.backend.ContainerAPI().ContainerCreate(ctx, client.ContainerCreateOptions{ Name: name, Config: config, HostConfig: hostConfig, @@ -700,14 +704,14 @@ func (s *Service) createRelayContainer(ctx context.Context, project *types.Proje for _, key := range networkKeys[1:] { netName := project.Networks[key].Name - if _, err := s.backend.APIClient().NetworkConnect(ctx, netName, client.NetworkConnectOptions{ + if _, err := s.backend.NetworkAPI().NetworkConnect(ctx, netName, client.NetworkConnectOptions{ Container: created.ID, EndpointConfig: endpointSettings(netName), }); err != nil { // remove the half-connected container: left in place (with its // restart policy) it would serve only a subset of the consumers' // networks, and its identity would shield it from recreation - if _, rmErr := s.backend.APIClient().ContainerRemove(ctx, created.ID, client.ContainerRemoveOptions{Force: true}); rmErr != nil { + if _, rmErr := s.backend.ContainerAPI().ContainerRemove(ctx, created.ID, client.ContainerRemoveOptions{Force: true}); rmErr != nil { logrus.Warnf("removing half-connected relay %s: %v", name, rmErr) } return "", fmt.Errorf("connect relay for service %s to network %s: %w", service.Name, netName, err) @@ -717,8 +721,8 @@ func (s *Service) createRelayContainer(ctx context.Context, project *types.Proje // no alias: nothing addresses the relay by name on this network, it // exists purely so the relay can reach the provider (see // ensureRelayLinkNetwork). - if _, err := s.backend.APIClient().NetworkConnect(ctx, linkNetwork, client.NetworkConnectOptions{Container: created.ID}); err != nil { - if _, rmErr := s.backend.APIClient().ContainerRemove(ctx, created.ID, client.ContainerRemoveOptions{Force: true}); rmErr != nil { + if _, err := s.backend.NetworkAPI().NetworkConnect(ctx, linkNetwork, client.NetworkConnectOptions{Container: created.ID}); err != nil { + if _, rmErr := s.backend.ContainerAPI().ContainerRemove(ctx, created.ID, client.ContainerRemoveOptions{Force: true}); rmErr != nil { logrus.Warnf("removing half-connected relay %s: %v", name, rmErr) } return "", fmt.Errorf("connect relay for service %s to its relay link network: %w", service.Name, err) @@ -730,7 +734,7 @@ func (s *Service) createRelayContainer(ctx context.Context, project *types.Proje func (s *Service) pullRelayImage(ctx context.Context) error { image := relayImage() s.backend.Events().On(progress.New(image, api.Working, "Pulling")) - response, err := s.backend.APIClient().ImagePull(ctx, image, client.ImagePullOptions{}) + response, err := s.backend.ImageAPI().ImagePull(ctx, image, client.ImagePullOptions{}) if err != nil { return fmt.Errorf("pull %s: %w", image, err) } diff --git a/internal/relay/relay_test.go b/internal/relay/relay_test.go index 8cf07e648b..b07a2ba517 100644 --- a/internal/relay/relay_test.go +++ b/internal/relay/relay_test.go @@ -40,8 +40,10 @@ type testBackend struct { apiClient client.APIClient } -func (b testBackend) APIClient() client.APIClient { return b.apiClient } -func (testBackend) Events() api.EventProcessor { return progress.Noop{} } +func (b testBackend) ContainerAPI() client.ContainerAPIClient { return b.apiClient } +func (b testBackend) NetworkAPI() client.NetworkAPIClient { return b.apiClient } +func (b testBackend) ImageAPI() client.ImageAPIClient { return b.apiClient } +func (testBackend) Events() api.EventProcessor { return progress.Noop{} } func (testBackend) IsDesktopIntegrationActive(context.Context) (bool, error) { return false, nil } diff --git a/pkg/compose/build.go b/pkg/compose/build.go index b849621087..a518522118 100644 --- a/pkg/compose/build.go +++ b/pkg/compose/build.go @@ -26,6 +26,7 @@ import ( "github.com/compose-spec/compose-go/v2/types" "github.com/docker/cli/cli/command" + "github.com/moby/moby/client" "github.com/sirupsen/logrus" "github.com/docker/compose/v5/internal/build" @@ -252,6 +253,10 @@ func (b buildBackend) CLI() command.Cli { return b.s.dockerCli } +func (b buildBackend) ImageBuildAPI() client.ImageBuildAPIClient { + return b.s.apiClient() +} + func (b buildBackend) Events() api.EventProcessor { return b.s.events } diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index a5b94a9c7f..630eee40cc 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -171,6 +171,11 @@ func WithMaxConcurrency(maxConcurrency int) Option { // WithDryRun configure Compose to run without actually applying changes func WithDryRun(s *composeService) error { + if s.engine != nil { + // The dry-run client wraps the client of the Docker CLI: it would leave the + // engine handed to the service, and the calls made on it, out of the dry run. + return errors.New("dry-run cannot be combined with an injected engine") + } s.dryRun = true cli, err := command.NewDockerCli() if err != nil { @@ -183,7 +188,7 @@ func WithDryRun(s *composeService) error { // keep the level already set (e.g. by --verbose). options.LogLevel = logrus.GetLevel().String() err = cli.Initialize(options, command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) { - return dryrun.NewDryRunClient(s.apiClient(), s.dockerCli) + return dryrun.NewDryRunClient(s.dockerCli.Client(), s.dockerCli) })) if err != nil { return err @@ -211,6 +216,15 @@ func WithEventProcessor(bus api.EventProcessor) Option { } type composeService struct { + // engine is the Docker Engine API the service works against. When unset, it is + // the client of dockerCli, which only connects on first use: a command that + // never reaches the engine (`compose config`) must not open the connection. + // WithDryRun refuses a service holding one: it only knows how to wrap the + // client of dockerCli. + engine client.APIClient + // dockerCli serves what is bound to a terminal and its plugins: the standard + // streams, the config file, buildx and the model plugin, attach and exec. + // The engine is not among them: it goes through the engine field. dockerCli command.Cli // prompt is used to interact with user and confirm actions prompt Prompt @@ -264,8 +278,13 @@ type composeService struct { // will get cleaned up at about the same time regardless even if not invoked. func (s *composeService) Close() error { var errs []error - if s.dockerCli != nil { - errs = append(errs, s.apiClient().Close()) + switch { + case s.engine != nil: + // The service owns the engine it was handed. The client of dockerCli stays + // with the Docker CLI, which closes it with the rest of its resources. + errs = append(errs, s.engine.Close()) + case s.dockerCli != nil: + errs = append(errs, s.dockerCli.Client().Close()) } if s.jobsExtClient != nil { errs = append(errs, s.jobsExtClient.Close()) @@ -274,6 +293,9 @@ func (s *composeService) Close() error { } func (s *composeService) apiClient() client.APIClient { + if s.engine != nil { + return s.engine + } return s.dockerCli.Client() } diff --git a/pkg/compose/compose_test.go b/pkg/compose/compose_test.go index 04c8c3b0cd..710fbf4574 100644 --- a/pkg/compose/compose_test.go +++ b/pkg/compose/compose_test.go @@ -22,11 +22,13 @@ import ( "github.com/docker/cli/cli/config/configfile" "github.com/moby/moby/api/types/swarm" + "github.com/moby/moby/api/types/volume" "github.com/moby/moby/client" "github.com/sirupsen/logrus" "go.uber.org/mock/gomock" "gotest.tools/v3/assert" + "github.com/docker/compose/v5/pkg/api" "github.com/docker/compose/v5/pkg/mocks" ) @@ -90,3 +92,42 @@ func TestWithDryRunKeepsLogLevel(t *testing.T) { assert.NilError(t, WithDryRun(&composeService{dockerCli: cli})) assert.Equal(t, logrus.GetLevel(), logrus.TraceLevel) } + +// TestEngineAPIIsReachedWithoutDockerCLI guards that the engine API is reached +// through the engine alone: a service handed an engine runs its operations and +// releases it on Close without ever asking the Docker CLI, which a mock with no +// expectation would fail on. +func TestEngineAPIIsReachedWithoutDockerCLI(t *testing.T) { + mockCtrl := gomock.NewController(t) + apiClient := mocks.NewMockAPIClient(mockCtrl) + svc := &composeService{ + engine: apiClient, + dockerCli: mocks.NewMockCli(mockCtrl), + } + + apiClient.EXPECT().VolumeList(gomock.Any(), gomock.Any()).Return(client.VolumeListResult{ + Items: []volume.Volume{{ + Name: "proj_data", + Driver: "local", + Labels: map[string]string{api.VolumeLabel: "data"}, + }}, + }, nil) + volumes, err := svc.actualVolumes(t.Context(), "proj") + assert.NilError(t, err) + assert.Equal(t, volumes["data"].Name, "proj_data") + + apiClient.EXPECT().Close().Return(nil) + assert.NilError(t, svc.Close()) +} + +// TestWithDryRunRefusesInjectedEngine guards that a dry run is never silently +// skipped: the dry-run client only wraps the client of the Docker CLI, so a +// service holding an engine of its own cannot honor it and says so. +func TestWithDryRunRefusesInjectedEngine(t *testing.T) { + mockCtrl := gomock.NewController(t) + svc := &composeService{engine: mocks.NewMockAPIClient(mockCtrl)} + + err := WithDryRun(svc) + assert.ErrorContains(t, err, "injected engine") + assert.Assert(t, !svc.dryRun) +} diff --git a/pkg/compose/engine.go b/pkg/compose/engine.go index b9140cac56..65eabf6a8d 100644 --- a/pkg/compose/engine.go +++ b/pkg/compose/engine.go @@ -31,7 +31,19 @@ type engineBackend struct { s *composeService } -func (b engineBackend) APIClient() client.APIClient { +func (b engineBackend) ContainerAPI() client.ContainerAPIClient { + return b.s.apiClient() +} + +func (b engineBackend) ExecAPI() client.ExecAPIClient { + return b.s.apiClient() +} + +func (b engineBackend) SystemAPI() client.SystemAPIClient { + return b.s.apiClient() +} + +func (b engineBackend) VolumeAPI() client.VolumeAPIClient { return b.s.apiClient() } diff --git a/pkg/compose/images.go b/pkg/compose/images.go index 241fa5f4ca..be7f80e9d0 100644 --- a/pkg/compose/images.go +++ b/pkg/compose/images.go @@ -95,7 +95,11 @@ type imagesBackend struct { s *composeService } -func (b imagesBackend) APIClient() client.APIClient { +func (b imagesBackend) ContainerAPI() client.ContainerAPIClient { + return b.s.apiClient() +} + +func (b imagesBackend) ImageAPI() client.ImageAPIClient { return b.s.apiClient() } diff --git a/pkg/compose/images_test.go b/pkg/compose/images_test.go index a541beb741..9929dfec13 100644 --- a/pkg/compose/images_test.go +++ b/pkg/compose/images_test.go @@ -225,7 +225,7 @@ func TestImagesBackendWiring(t *testing.T) { b := imagesBackend{s} assert.Equal(t, b.MaxConcurrency(), 7) - assert.Assert(t, b.APIClient() == client.APIClient(api)) + assert.Assert(t, b.ImageAPI() == client.ImageAPIClient(api)) version, err := b.RuntimeAPIVersion(t.Context()) assert.NilError(t, err) assert.Equal(t, version, "1.48") diff --git a/pkg/compose/monitor_test.go b/pkg/compose/monitor_test.go index d0ce2ae3ad..9b197cd332 100644 --- a/pkg/compose/monitor_test.go +++ b/pkg/compose/monitor_test.go @@ -114,7 +114,7 @@ func expectEventStream(apiClient *mocks.MockAPIClient, initial []container.Summa func TestMonitorStartLifecycle(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") got := recordEvents(m) messages, _ := expectEventStream(apiClient, []container.Summary{ @@ -165,7 +165,7 @@ func TestMonitorStartLifecycle(t *testing.T) { func TestMonitorStartServiceFilter(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") m.WithServices([]string{"web"}) got := recordEvents(m) @@ -191,7 +191,7 @@ func TestMonitorStartServiceFilter(t *testing.T) { func TestMonitorStartNoContainers(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") expectEventStream(apiClient, nil, 1) @@ -201,7 +201,7 @@ func TestMonitorStartNoContainers(t *testing.T) { func TestMonitorStartEventsError(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") _, errs := expectEventStream(apiClient, []container.Summary{ {ID: "c1", Labels: map[string]string{api.ServiceLabel: "db"}}, @@ -216,7 +216,7 @@ func TestMonitorStartEventsError(t *testing.T) { func TestMonitorStartContextCancelled(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") expectEventStream(apiClient, []container.Summary{ {ID: "c1", Labels: map[string]string{api.ServiceLabel: "db"}}, @@ -231,7 +231,7 @@ func TestMonitorStartContextCancelled(t *testing.T) { func TestMonitorStartBadExitCode(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") messages, _ := expectEventStream(apiClient, []container.Summary{ {ID: "c1", Labels: map[string]string{api.ServiceLabel: "db"}}, @@ -292,7 +292,7 @@ func newMonitorTestFixtureListing(t *testing.T, initial []container.Summary, lis return client.ContainerListResult{Items: initial}, nil }) - m := logstream.NewMonitor(apiMock, strings.ToLower(testProject)) + m := logstream.NewMonitor(apiMock, apiMock, strings.ToLower(testProject)) return m, apiMock } @@ -486,7 +486,7 @@ func TestMonitorStopBeforeDiePreservesExitCode(t *testing.T) { // entry's effect on target's following start event be observed. func TestMonitorDieClearsStaleRestartingEntry(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") got := recordEvents(m) messages, _ := expectEventStream(apiClient, []container.Summary{ @@ -653,7 +653,7 @@ func monitorWithEventLog(t *testing.T, listed []container.Summary, during func(* DoAndReturn(func(_ context.Context, opts client.EventsListOptions) client.EventsResult { return log.subscribe(t, opts, live...) }) - return logstream.NewMonitor(apiClient, "p"), apiClient, log + return logstream.NewMonitor(apiClient, apiClient, "p"), apiClient, log } // startWithin runs the monitor and fails the test when it doesn't terminate @@ -889,7 +889,7 @@ func TestMonitorStartFailsWithoutDaemonTime(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) apiClient.EXPECT().Info(gomock.Any(), gomock.Any()).Return( client.SystemInfoResult{Info: system.Info{SystemTime: tt.systemTime}}, nil) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") err := m.Start(t.Context()) @@ -943,7 +943,7 @@ func TestMonitorStartWithoutInfoSubscribesLive(t *testing.T) { func TestMonitorStartCanceledBeforeDaemonTime(t *testing.T) { apiClient := mocks.NewMockAPIClient(gomock.NewController(t)) apiClient.EXPECT().Info(gomock.Any(), gomock.Any()).Return(client.SystemInfoResult{}, context.Canceled) - m := logstream.NewMonitor(apiClient, "p") + m := logstream.NewMonitor(apiClient, apiClient, "p") ctx, cancel := context.WithCancel(t.Context()) cancel() diff --git a/pkg/compose/projectloader.go b/pkg/compose/projectloader.go index 049a4f99c4..189ae1bcca 100644 --- a/pkg/compose/projectloader.go +++ b/pkg/compose/projectloader.go @@ -51,7 +51,11 @@ type projectLoaderBackend struct { s *composeService } -func (b projectLoaderBackend) APIClient() client.APIClient { +func (b projectLoaderBackend) ContainerAPI() client.ContainerAPIClient { + return b.s.apiClient() +} + +func (b projectLoaderBackend) NetworkAPI() client.NetworkAPIClient { return b.s.apiClient() } diff --git a/pkg/compose/publish.go b/pkg/compose/publish.go index 3bb6e63337..af0e0d3431 100644 --- a/pkg/compose/publish.go +++ b/pkg/compose/publish.go @@ -41,7 +41,11 @@ type publishBackend struct { s *composeService } -func (b publishBackend) APIClient() client.APIClient { +func (b publishBackend) DistributionAPI() client.DistributionAPIClient { + return b.s.apiClient() +} + +func (b publishBackend) SystemAPI() client.SystemAPIClient { return b.s.apiClient() } diff --git a/pkg/compose/publish_test.go b/pkg/compose/publish_test.go index 6feaea75c7..ecb91886f1 100644 --- a/pkg/compose/publish_test.go +++ b/pkg/compose/publish_test.go @@ -42,7 +42,7 @@ func TestPublishBackendWiring(t *testing.T) { b := publishBackend{s} t.Run("service accessors", func(t *testing.T) { - assert.Assert(t, b.APIClient() == client.APIClient(api)) + assert.Assert(t, b.DistributionAPI() == client.DistributionAPIClient(api)) assert.Assert(t, b.AuthProvider() == auth) assert.Assert(t, b.Events() == compose.EventProcessor(events)) }) diff --git a/pkg/compose/relay.go b/pkg/compose/relay.go index 5cb6d4fe86..4ad874467f 100644 --- a/pkg/compose/relay.go +++ b/pkg/compose/relay.go @@ -67,7 +67,15 @@ type relayBackend struct { s *composeService } -func (b relayBackend) APIClient() client.APIClient { +func (b relayBackend) ContainerAPI() client.ContainerAPIClient { + return b.s.apiClient() +} + +func (b relayBackend) ImageAPI() client.ImageAPIClient { + return b.s.apiClient() +} + +func (b relayBackend) NetworkAPI() client.NetworkAPIClient { return b.s.apiClient() } diff --git a/pkg/compose/up.go b/pkg/compose/up.go index 8d1038d251..eef5b2b322 100644 --- a/pkg/compose/up.go +++ b/pkg/compose/up.go @@ -332,7 +332,8 @@ func (s *composeService) runInteractiveUp(ctx context.Context, created *createdU } } - monitor := logstream.NewMonitor(s.apiClient(), project.Name) + engine := s.apiClient() + monitor := logstream.NewMonitor(engine, engine, project.Name) if len(options.Start.Services) > 0 { monitor.WithServices(options.Start.Services) } else {