Skip to content

Commit bdd70c1

Browse files
authored
Merge pull request #5739 from robmry/fix_links_check
Allow '--link' with '--network bridge'
2 parents cc224b8 + 2c35778 commit bdd70c1

2 files changed

Lines changed: 71 additions & 8 deletions

File tree

cli/command/container/opts.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,9 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
825825
n.Aliases = make([]string, copts.aliases.Len())
826826
copy(n.Aliases, copts.aliases.GetAll())
827827
}
828-
if n.Target != "default" && copts.links.Len() > 0 {
828+
// For a user-defined network, "--link" is an endpoint option, it creates an alias. But,
829+
// for the default bridge it defines a legacy-link.
830+
if container.NetworkMode(n.Target).IsUserDefined() && copts.links.Len() > 0 {
829831
n.Links = make([]string, copts.links.Len())
830832
copy(n.Links, copts.links.GetAll())
831833
}

cli/command/container/opts_test.go

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,75 @@ func mustParse(t *testing.T, args string) (*container.Config, *container.HostCon
7272
}
7373

7474
func TestParseRunLinks(t *testing.T) {
75-
if _, hostConfig, _ := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
76-
t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
77-
}
78-
if _, hostConfig, _ := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
79-
t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
75+
tests := []struct {
76+
name string
77+
input string
78+
expHostConfigLinks []string
79+
expNetConfigLinks map[string][]string
80+
}{
81+
// Default bridge - legacy links ...
82+
{
83+
name: "default/onelink",
84+
input: "--link a:b",
85+
expHostConfigLinks: []string{"a:b"},
86+
expNetConfigLinks: map[string][]string{"default": nil},
87+
},
88+
{
89+
name: "default/twolinks",
90+
input: "--link a:b --link c:d",
91+
expHostConfigLinks: []string{"a:b", "c:d"},
92+
expNetConfigLinks: map[string][]string{"default": nil},
93+
},
94+
{
95+
name: "bridge/onelink",
96+
input: "--network bridge --link a:b",
97+
expHostConfigLinks: []string{"a:b"},
98+
// expNetConfigLinks - no EndpointsConfig is created for a single named network with no options set.
99+
// See the "For backward compatibility" comment in parseNetworkOpts().
100+
},
101+
{
102+
name: "default/nolinks",
103+
expNetConfigLinks: map[string][]string{"default": nil},
104+
},
105+
106+
// User-defined bridge - links become DNS aliases ...
107+
{
108+
name: "userdefnet/onelink",
109+
input: "--network userdefnet --link a:b",
110+
expHostConfigLinks: []string{"a:b"},
111+
expNetConfigLinks: map[string][]string{"userdefnet": {"a:b"}},
112+
},
113+
{
114+
name: "userdefnet/twolinks",
115+
input: "--network userdefnet --link a:b --link c:d",
116+
expHostConfigLinks: []string{"a:b", "c:d"},
117+
expNetConfigLinks: map[string][]string{"userdefnet": {"a:b", "c:d"}},
118+
},
119+
{
120+
name: "userdefnet/nolinks",
121+
input: "--network userdefnet",
122+
},
123+
{
124+
// Link options are applied to the first network (and there's no "advanced syntax"
125+
// link key, like "--network name=userdefnet,link=a:b").
126+
name: "links apply to the first network",
127+
input: "--network userdefnet --link a:b --network bar --link c:d",
128+
expHostConfigLinks: []string{"a:b", "c:d"},
129+
expNetConfigLinks: map[string][]string{"userdefnet": {"a:b", "c:d"}, "bar": nil},
130+
},
80131
}
81-
if _, hostConfig, _ := mustParse(t, ""); len(hostConfig.Links) != 0 {
82-
t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
132+
133+
for _, tc := range tests {
134+
t.Run(tc.name, func(t *testing.T) {
135+
_, hostConfig, netConfig := mustParse(t, tc.input)
136+
assert.Check(t, is.DeepEqual(hostConfig.Links, tc.expHostConfigLinks))
137+
assert.Check(t, is.Len(netConfig.EndpointsConfig, len(tc.expNetConfigLinks)))
138+
for netName, expLinks := range tc.expNetConfigLinks {
139+
nc, ok := netConfig.EndpointsConfig[netName]
140+
assert.Assert(t, ok)
141+
assert.Check(t, is.DeepEqual(nc.Links, expLinks))
142+
}
143+
})
83144
}
84145
}
85146

0 commit comments

Comments
 (0)