Skip to content

Commit 9422003

Browse files
committed
fix: skip connection if the DLE container is already connected to an internal network
1 parent 5d503c7 commit 9422003

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

pkg/util/networks/networks.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ const (
2828
)
2929

3030
// Setup creates a new internal Docker network and connects container to it.
31-
func Setup(ctx context.Context, dockerCLI *client.Client, instanceID, containerID string) (string, error) {
31+
func Setup(ctx context.Context, dockerCLI *client.Client, instanceID, containerName string) (string, error) {
3232
networkName := getNetworkName(instanceID)
3333

3434
log.Dbg("Discovering internal network:", networkName)
3535

3636
networkResource, err := dockerCLI.NetworkInspect(ctx, networkName, types.NetworkInspectOptions{})
3737
if err == nil {
38-
if err := dockerCLI.NetworkConnect(ctx, networkResource.ID, containerID, &network.EndpointSettings{}); err != nil {
39-
return "", err
38+
if !hasContainerConnected(networkResource, containerName) {
39+
if err := dockerCLI.NetworkConnect(ctx, networkResource.ID, containerName, &network.EndpointSettings{}); err != nil {
40+
return "", err
41+
}
42+
43+
log.Dbg(fmt.Sprintf("Container %s has been connected to %s", containerName, networkName))
4044
}
4145

4246
return networkResource.ID, nil
@@ -61,7 +65,7 @@ func Setup(ctx context.Context, dockerCLI *client.Client, instanceID, containerI
6165

6266
log.Dbg("A new internal network has been created:", internalNetwork.ID)
6367

64-
if err := dockerCLI.NetworkConnect(ctx, internalNetwork.ID, containerID, &network.EndpointSettings{}); err != nil {
68+
if err := dockerCLI.NetworkConnect(ctx, internalNetwork.ID, containerName, &network.EndpointSettings{}); err != nil {
6569
return "", err
6670
}
6771

@@ -108,11 +112,13 @@ func Connect(ctx context.Context, dockerCLI *client.Client, instanceID, containe
108112
return fmt.Errorf("internal network not found: %w", err)
109113
}
110114

111-
if err := dockerCLI.NetworkConnect(ctx, networkResource.ID, containerID, &network.EndpointSettings{}); err != nil {
112-
return err
113-
}
115+
if !hasContainerConnected(networkResource, containerID) {
116+
if err := dockerCLI.NetworkConnect(ctx, networkResource.ID, containerID, &network.EndpointSettings{}); err != nil {
117+
return err
118+
}
114119

115-
log.Dbg(fmt.Sprintf("Container %s has been connected to %s", instanceID, networkName))
120+
log.Dbg(fmt.Sprintf("Container %s has been connected to %s", instanceID, networkName))
121+
}
116122

117123
return nil
118124
}
@@ -147,6 +153,16 @@ func Reconnect(ctx context.Context, dockerCLI *client.Client, instanceID, contai
147153
return nil
148154
}
149155

156+
func hasContainerConnected(networkResource types.NetworkResource, containerID string) bool {
157+
for _, container := range networkResource.Containers {
158+
if container.Name == containerID {
159+
return true
160+
}
161+
}
162+
163+
return false
164+
}
165+
150166
func getNetworkName(instanceID string) string {
151167
return networkPrefix + instanceID
152168
}

pkg/util/networks/networks_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package networks
77
import (
88
"testing"
99

10+
"github.com/docker/docker/api/types"
1011
"github.com/stretchr/testify/assert"
1112
)
1213

@@ -17,3 +18,30 @@ func TestInternalNetworks(t *testing.T) {
1718
assert.Equal(t, "dle_network_testInstanceID", getNetworkName(instanceID))
1819
})
1920
}
21+
22+
func TestIfContainerConnected(t *testing.T) {
23+
t.Run("test if container connected", func(t *testing.T) {
24+
resource := types.NetworkResource{
25+
Containers: map[string]types.EndpointResource{
26+
"testID": {Name: "test_server"},
27+
},
28+
}
29+
testCases := []struct {
30+
containerName string
31+
result bool
32+
}{
33+
{
34+
containerName: "test_server",
35+
result: true,
36+
},
37+
{
38+
containerName: "not_connected_server",
39+
result: false,
40+
},
41+
}
42+
43+
for _, tc := range testCases {
44+
assert.Equal(t, tc.result, hasContainerConnected(resource, tc.containerName))
45+
}
46+
})
47+
}

0 commit comments

Comments
 (0)