Skip to content

Commit

Permalink
Merge pull request #17323 from spowelljr/fixNetworkError
Browse files Browse the repository at this point in the history
Fix network not found not being detected on new Docker versions
  • Loading branch information
medyagh authored Oct 3, 2023
2 parents 36316b0 + c1ea47c commit ada5465
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/drivers/kic/oci/network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net"
"os/exec"
"regexp"
"strings"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
62 changes: 62 additions & 0 deletions pkg/drivers/kic/oci/network_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit ada5465

Please sign in to comment.