diff --git a/pkg/drivers/kic/oci/network.go b/pkg/drivers/kic/oci/network.go index 274f01917d10..9ca08fda28d2 100644 --- a/pkg/drivers/kic/oci/network.go +++ b/pkg/drivers/kic/oci/network.go @@ -35,16 +35,16 @@ import ( func RoutableHostIPFromInside(ociBin string, clusterName string, containerName string) (net.IP, error) { if ociBin == Docker { if runtime.GOOS == "linux" { - _, gateway, err := dockerNetworkInspect(clusterName) + info, err := dockerNetworkInspect(clusterName) if err != nil { if errors.Is(err, ErrNetworkNotFound) { klog.Infof("The container %s is not attached to a network, this could be because the cluster was created by minikube 0 { + args = append(args, "-o") + args = append(args, fmt.Sprintf("com.docker.network.driver.mtu=%d", mtu)) + } + + rr, err := runCmd(exec.Command(Docker, args...)) if err != nil { // Pool overlaps with other one on this address space if strings.Contains(rr.Output(), "Pool overlaps") { @@ -99,32 +127,50 @@ func tryCreateDockerNetwork(subnetAddr string, subnetMask int, name string) (net return gateway, nil } -// returns subnet and gate if exists -func dockerNetworkInspect(name string) (*net.IPNet, net.IP, error) { - cmd := exec.Command(Docker, "network", "inspect", name, "--format", "{{(index .IPAM.Config 0).Subnet}},{{(index .IPAM.Config 0).Gateway}}") +// netInfo holds part of a docker or podman network information relevant to kic drivers +type netInfo struct { + name string + subnet *net.IPNet + gateway net.IP + mtu int +} + +// if exists returns subnet, gateway and mtu +func dockerNetworkInspect(name string) (netInfo, error) { + var info = netInfo{name: name} + cmd := exec.Command(Docker, "network", "inspect", name, "--format", `{{(index .IPAM.Config 0).Subnet}},{{(index .IPAM.Config 0).Gateway}},{{(index .Options "com.docker.network.driver.mtu")}}`) rr, err := runCmd(cmd) if err != nil { logDockerNetworkInspect(name) if strings.Contains(rr.Output(), "No such network") { - return nil, nil, ErrNetworkNotFound + + return info, ErrNetworkNotFound } - return nil, nil, err + return info, err } - // results looks like 172.17.0.0/16,172.17.0.1 - ips := strings.Split(strings.TrimSpace(rr.Stdout.String()), ",") - if len(ips) == 0 { - return nil, nil, fmt.Errorf("empty IP list parsed from: %q", rr.Output()) + + // results looks like 172.17.0.0/16,172.17.0.1,1500 + vals := strings.Split(strings.TrimSpace(rr.Stdout.String()), ",") + if len(vals) == 0 { + return info, fmt.Errorf("empty list network inspect: %q", rr.Output()) } - _, subnet, err := net.ParseCIDR(ips[0]) - if err != nil { - return nil, nil, errors.Wrapf(err, "parse subnet for %s", name) + if len(vals) > 0 { + info.gateway = net.ParseIP(vals[1]) + mtu, err := strconv.Atoi(vals[2]) + if err != nil { + klog.Warningf("couldn't parse mtu for docker network %q: %v", name, err) + } else { + info.mtu = mtu + } } - var gateway net.IP - if len(ips) > 0 { - gateway = net.ParseIP(ips[1]) + + _, info.subnet, err = net.ParseCIDR(vals[0]) + if err != nil { + return info, errors.Wrapf(err, "parse subnet for %s", name) } - return subnet, gateway, nil + + return info, nil } func logDockerNetworkInspect(name string) { @@ -157,7 +203,7 @@ func RemoveNetwork(name string) error { } func networkExists(name string) bool { - _, _, err := dockerNetworkInspect(name) + _, err := dockerNetworkInspect(name) if err != nil && !errors.Is(err, ErrNetworkNotFound) { // log unexpected error klog.Warningf("Error inspecting docker network %s: %v", name, err) }