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

Commit b38d35a

Browse files
committed
set container links and external links in network
Signed-off-by: Lorena Rangel <[email protected]>
1 parent 399f6cd commit b38d35a

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

local/e2e/compose/fixtures/network-alias/compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ services:
22

33
container1:
44
image: nginx
5+
links:
6+
- container2:container
57

68
container2:
79
image: nginx

local/e2e/compose/networks_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestNetworks(t *testing.T) {
7171
})
7272
}
7373

74-
func TestNetworkAliasses(t *testing.T) {
74+
func TestNetworkAliassesAndLinks(t *testing.T) {
7575
c := NewParallelE2eCLI(t, binDir)
7676

7777
const projectName = "network_alias_e2e"
@@ -80,11 +80,16 @@ func TestNetworkAliasses(t *testing.T) {
8080
c.RunDockerCmd("compose", "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "up", "-d")
8181
})
8282

83-
t.Run("curl", func(t *testing.T) {
83+
t.Run("curl alias", func(t *testing.T) {
8484
res := c.RunDockerCmd("compose", "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "exec", "-T", "container1", "curl", "http://alias-of-container2/")
8585
assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
8686
})
8787

88+
t.Run("curl links", func(t *testing.T) {
89+
res := c.RunDockerCmd("compose", "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "exec", "-T", "container1", "curl", "container")
90+
assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
91+
})
92+
8893
t.Run("down", func(t *testing.T) {
8994
_ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
9095
})

pkg/compose/convergence.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"strconv"
23+
"strings"
2324
"time"
2425

2526
"github.com/compose-spec/compose-go/types"
@@ -317,8 +318,14 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
317318
createdContainer := moby.Container{
318319
ID: created.ID,
319320
Labels: containerConfig.Labels,
321+
Names: []string{"/" + name},
320322
}
321323
cState.Add(createdContainer)
324+
325+
links, err := s.getLinks(ctx, service)
326+
if err != nil {
327+
return err
328+
}
322329
for _, netName := range service.NetworksByPriority() {
323330
netwrk := project.Networks[netName]
324331
cfg := service.Networks[netName]
@@ -330,15 +337,15 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
330337
}
331338
}
332339

333-
err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, aliases...)
340+
err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, links, aliases...)
334341
if err != nil {
335342
return err
336343
}
337344
}
338345
return nil
339346
}
340347

341-
func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, aliases ...string) error {
348+
func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, links []string, aliases ...string) error {
342349
var (
343350
ipv4ddress string
344351
ipv6Address string
@@ -347,17 +354,54 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
347354
ipv4ddress = cfg.Ipv4Address
348355
ipv6Address = cfg.Ipv6Address
349356
}
350-
err := s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
357+
err := s.apiClient.NetworkDisconnect(ctx, netwrk, id, false)
358+
if err != nil {
359+
return err
360+
}
361+
362+
err = s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
351363
Aliases: aliases,
352364
IPAddress: ipv4ddress,
353365
GlobalIPv6Address: ipv6Address,
366+
Links: links,
354367
})
355368
if err != nil {
356369
return err
357370
}
358371
return nil
359372
}
360373

374+
func (s *composeService) getLinks(ctx context.Context, service types.ServiceConfig) ([]string, error) {
375+
cState, err := GetContextContainerState(ctx)
376+
if err != nil {
377+
return nil, err
378+
}
379+
links := []string{}
380+
for _, serviceLink := range service.Links {
381+
s := strings.Split(serviceLink, ":")
382+
serviceName := serviceLink
383+
serviceAlias := ""
384+
if len(s) == 2 {
385+
serviceName = s[0]
386+
serviceAlias = s[1]
387+
}
388+
containers := cState.GetContainers()
389+
depServiceContainers := containers.filter(isService(serviceName))
390+
for _, container := range depServiceContainers {
391+
name := getCanonicalContainerName(container)
392+
if serviceAlias != "" {
393+
links = append(links,
394+
fmt.Sprintf("%s:%s", name, serviceAlias))
395+
}
396+
links = append(links,
397+
fmt.Sprintf("%s:%s", name, name),
398+
fmt.Sprintf("%s:%s", name, getContainerNameWithoutProject(container)))
399+
}
400+
}
401+
links = append(links, service.ExternalLinks...)
402+
return links, nil
403+
}
404+
361405
func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) {
362406
containers, err := s.getContainers(ctx, project.Name, oneOffExclude, false, service)
363407
if err != nil {

0 commit comments

Comments
 (0)