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

Commit 8a6613e

Browse files
gloursndeloof
authored andcommitted
Add a default network configuration for the project and use it for service without network configuration
Signed-off-by: Guillaume Lours <[email protected]>
1 parent b489ea7 commit 8a6613e

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

compose-ref.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func createService(cli *client.Client, project string, prjDir string, s compose.
215215
labels[internal.LabelConfig] = string(b)
216216

217217
fmt.Printf("Creating container for service %s ... ", s.Name)
218-
networkMode := internal.NetworkMode(s, networks)
218+
networkMode := internal.NetworkMode(project, s, networks)
219219
mounts, err := internal.CreateContainerMounts(s, prjDir)
220220
if err != nil {
221221
return err

internal/network.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ func GetNetworksFromConfig(cli *client.Client, project string, config *compose.C
2323
}
2424
networks[name] = id
2525
}
26+
if _, ok := networks["default"]; !ok {
27+
name, id, err := createNetwork(cli, project, "default",
28+
compose.NetworkConfig{Name: fmt.Sprintf("%s-default", project)})
29+
if err != nil {
30+
return nil, err
31+
}
32+
networks[name] = id
33+
}
2634
return networks, nil
2735
}
2836

@@ -138,11 +146,11 @@ func collectNetworks(cli *client.Client, project string) (map[string][]types.Net
138146
return networks, nil
139147
}
140148

141-
func NetworkMode(serviceConfig compose.ServiceConfig, networks map[string]string) container.NetworkMode {
149+
func NetworkMode(project string, serviceConfig compose.ServiceConfig, networks map[string]string) container.NetworkMode {
142150
mode := serviceConfig.NetworkMode
143151
if mode == "" {
144152
if len(networks) > 0 {
145-
for name := range getNetworksForService(serviceConfig) {
153+
for name := range getNetworksForService(project, serviceConfig) {
146154
if _, ok := networks[name]; ok {
147155
return container.NetworkMode(networks[name])
148156
}
@@ -153,32 +161,29 @@ func NetworkMode(serviceConfig compose.ServiceConfig, networks map[string]string
153161
return container.NetworkMode(mode)
154162
}
155163

156-
func getNetworksForService(config compose.ServiceConfig) map[string]*compose.ServiceNetworkConfig {
164+
func getNetworksForService(project string, config compose.ServiceConfig) map[string]*compose.ServiceNetworkConfig {
157165
if len(config.Networks) > 0 {
158166
return config.Networks
159167
}
160-
return map[string]*compose.ServiceNetworkConfig{"default": nil}
168+
return map[string]*compose.ServiceNetworkConfig{fmt.Sprintf("%s-default", project): nil}
161169
}
162170

163-
164171
func BuildDefaultNetworkConfig(serviceConfig compose.ServiceConfig, networkMode container.NetworkMode) *network.NetworkingConfig {
165172
config := map[string]*network.EndpointSettings{}
166173
net := string(networkMode)
167174
config[net] = &network.EndpointSettings{
168-
Aliases: getAliases(serviceConfig.Name, serviceConfig.Networks[net]),
175+
Aliases: getAliases(serviceConfig.Name, serviceConfig.Networks[net], ""),
169176
}
170-
171177
return &network.NetworkingConfig{
172178
EndpointsConfig: config,
173179
}
174180
}
175181

176-
177182
func ConnectContainerToNetworks(context context.Context, cli *client.Client,
178183
serviceConfig compose.ServiceConfig, containerID string, networks map[string]string) error {
179184
for key, net := range serviceConfig.Networks {
180185
config := &network.EndpointSettings{
181-
Aliases: getAliases(serviceConfig.Name, net),
186+
Aliases: getAliases(serviceConfig.Name, net, containerID),
182187
}
183188
err := cli.NetworkConnect(context, networks[key], containerID, config)
184189
if err != nil {
@@ -188,8 +193,11 @@ func ConnectContainerToNetworks(context context.Context, cli *client.Client,
188193
return nil
189194
}
190195

191-
func getAliases(serviceName string, c *compose.ServiceNetworkConfig) []string {
196+
func getAliases(serviceName string, c *compose.ServiceNetworkConfig, containerID string) []string {
192197
aliases := []string{serviceName}
198+
if containerID != "" {
199+
aliases = append(aliases, containerShortID(containerID))
200+
}
193201
if c != nil {
194202
aliases = append(aliases, c.Aliases...)
195203
}
@@ -219,3 +227,7 @@ func ExposedPorts(ports []compose.ServicePortConfig) nat.PortSet {
219227
}
220228
return natPorts
221229
}
230+
231+
func containerShortID(containerID string) string {
232+
return containerID[:12]
233+
}

0 commit comments

Comments
 (0)