Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit f8bf0ac

Browse files
authored
Merge pull request #1018 from gtardif/compose_moby_ctx
`compose up` and other compose commands running on “Moby” context type.
2 parents 3190456 + fea0cf8 commit f8bf0ac

29 files changed

+38
-89
lines changed

Diff for: .github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ jobs:
6060

6161
- name: Test
6262
env:
63-
BUILD_TAGS: example,local
63+
BUILD_TAGS: example
6464
run: make -f builder.Makefile test
6565

6666
- name: Build for local E2E
6767
env:
68-
BUILD_TAGS: example,local,e2e
68+
BUILD_TAGS: example,e2e
6969
run: make -f builder.Makefile cli
7070

7171
- name: E2E Test

Diff for: Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protos: ## Generate go code from .proto files
3939
cli: ## Compile the cli
4040
@docker build . --target cli \
4141
--platform local \
42-
--build-arg BUILD_TAGS=example,local,e2e \
42+
--build-arg BUILD_TAGS=example,e2e \
4343
--build-arg GIT_TAG=$(GIT_TAG) \
4444
--output ./bin
4545

@@ -63,7 +63,7 @@ cross: ## Compile the CLI for linux, darwin and windows
6363

6464
test: ## Run unit tests
6565
@docker build . \
66-
--build-arg BUILD_TAGS=example,local \
66+
--build-arg BUILD_TAGS=example \
6767
--build-arg GIT_TAG=$(GIT_TAG) \
6868
--target test
6969

@@ -72,7 +72,7 @@ cache-clear: ## Clear the builder cache
7272

7373
lint: ## run linter(s)
7474
@docker build . \
75-
--build-arg BUILD_TAGS=example,local,e2e \
75+
--build-arg BUILD_TAGS=example,e2e \
7676
--build-arg GIT_TAG=$(GIT_TAG) \
7777
--target lint
7878

