File tree 1 file changed +34
-6
lines changed
1 file changed +34
-6
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ package docker
18
18
19
19
import (
20
20
"fmt"
21
+ "strings"
21
22
22
23
"sigs.k8s.io/kind/pkg/cluster/constants"
23
24
"sigs.k8s.io/kind/pkg/exec"
@@ -36,13 +37,17 @@ func CreateNetwork(networkName string) error {
36
37
}
37
38
38
39
// DeleteNetwork delete the special network
40
+ // only when the network was created by kind
39
41
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
46
51
}
47
52
48
53
// IsNetworkExist check if the network exist
@@ -58,3 +63,26 @@ func IsNetworkExist(networkName string) bool {
58
63
59
64
return true
60
65
}
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
+ }
You can’t perform that action at this time.
0 commit comments