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

Commit 796d08d

Browse files
authored
Merge pull request #1793 from lorenrh/il-102-links
2 parents 399f6cd + b5d3eda commit 796d08d

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

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

+2
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

+7-2
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

+67-5
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"
@@ -314,11 +315,23 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
314315
if err != nil {
315316
return err
316317
}
318+
inspectedContainer, err := s.apiClient.ContainerInspect(ctx, created.ID)
319+
if err != nil {
320+
return err
321+
}
317322
createdContainer := moby.Container{
318-
ID: created.ID,
319-
Labels: containerConfig.Labels,
323+
ID: inspectedContainer.ID,
324+
Labels: inspectedContainer.Config.Labels,
325+
Names: []string{inspectedContainer.Name},
326+
NetworkSettings: &moby.SummaryNetworkSettings{
327+
Networks: inspectedContainer.NetworkSettings.Networks,
328+
},
320329
}
321330
cState.Add(createdContainer)
331+
links, err := s.getLinks(ctx, service)
332+
if err != nil {
333+
return err
334+
}
322335
for _, netName := range service.NetworksByPriority() {
323336
netwrk := project.Networks[netName]
324337
cfg := service.Networks[netName]
@@ -329,16 +342,33 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
329342
aliases = append(aliases, cfg.Aliases...)
330343
}
331344
}
332-
333-
err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, aliases...)
345+
if val, ok := createdContainer.NetworkSettings.Networks[netwrk.Name]; ok {
346+
if shortIDAliasExists(createdContainer.ID, val.Aliases...) {
347+
continue
348+
}
349+
err := s.apiClient.NetworkDisconnect(ctx, netwrk.Name, createdContainer.ID, false)
350+
if err != nil {
351+
return err
352+
}
353+
}
354+
err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, links, aliases...)
334355
if err != nil {
335356
return err
336357
}
337358
}
338359
return nil
339360
}
340361

341-
func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, aliases ...string) error {
362+
func shortIDAliasExists(containerID string, aliases ...string) bool {
363+
for _, alias := range aliases {
364+
if alias == containerID[:12] {
365+
return true
366+
}
367+
}
368+
return false
369+
}
370+
371+
func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, links []string, aliases ...string) error {
342372
var (
343373
ipv4ddress string
344374
ipv6Address string
@@ -351,13 +381,45 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
351381
Aliases: aliases,
352382
IPAddress: ipv4ddress,
353383
GlobalIPv6Address: ipv6Address,
384+
Links: links,
354385
})
355386
if err != nil {
356387
return err
357388
}
358389
return nil
359390
}
360391

392+
func (s *composeService) getLinks(ctx context.Context, service types.ServiceConfig) ([]string, error) {
393+
cState, err := GetContextContainerState(ctx)
394+
if err != nil {
395+
return nil, err
396+
}
397+
links := []string{}
398+
for _, serviceLink := range service.Links {
399+
s := strings.Split(serviceLink, ":")
400+
serviceName := serviceLink
401+
serviceAlias := ""
402+
if len(s) == 2 {
403+
serviceName = s[0]
404+
serviceAlias = s[1]
405+
}
406+
containers := cState.GetContainers()
407+
depServiceContainers := containers.filter(isService(serviceName))
408+
for _, container := range depServiceContainers {
409+
name := getCanonicalContainerName(container)
410+
if serviceAlias != "" {
411+
links = append(links,
412+
fmt.Sprintf("%s:%s", name, serviceAlias))
413+
}
414+
links = append(links,
415+
fmt.Sprintf("%s:%s", name, name),
416+
fmt.Sprintf("%s:%s", name, getContainerNameWithoutProject(container)))
417+
}
418+
}
419+
links = append(links, service.ExternalLinks...)
420+
return links, nil
421+
}
422+
361423
func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) {
362424
containers, err := s.getContainers(ctx, project.Name, oneOffExclude, false, service)
363425
if err != nil {

0 commit comments

Comments
 (0)