Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix network not found not being detected on new Docker versions #17323

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
spowelljr marked this conversation as resolved.
Show resolved Hide resolved
if got != tc.isNotFound {
t.Errorf("isNetworkNotFound(%s) = %t; want = %t", tc.output, got, tc.isNotFound)
}
}
}
Loading