Diff for: api/client/client.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ import (
3232

3333
// New returns a backend client associated with current context
3434
func New(ctx context.Context) (*Client, error) {
35+
return newWithDefaultBackend(ctx, "")
36+
}
37+
38+
// NewWithDefaultLocalBackend returns a backend client associated with current context or local backend if on default context type
39+
func NewWithDefaultLocalBackend(ctx context.Context) (*Client, error) {
40+
return newWithDefaultBackend(ctx, store.LocalContextType)
41+
}
42+
43+
func newWithDefaultBackend(ctx context.Context, defaultBackend string) (*Client, error) {
3544
currentContext := apicontext.CurrentContext(ctx)
3645
s := store.ContextStore(ctx)
3746

@@ -40,7 +49,12 @@ func New(ctx context.Context) (*Client, error) {
4049
return nil, err
4150
}
4251

43-
service, err := backend.Get(ctx, cc.Type())
52+
backendName := cc.Type()
53+
if backendName == store.DefaultContextType && defaultBackend != "" {
54+
backendName = defaultBackend
55+
}
56+
57+
service, err := backend.Get(ctx, backendName)
4458
if err != nil {
4559
return nil, err
4660
}

Diff for: cli/cmd/compose/build.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func buildCommand() *cobra.Command {
4545
}
4646

4747
func runBuild(ctx context.Context, opts buildOptions, services []string) error {
48-
c, err := client.New(ctx)
48+
c, err := client.NewWithDefaultLocalBackend(ctx)
4949
if err != nil {
5050
return err
5151
}

Diff for: cli/cmd/compose/compose.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717
package compose
1818

1919
import (
20-
"context"
21-
2220
"github.com/compose-spec/compose-go/cli"
2321
"github.com/compose-spec/compose-go/types"
2422
"github.com/spf13/cobra"
2523
"github.com/spf13/pflag"
2624

27-
"github.com/docker/compose-cli/api/client"
2825
"github.com/docker/compose-cli/context/store"
29-
"github.com/docker/compose-cli/errdefs"
3026
)
3127

3228
type composeOptions struct {
@@ -76,9 +72,6 @@ func Command(contextType string) *cobra.Command {
7672
command := &cobra.Command{
7773
Short: "Docker Compose",
7874
Use: "compose",
79-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
80-
return checkComposeSupport(cmd.Context())
81-
},
8275
}
8376

8477
command.AddCommand(
@@ -90,7 +83,7 @@ func Command(contextType string) *cobra.Command {
9083
convertCommand(),
9184
)
9285

93-
if contextType == store.LocalContextType {
86+
if contextType == store.LocalContextType || contextType == store.DefaultContextType {
9487
command.AddCommand(
9588
buildCommand(),
9689
pushCommand(),
@@ -101,15 +94,6 @@ func Command(contextType string) *cobra.Command {
10194
return command
10295
}
10396

104-
func checkComposeSupport(ctx context.Context) error {
105-
_, err := client.New(ctx)
106-
if errdefs.IsNotFoundError(err) {
107-
return errdefs.ErrNotImplemented
108-
}
109-
110-
return err
111-
}
112-
11397
//
11498
func filter(project *types.Project, services []string) error {
11599
if len(services) == 0 {

Diff for: cli/cmd/compose/convert.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func convertCommand() *cobra.Command {
4646

4747
func runConvert(ctx context.Context, opts composeOptions) error {
4848
var json []byte
49-
c, err := client.New(ctx)
49+
c, err := client.NewWithDefaultLocalBackend(ctx)
5050
if err != nil {
5151
return err
5252
}

Diff for: cli/cmd/compose/down.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func downCommand() *cobra.Command {
4141
}
4242

4343
func runDown(ctx context.Context, opts composeOptions) error {
44-
c, err := client.New(ctx)
44+
c, err := client.NewWithDefaultLocalBackend(ctx)
4545
if err != nil {
4646
return err
4747
}

Diff for: cli/cmd/compose/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func listCommand() *cobra.Command {
4343
}
4444

4545
func runList(ctx context.Context, opts composeOptions) error {
46-
c, err := client.New(ctx)
46+
c, err := client.NewWithDefaultLocalBackend(ctx)
4747
if err != nil {
4848
return err
4949
}

Diff for: cli/cmd/compose/logs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func logsCommand() *cobra.Command {
4141
}
4242

4343
func runLogs(ctx context.Context, opts composeOptions) error {
44-
c, err := client.New(ctx)
44+
c, err := client.NewWithDefaultLocalBackend(ctx)
4545
if err != nil {
4646
return err
4747
}

Diff for: cli/cmd/compose/ps.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func psCommand() *cobra.Command {
4545
}
4646

4747
func runPs(ctx context.Context, opts composeOptions) error {
48-
c, err := client.New(ctx)
48+
c, err := client.NewWithDefaultLocalBackend(ctx)
4949
if err != nil {
5050
return err
5151
}

Diff for: cli/cmd/compose/pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func pullCommand() *cobra.Command {
4646
}
4747

4848
func runPull(ctx context.Context, opts pullOptions, services []string) error {
49-
c, err := client.New(ctx)
49+
c, err := client.NewWithDefaultLocalBackend(ctx)
5050
if err != nil {
5151
return err
5252
}

Diff for: cli/cmd/compose/push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func pushCommand() *cobra.Command {
4646
}
4747

4848
func runPush(ctx context.Context, opts pushOptions, services []string) error {
49-
c, err := client.New(ctx)
49+
c, err := client.NewWithDefaultLocalBackend(ctx)
5050
if err != nil {
5151
return err
5252
}

Diff for: cli/cmd/compose/up.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func upCommand(contextType string) *cobra.Command {
3838
Use: "up [SERVICE...]",
3939
RunE: func(cmd *cobra.Command, args []string) error {
4040
switch contextType {
41-
case store.LocalContextType:
41+
case store.LocalContextType, store.DefaultContextType:
4242
return runCreateStart(cmd.Context(), opts, args)
4343
default:
4444
return runUp(cmd.Context(), opts, args)
@@ -100,7 +100,7 @@ func runCreateStart(ctx context.Context, opts composeOptions, services []string)
100100
}
101101

102102
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
103-
c, err := client.New(ctx)
103+
c, err := client.NewWithDefaultLocalBackend(ctx)
104104
if err != nil {
105105
return nil, nil, err
106106
}

Diff for: local/backend.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/build.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/compose.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53
@@ -502,7 +500,7 @@ func (s *composeService) Down(ctx context.Context, projectName string) error {
502500
func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, eg *errgroup.Group, filter filters.Args) error {
503501
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
504502
Filters: filter,
505-
All: true,
503+
All: true,
506504
})
507505
if err != nil {
508506
return err
@@ -806,7 +804,7 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i
806804
StopTimeout: toSeconds(s.StopGracePeriod),
807805
}
808806

809-
mountOptions, err := buildContainerMountOptions(p, s, inherit)
807+
mountOptions, err := buildContainerMountOptions(s, inherit)
810808
if err != nil {
811809
return nil, nil, nil, err
812810
}
@@ -853,7 +851,7 @@ func buildContainerBindingOptions(s types.ServiceConfig) nat.PortMap {
853851
return bindings
854852
}
855853

856-
func buildContainerMountOptions(p *types.Project, s types.ServiceConfig, inherit *moby.Container) ([]mount.Mount, error) {
854+
func buildContainerMountOptions(s types.ServiceConfig, inherit *moby.Container) ([]mount.Mount, error) {
857855
mounts := []mount.Mount{}
858856
var inherited []string
859857
if inherit != nil {

Diff for: local/compose_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/container.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/containers.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/convergence.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/convert.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/convert_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/dependencies.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/dependencies_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/e2e/compose_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import (
3030

3131
func TestLocalComposeUp(t *testing.T) {
3232
c := NewParallelE2eCLI(t, binDir)
33-
c.RunDockerCmd("context", "create", "local", "test-context").Assert(t, icmd.Success)
34-
c.RunDockerCmd("context", "use", "test-context").Assert(t, icmd.Success)
3533

3634
const projectName = "compose-e2e-demo"
3735

@@ -54,12 +52,12 @@ func TestLocalComposeUp(t *testing.T) {
5452
output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
5553
assert.Assert(t, strings.Contains(output, `"word":`))
5654

57-
res = c.RunDockerCmd("--context", "default", "network", "ls")
55+
res = c.RunDockerCmd("network", "ls")
5856
res.Assert(t, icmd.Expected{Out: projectName + "_default"})
5957
})
6058

6159
t.Run("check compose labels", func(t *testing.T) {
62-
res := c.RunDockerCmd("--context", "default", "inspect", projectName+"_web_1")
60+
res := c.RunDockerCmd("inspect", projectName+"_web_1")
6361
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
6462
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`})
6563
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`})
@@ -69,7 +67,7 @@ func TestLocalComposeUp(t *testing.T) {
6967
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.service": "web"`})
7068
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version":`})
7169

72-
res = c.RunDockerCmd("--context", "default", "network", "inspect", projectName+"_default")
70+
res = c.RunDockerCmd("network", "inspect", projectName+"_default")
7371
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.network": "default"`})
7472
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": `})
7573
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version": `})
@@ -85,15 +83,13 @@ func TestLocalComposeUp(t *testing.T) {
8583
})
8684

8785
t.Run("check networks after down", func(t *testing.T) {
88-
res := c.RunDockerCmd("--context", "default", "network", "ls")
86+
res := c.RunDockerCmd("network", "ls")
8987
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
9088
})
9189
}
9290

9391
func TestLocalComposeVolume(t *testing.T) {
9492
c := NewParallelE2eCLI(t, binDir)
95-
c.RunDockerCmd("context", "create", "local", "test-context").Assert(t, icmd.Success)
96-
c.RunDockerCmd("context", "use", "test-context").Assert(t, icmd.Success)
9793

9894
const projectName = "compose-e2e-volume"
9995

Diff for: local/labels.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/util.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: local/volumes.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build local
2-
31
/*
42
Copyright 2020 Docker Compose CLI authors
53

Diff for: tests/e2e/e2e_test.go

-17
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,6 @@ func TestMain(m *testing.M) {
4747
os.Exit(exitCode)
4848
}
4949

50-
func TestComposeNotImplemented(t *testing.T) {
51-
c := NewParallelE2eCLI(t, binDir)
52-
res := c.RunDockerCmd("context", "show")
53-
res.Assert(t, icmd.Expected{Out: "default"})
54-
res = c.RunDockerOrExitError("compose", "up")
55-
res.Assert(t, icmd.Expected{
56-
ExitCode: 1,
57-
Err: `Command "compose up" not available in current context (default)`,
58-
})
59-
60-
res = c.RunDockerOrExitError("compose", "-f", "titi.yaml", "up")
61-
res.Assert(t, icmd.Expected{
62-
ExitCode: 1,
63-
Err: `Command "compose up" not available in current context (default)`,
64-
})
65-
}
66-
6750
func TestContextDefault(t *testing.T) {
6851
c := NewParallelE2eCLI(t, binDir)
6952

0 commit comments

Comments
 (0)