Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
Add a default network configuration for the project and use it for se…
Browse files Browse the repository at this point in the history
…rvice without network configuration

Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
glours authored and ndeloof committed Feb 12, 2020
1 parent b489ea7 commit 8a6613e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compose-ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 22 additions & 10 deletions internal/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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])
}
Expand All @@ -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 {
Expand All @@ -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...)
}
Expand Down Expand Up @@ -219,3 +227,7 @@ func ExposedPorts(ports []compose.ServicePortConfig) nat.PortSet {
}
return natPorts
}

func containerShortID(containerID string) string {
return containerID[:12]
}

0 comments on commit 8a6613e

Please sign in to comment.