Skip to content

Commit 48977ec

Browse files
only delete network which created by kind.
Signed-off-by: Jintao Zhang <zhangjintao9020@gmail.com>
1 parent e9b32dc commit 48977ec

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

pkg/container/docker/network.go

+29-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package docker
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
"sigs.k8s.io/kind/pkg/cluster/constants"
2324
"sigs.k8s.io/kind/pkg/exec"
@@ -36,13 +37,17 @@ func CreateNetwork(networkName string) error {
3637
}
3738

3839
// DeleteNetwork delete the special network
40+
// only when the network was created by kind
3941
func DeleteNetwork(networkName string) error {
40-
cmd := exec.Command(
41-
"docker", "network",
42-
"rm",
43-
networkName,
44-
)
45-
return cmd.Run()
42+
if isNetworkCreatedByKind(networkName) {
43+
cmd := exec.Command(
44+
"docker", "network",
45+
"rm",
46+
networkName,
47+
)
48+
return cmd.Run()
49+
}
50+
return nil
4651
}
4752

4853
// IsNetworkExist check if the network exist
@@ -58,3 +63,21 @@ func IsNetworkExist(networkName string) bool {
5863

5964
return true
6065
}
66+
67+
// isNetworkCreatedByKind checks if it was created by kind
68+
func isNetworkCreatedByKind(networkName string) bool {
69+
cmd := exec.Command(
70+
"docker", "network",
71+
"ls",
72+
"--filter=label="+fmt.Sprintf("%s=%s", constants.ClusterLabelKey, networkName),
73+
"--format='{{ .Name }}'",
74+
)
75+
76+
lines, err := exec.CombinedOutputLines(cmd)
77+
78+
if err != nil {
79+
return false
80+
}
81+
82+
return strings.Contains(strings.Join(lines, ","), networkName)
83+
}

0 commit comments

Comments
 (0)