- Make sure we use the correct network name for external ones.
- Make the default network overridable and only creates networks that
are used by services — so that default network is only created if a
service doesn't declare a network.
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
| ... | ... |
@@ -117,7 +117,9 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo |
| 117 | 117 |
|
| 118 | 118 |
namespace := convert.NewNamespace(opts.namespace) |
| 119 | 119 |
|
| 120 |
- networks, externalNetworks := convert.Networks(namespace, config.Networks) |
|
| 120 |
+ serviceNetworks := getServicesDeclaredNetworks(config.Services) |
|
| 121 |
+ |
|
| 122 |
+ networks, externalNetworks := convert.Networks(namespace, config.Networks, serviceNetworks) |
|
| 121 | 123 |
if err := validateExternalNetworks(ctx, dockerCli, externalNetworks); err != nil {
|
| 122 | 124 |
return err |
| 123 | 125 |
} |
| ... | ... |
@@ -131,6 +133,20 @@ func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deplo |
| 131 | 131 |
return deployServices(ctx, dockerCli, services, namespace, opts.sendRegistryAuth) |
| 132 | 132 |
} |
| 133 | 133 |
|
| 134 |
+func getServicesDeclaredNetworks(serviceConfigs []composetypes.ServiceConfig) map[string]struct{} {
|
|
| 135 |
+ serviceNetworks := map[string]struct{}{}
|
|
| 136 |
+ for _, serviceConfig := range serviceConfigs {
|
|
| 137 |
+ if len(serviceConfig.Networks) == 0 {
|
|
| 138 |
+ serviceNetworks["default"] = struct{}{}
|
|
| 139 |
+ continue |
|
| 140 |
+ } |
|
| 141 |
+ for network := range serviceConfig.Networks {
|
|
| 142 |
+ serviceNetworks[network] = struct{}{}
|
|
| 143 |
+ } |
|
| 144 |
+ } |
|
| 145 |
+ return serviceNetworks |
|
| 146 |
+} |
|
| 147 |
+ |
|
| 134 | 148 |
func propertyWarnings(properties map[string]string) string {
|
| 135 | 149 |
var msgs []string |
| 136 | 150 |
for name, description := range properties {
|
| ... | ... |
@@ -43,20 +43,15 @@ func AddStackLabel(namespace Namespace, labels map[string]string) map[string]str |
| 43 | 43 |
type networkMap map[string]composetypes.NetworkConfig |
| 44 | 44 |
|
| 45 | 45 |
// Networks from the compose-file type to the engine API type |
| 46 |
-func Networks(namespace Namespace, networks networkMap) (map[string]types.NetworkCreate, []string) {
|
|
| 46 |
+func Networks(namespace Namespace, networks networkMap, servicesNetworks map[string]struct{}) (map[string]types.NetworkCreate, []string) {
|
|
| 47 | 47 |
if networks == nil {
|
| 48 | 48 |
networks = make(map[string]composetypes.NetworkConfig) |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 |
- // TODO: only add default network if it's used |
|
| 52 |
- if _, ok := networks["default"]; !ok {
|
|
| 53 |
- networks["default"] = composetypes.NetworkConfig{}
|
|
| 54 |
- } |
|
| 55 |
- |
|
| 56 | 51 |
externalNetworks := []string{}
|
| 57 | 52 |
result := make(map[string]types.NetworkCreate) |
| 58 |
- |
|
| 59 |
- for internalName, network := range networks {
|
|
| 53 |
+ for internalName := range servicesNetworks {
|
|
| 54 |
+ network := networks[internalName] |
|
| 60 | 55 |
if network.External.External {
|
| 61 | 56 |
externalNetworks = append(externalNetworks, network.External.Name) |
| 62 | 57 |
continue |
| ... | ... |
@@ -28,6 +28,11 @@ func TestAddStackLabel(t *testing.T) {
|
| 28 | 28 |
|
| 29 | 29 |
func TestNetworks(t *testing.T) {
|
| 30 | 30 |
namespace := Namespace{name: "foo"}
|
| 31 |
+ serviceNetworks := map[string]struct{}{
|
|
| 32 |
+ "normal": {},
|
|
| 33 |
+ "outside": {},
|
|
| 34 |
+ "default": {},
|
|
| 35 |
+ } |
|
| 31 | 36 |
source := networkMap{
|
| 32 | 37 |
"normal": composetypes.NetworkConfig{
|
| 33 | 38 |
Driver: "overlay", |
| ... | ... |
@@ -79,7 +84,7 @@ func TestNetworks(t *testing.T) {
|
| 79 | 79 |
}, |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 |
- networks, externals := Networks(namespace, source) |
|
| 82 |
+ networks, externals := Networks(namespace, source, serviceNetworks) |
|
| 83 | 83 |
assert.DeepEqual(t, networks, expected) |
| 84 | 84 |
assert.DeepEqual(t, externals, []string{"special"})
|
| 85 | 85 |
} |