From 8a6613ee8c321a15c338afa9f5b1ba6fd3ca0d78 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Wed, 12 Feb 2020 14:24:11 +0100 Subject: [PATCH] Add a default network configuration for the project and use it for service without network configuration Signed-off-by: Guillaume Lours --- compose-ref.go | 2 +- internal/network.go | 32 ++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/compose-ref.go b/compose-ref.go index 2f58296..2c226cb 100644 --- a/compose-ref.go +++ b/compose-ref.go @@ -215,7 +215,7 @@ func createService(cli *client.Client, project string, prjDir string, s compose. labels[internal.LabelConfig] = string(b) fmt.Printf("Creating container for service %s ... ", s.Name) - networkMode := internal.NetworkMode(s, networks) + networkMode := internal.NetworkMode(project, s, networks) mounts, err := internal.CreateContainerMounts(s, prjDir) if err != nil { return err diff --git a/internal/network.go b/internal/network.go index 23f50af..8189f04 100644 --- a/internal/network.go +++ b/internal/network.go @@ -23,6 +23,14 @@ func GetNetworksFromConfig(cli *client.Client, project string, config *compose.C } networks[name] = id } + if _, ok := networks["default"]; !ok { + name, id, err := createNetwork(cli, project, "default", + compose.NetworkConfig{Name: fmt.Sprintf("%s-default", project)}) + if err != nil { + return nil, err + } + networks[name] = id + } return networks, nil } @@ -138,11 +146,11 @@ func collectNetworks(cli *client.Client, project string) (map[string][]types.Net return networks, nil } -func NetworkMode(serviceConfig compose.ServiceConfig, networks map[string]string) container.NetworkMode { +func NetworkMode(project string, serviceConfig compose.ServiceConfig, networks map[string]string) container.NetworkMode { mode := serviceConfig.NetworkMode if mode == "" { if len(networks) > 0 { - for name := range getNetworksForService(serviceConfig) { + for name := range getNetworksForService(project, serviceConfig) { if _, ok := networks[name]; ok { return container.NetworkMode(networks[name]) } @@ -153,32 +161,29 @@ func NetworkMode(serviceConfig compose.ServiceConfig, networks map[string]string return container.NetworkMode(mode) } -func getNetworksForService(config compose.ServiceConfig) map[string]*compose.ServiceNetworkConfig { +func getNetworksForService(project string, config compose.ServiceConfig) map[string]*compose.ServiceNetworkConfig { if len(config.Networks) > 0 { return config.Networks } - return map[string]*compose.ServiceNetworkConfig{"default": nil} + return map[string]*compose.ServiceNetworkConfig{fmt.Sprintf("%s-default", project): nil} } - func BuildDefaultNetworkConfig(serviceConfig compose.ServiceConfig, networkMode container.NetworkMode) *network.NetworkingConfig { config := map[string]*network.EndpointSettings{} net := string(networkMode) config[net] = &network.EndpointSettings{ - Aliases: getAliases(serviceConfig.Name, serviceConfig.Networks[net]), + Aliases: getAliases(serviceConfig.Name, serviceConfig.Networks[net], ""), } - return &network.NetworkingConfig{ EndpointsConfig: config, } } - func ConnectContainerToNetworks(context context.Context, cli *client.Client, serviceConfig compose.ServiceConfig, containerID string, networks map[string]string) error { for key, net := range serviceConfig.Networks { config := &network.EndpointSettings{ - Aliases: getAliases(serviceConfig.Name, net), + Aliases: getAliases(serviceConfig.Name, net, containerID), } err := cli.NetworkConnect(context, networks[key], containerID, config) if err != nil { @@ -188,8 +193,11 @@ func ConnectContainerToNetworks(context context.Context, cli *client.Client, return nil } -func getAliases(serviceName string, c *compose.ServiceNetworkConfig) []string { +func getAliases(serviceName string, c *compose.ServiceNetworkConfig, containerID string) []string { aliases := []string{serviceName} + if containerID != "" { + aliases = append(aliases, containerShortID(containerID)) + } if c != nil { aliases = append(aliases, c.Aliases...) } @@ -219,3 +227,7 @@ func ExposedPorts(ports []compose.ServicePortConfig) nat.PortSet { } return natPorts } + +func containerShortID(containerID string) string { + return containerID[:12] +}