Fix advanced options for backward compatibility

For backward compatibility: if no custom options are provided for the network,
and only a single network is specified, omit the endpoint-configuration
on the client (the daemon will still create it when creating the container)

This fixes an issue on older versions of legacy Swarm, which did not support
`NetworkingConfig.EndpointConfig`.

This was introduced in 5bc09639cc (#1767)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-05-23 21:27:02 +02:00
parent b45b4b28f9
commit 4d7e6bf629
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 12 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path" "path"
"reflect"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -707,6 +708,15 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*networktypes.Endpoin
if _, ok := endpoints[n.Target]; ok { if _, ok := endpoints[n.Target]; ok {
return nil, errdefs.InvalidParameter(errors.Errorf("network %q is specified multiple times", n.Target)) return nil, errdefs.InvalidParameter(errors.Errorf("network %q is specified multiple times", n.Target))
} }
// For backward compatibility: if no custom options are provided for the network,
// and only a single network is specified, omit the endpoint-configuration
// on the client (the daemon will still create it when creating the container)
if i == 0 && len(copts.netMode.Value()) == 1 {
if ep == nil || reflect.DeepEqual(*ep, networktypes.EndpointSettings{}) {
continue
}
}
endpoints[n.Target] = ep endpoints[n.Target] = ep
} }
if hasUserDefined && hasNonUserDefined { if hasUserDefined && hasNonUserDefined {

View File

@ -401,13 +401,13 @@ func TestParseNetworkConfig(t *testing.T) {
{ {
name: "single-network-legacy", name: "single-network-legacy",
flags: []string{"--network", "net1"}, flags: []string{"--network", "net1"},
expected: map[string]*networktypes.EndpointSettings{"net1": {}}, expected: map[string]*networktypes.EndpointSettings{},
expectedCfg: container.HostConfig{NetworkMode: "net1"}, expectedCfg: container.HostConfig{NetworkMode: "net1"},
}, },
{ {
name: "single-network-advanced", name: "single-network-advanced",
flags: []string{"--network", "name=net1"}, flags: []string{"--network", "name=net1"},
expected: map[string]*networktypes.EndpointSettings{"net1": {}}, expected: map[string]*networktypes.EndpointSettings{},
expectedCfg: container.HostConfig{NetworkMode: "net1"}, expectedCfg: container.HostConfig{NetworkMode: "net1"},
}, },
{ {