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

Commit 769dd23

Browse files
authored
Merge pull request #1625 from ndeloof/network_alisasses
use highest priority network to declare network aliasses
2 parents 63057f6 + 4186cce commit 769dd23

File tree

6 files changed

+55
-20
lines changed

6 files changed

+55
-20
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/awslabs/goformation/v4 v4.15.6
1818
github.com/buger/goterm v1.0.0
1919
github.com/cnabio/cnab-to-oci v0.3.1-beta1
20-
github.com/compose-spec/compose-go v0.0.0-20210427143821-6d1c5982084f
20+
github.com/compose-spec/compose-go v0.0.0-20210503135708-e8ee37c1478c
2121
github.com/containerd/console v1.0.1
2222
github.com/containerd/containerd v1.4.3
2323
github.com/containerd/continuity v0.0.0-20200928162600-f2cc35102c2a // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
308308
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
309309
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
310310
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
311-
github.com/compose-spec/compose-go v0.0.0-20210427143821-6d1c5982084f h1:EJu2tLPcjRTAdFDJZOQj57ehBo+m71Iz6sUf1Q8/BOY=
312-
github.com/compose-spec/compose-go v0.0.0-20210427143821-6d1c5982084f/go.mod h1:6eIT9U2OgdHmkRD6szmqatCrWWEEUSwl/j2iJYH4jLo=
311+
github.com/compose-spec/compose-go v0.0.0-20210503135708-e8ee37c1478c h1:XDrhClIbE/zCDU4KWCYSYmceYZj5EBn3DMhQ7hVvyUs=
312+
github.com/compose-spec/compose-go v0.0.0-20210503135708-e8ee37c1478c/go.mod h1:6eIT9U2OgdHmkRD6szmqatCrWWEEUSwl/j2iJYH4jLo=
313313
github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f/go.mod h1:OApqhQ4XNSNC13gXIwDjhOQxjWa/NxkwZXJ1EvqT0ko=
314314
github.com/containerd/cgroups v0.0.0-20200531161412-0dbf7f05ba59/go.mod h1:pA0z1pT8KYB3TCXK/ocprsh7MAkoW8bZVzPdih9snmM=
315315
github.com/containerd/cgroups v0.0.0-20200710171044-318312a37340 h1:9atoWyI9RtXFwf7UDbme/6M8Ud0rFrx+Q3ZWgSnsxtw=

local/compose/convergence.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,9 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
318318
Labels: containerConfig.Labels,
319319
}
320320
cState.Add(createdContainer)
321-
for netName, cfg := range service.Networks {
321+
for _, netName := range service.NetworksByPriority() {
322322
netwrk := project.Networks[netName]
323+
cfg := service.Networks[netName]
323324
aliases := []string{getContainerName(project.Name, service, number)}
324325
if useNetworkAliases {
325326
aliases = append(aliases, service.Name)

local/compose/create.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,22 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project,
276276
if networkMode == "" {
277277
networkMode = getDefaultNetworkMode(p, service)
278278
}
279+
280+
var networkConfig *network.NetworkingConfig
281+
// TODO use network with highest priority, attribute is missing from compose-go
282+
for _, id := range service.NetworksByPriority() {
283+
net := p.Networks[id]
284+
config := service.Networks[id]
285+
networkConfig = &network.NetworkingConfig{
286+
EndpointsConfig: map[string]*network.EndpointSettings{
287+
net.Name: {
288+
Aliases: getAliases(service, config),
289+
},
290+
},
291+
}
292+
break
293+
}
294+
279295
ipcmode, err := getMode(ctx, service.Name, service.Ipc)
280296
if err != nil {
281297
return nil, nil, nil, err
@@ -328,7 +344,6 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project,
328344
LogConfig: logConfig,
329345
}
330346

331-
networkConfig := buildDefaultNetworkConfig(service, container.NetworkMode(networkMode), getContainerName(p.Name, service, number))
332347
return &containerConfig, &hostConfig, networkConfig, nil
333348
}
334349

@@ -843,21 +858,6 @@ func buildTmpfsOptions(tmpfs *types.ServiceVolumeTmpfs) *mount.TmpfsOptions {
843858
}
844859
}
845860

846-
func buildDefaultNetworkConfig(s types.ServiceConfig, networkMode container.NetworkMode, containerName string) *network.NetworkingConfig {
847-
if len(s.Networks) == 0 {
848-
return nil
849-
}
850-
config := map[string]*network.EndpointSettings{}
851-
net := string(networkMode)
852-
config[net] = &network.EndpointSettings{
853-
Aliases: append(getAliases(s, s.Networks[net]), containerName),
854-
}
855-
856-
return &network.NetworkingConfig{
857-
EndpointsConfig: config,
858-
}
859-
}
860-
861861
func getAliases(s types.ServiceConfig, c *types.ServiceNetworkConfig) []string {
862862
aliases := []string{s.Name}
863863
if c != nil {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
3+
container1:
4+
image: nginx
5+
6+
container2:
7+
image: nginx
8+
networks:
9+
foo:
10+
aliases:
11+
- alias-of-container2
12+
13+
networks:
14+
foo:
15+
name: bar

local/e2e/compose/networks_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,22 @@ func TestNetworks(t *testing.T) {
7070
assert.Assert(t, !strings.Contains(res.Combined(), "microservices"), res.Combined())
7171
})
7272
}
73+
74+
func TestNetworkAliasses(t *testing.T) {
75+
c := NewParallelE2eCLI(t, binDir)
76+
77+
const projectName = "network_alias_e2e"
78+
79+
t.Run("up", func(t *testing.T) {
80+
c.RunDockerCmd("compose", "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "up", "-d")
81+
})
82+
83+
t.Run("curl", func(t *testing.T) {
84+
res := c.RunDockerCmd("compose", "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "exec", "-T", "container1", "curl", "http://alias-of-container2/")
85+
assert.Assert(t, !strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
86+
})
87+
88+
t.Run("down", func(t *testing.T) {
89+
_ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
90+
})
91+
}

0 commit comments

Comments
 (0)