Skip to content

Commit 95fa838

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

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

pkg/container/docker/network.go

+34-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,26 @@ 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+
ok := strings.Contains(strings.Join(lines, ","), networkName)
83+
84+
if ok {
85+
return true
86+
}
87+
return false
88+
}

pkg/container/docker/network_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package docker
18+
19+
import "testing"
20+
21+
func TestNetworkAction(t *testing.T) {
22+
var testNetworkName = "kind-test-network"
23+
24+
if err := CreateNetwork(testNetworkName); err != nil {
25+
t.Error("expected an error for CreateNetwork")
26+
}
27+
28+
if willBeTrue := IsNetworkExist(testNetworkName); willBeTrue != true {
29+
t.Error("expected an error for IsNetworkExist")
30+
}
31+
32+
if willBeTrue := isNetworkCreatedByKind(testNetworkName); willBeTrue != true {
33+
t.Error("expected an error for isNetworkCreatedByKind")
34+
}
35+
36+
if err := DeleteNetwork(testNetworkName); err != nil {
37+
t.Error("expected an error for DeleteNetwork")
38+
}
39+
40+
if willBeFalse := isNetworkCreatedByKind(testNetworkName); willBeFalse != false {
41+
t.Error("expected an error for isNetworkCreatedByKind")
42+
}
43+
44+
if willBeFalse := isNetworkCreatedByKind("qwerty.asdfg"); willBeFalse != false {
45+
t.Error("expected an error for isNetworkCreatedByKind")
46+
}
47+
48+
}

0 commit comments

Comments
 (0)