diff --git a/pkg/drivers/kic/oci/network_create.go b/pkg/drivers/kic/oci/network_create.go index a8d617caf792..5e5aaaff421b 100644 --- a/pkg/drivers/kic/oci/network_create.go +++ b/pkg/drivers/kic/oci/network_create.go @@ -23,6 +23,7 @@ import ( "fmt" "net" "os/exec" + "regexp" "strings" "github.com/blang/semver/v4" @@ -207,8 +208,7 @@ func dockerNetworkInspect(name string) (netInfo, error) { rr, err := dockerInspectGetter(name) if err != nil { logDockerNetworkInspect(Docker, name) - if strings.Contains(rr.Output(), "No such network") { - + if isNetworkNotFound(rr.Output()) { return info, ErrNetworkNotFound } return info, err @@ -293,7 +293,7 @@ func RemoveNetwork(ociBin string, name string) error { } rr, err := runCmd(exec.Command(ociBin, "network", "rm", name)) if err != nil { - if strings.Contains(rr.Output(), "No such network") { + if isNetworkNotFound(rr.Output()) { return ErrNetworkNotFound } // Error response from daemon: error while removing network: network mynet123 id f9e1c50b89feb0b8f4b687f3501a81b618252c9907bc20666e386d0928322387 has active endpoints @@ -347,3 +347,9 @@ func DeleteKICNetworksByLabel(ociBin string, label string) []error { } return nil } + +func isNetworkNotFound(output string) bool { + // "No such network" on Docker 20.X.X and before, "network %s not found" on Docker 23.X.X and later + re := regexp.MustCompile(`(No such network)|(network .+ not found)`) + return re.MatchString(output) +} diff --git a/pkg/drivers/kic/oci/network_create_test.go b/pkg/drivers/kic/oci/network_create_test.go index a4d25bf28883..ac4e1c45eff1 100644 --- a/pkg/drivers/kic/oci/network_create_test.go +++ b/pkg/drivers/kic/oci/network_create_test.go @@ -143,3 +143,65 @@ func TestPodmanInspect(t *testing.T) { }) } } + +func TestIsNetworkNotFound(t *testing.T) { + tests := []struct { + output string + isNotFound bool + }{ + {"Error: No such network: cat", true}, + {"Error response from daemon: network cat not found", true}, + {`[ + { + "Name": "abcde123", + "Id": "4683c88eb412f2744e9763a4bebcb5e3b73a11dbcc79d6d9ab64ab2f10e08faa", + "Created": "2023-09-29T17:12:11.774716834Z", + "Scope": "local", + "Driver": "bridge", + "EnableIPv6": false, + "IPAM": { + "Driver": "default", + "Options": {}, + "Config": [ + { + "Subnet": "192.168.49.0/24", + "Gateway": "192.168.49.1" + } + ] + }, + "Internal": false, + "Attachable": false, + "Ingress": false, + "ConfigFrom": { + "Network": "" + }, + "ConfigOnly": false, + "Containers": { + "b6954f226ccfdb7d190e3792be8d569e4bc5e3c44833d9e274835212fca4f4d2": { + "Name": "p2", + "EndpointID": "30fd6525dab2b0a4f1953a3c8cae7485be272e09938dffe3d6de81e84c574826", + "MacAddress": "02:42:c0:a8:31:02", + "IPv4Address": "192.168.49.2/24", + "IPv6Address": "" + } + }, + "Options": { + "--icc": "", + "--ip-masq": "", + "com.docker.network.driver.mtu": "65535" + }, + "Labels": { + "created_by.minikube.sigs.k8s.io": "true", + "name.minikube.sigs.k8s.io": "minikube" + } + } +]`, false}, + } + + for _, tc := range tests { + got := isNetworkNotFound(tc.output) + if got != tc.isNotFound { + t.Errorf("isNetworkNotFound(%s) = %t; want = %t", tc.output, got, tc.isNotFound) + } + } +}