From 81182fca536124778d722a14814a8858ee14029f Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Thu, 2 Jun 2022 01:53:20 +0200 Subject: [PATCH] Add links to container create request. In v1, links were sent alongside the rest of the container create request, as part of `HostConfig`. In v2, links are usually set on the connect container to network request that happens after the create. However, this only happens if the service has one or more networks defined for it. If the services are configured to use the default bridge network, this request is not made and so links are never configured. Signed-off-by: Laura Brehm --- pkg/compose/create.go | 6 ++++++ pkg/e2e/fixtures/network-links/compose.yaml | 9 +++++++++ pkg/e2e/networks_test.go | 21 ++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 pkg/e2e/fixtures/network-links/compose.yaml diff --git a/pkg/compose/create.go b/pkg/compose/create.go index bc551b167..c70f8d8d4 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -355,6 +355,11 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, volumesFrom = append(volumesFrom, v[len("container:"):]) } + links, err := s.getLinks(ctx, p.Name, service, number) + if err != nil { + return nil, nil, nil, err + } + securityOpts, err := parseSecurityOpts(p, service.SecurityOpt) if err != nil { return nil, nil, nil, err @@ -389,6 +394,7 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, Runtime: service.Runtime, LogConfig: logConfig, GroupAdd: service.GroupAdd, + Links: links, } return &containerConfig, &hostConfig, networkConfig, nil diff --git a/pkg/e2e/fixtures/network-links/compose.yaml b/pkg/e2e/fixtures/network-links/compose.yaml new file mode 100644 index 000000000..c09a33fcd --- /dev/null +++ b/pkg/e2e/fixtures/network-links/compose.yaml @@ -0,0 +1,9 @@ +services: + container1: + image: nginx + network_mode: bridge + container2: + image: nginx + network_mode: bridge + links: + - container1 diff --git a/pkg/e2e/networks_test.go b/pkg/e2e/networks_test.go index 36582dfc7..85893b557 100644 --- a/pkg/e2e/networks_test.go +++ b/pkg/e2e/networks_test.go @@ -69,7 +69,7 @@ func TestNetworks(t *testing.T) { }) } -func TestNetworkAliassesAndLinks(t *testing.T) { +func TestNetworkAliasses(t *testing.T) { c := NewParallelE2eCLI(t, binDir) const projectName = "network_alias_e2e" @@ -93,6 +93,25 @@ func TestNetworkAliassesAndLinks(t *testing.T) { }) } +func TestNetworkLinks(t *testing.T) { + c := NewParallelE2eCLI(t, binDir) + + const projectName = "network_link_e2e" + + t.Run("up", func(t *testing.T) { + c.RunDockerComposeCmd("-f", "./fixtures/network-links/compose.yaml", "--project-name", projectName, "up", "-d") + }) + + t.Run("curl links in default bridge network", func(t *testing.T) { + res := c.RunDockerComposeCmd("-f", "./fixtures/network-links/compose.yaml", "--project-name", projectName, "exec", "-T", "container2", "curl", "http://container1/") + assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout()) + }) + + t.Run("down", func(t *testing.T) { + _ = c.RunDockerComposeCmd("--project-name", projectName, "down") + }) +} + func TestIPAMConfig(t *testing.T) { c := NewParallelE2eCLI(t, binDir)