make struct for netinfo

pull/9530/head
Medya Gh 2020-10-23 13:45:25 -07:00
parent 94485d5a19
commit aa97aca2ff
2 changed files with 44 additions and 26 deletions

View File

@ -35,16 +35,16 @@ import (
func RoutableHostIPFromInside(ociBin string, clusterName string, containerName string) (net.IP, error) { func RoutableHostIPFromInside(ociBin string, clusterName string, containerName string) (net.IP, error) {
if ociBin == Docker { if ociBin == Docker {
if runtime.GOOS == "linux" { if runtime.GOOS == "linux" {
_, gateway, _, err := dockerNetworkInspect(clusterName) info, err := dockerNetworkInspect(clusterName)
if err != nil { if err != nil {
if errors.Is(err, ErrNetworkNotFound) { 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 <v1.14, will try to get the IP using container gatway", containerName) klog.Infof("The container %s is not attached to a network, this could be because the cluster was created by minikube <v1.14, will try to get the IP using container gatway", containerName)
return containerGatewayIP(Docker, containerName) return containerGatewayIP(Docker, containerName)
} }
return gateway, errors.Wrap(err, "network inspect") return info.gateway, errors.Wrap(err, "network inspect")
} }
return gateway, nil return info.gateway, nil
} }
// for windows and mac, the gateway ip is not routable so we use dns trick. // for windows and mac, the gateway ip is not routable so we use dns trick.
return digDNS(ociBin, containerName, "host.docker.internal") return digDNS(ociBin, containerName, "host.docker.internal")

View File

@ -38,7 +38,10 @@ const firstSubnetAddr = "192.168.49.0"
const defaultSubnetMask = 24 const defaultSubnetMask = 24
// will be used if docker bridge config doesn't exist related issue #9528 // will be used if docker bridge config doesn't exist related issue #9528
const defaultNetworkMTU = 1500 const defaultMTU = 1500
// name of the bridge network that docker creates by default to be used to get the MTU. ( related issue #9528)
const dockerDefaultBridgeName = "bridge"
// CreateNetwork creates a network returns gateway and error, minikube creates one network per cluster // CreateNetwork creates a network returns gateway and error, minikube creates one network per cluster
func CreateNetwork(ociBin string, name string) (net.IP, error) { func CreateNetwork(ociBin string, name string) (net.IP, error) {
@ -50,20 +53,24 @@ func CreateNetwork(ociBin string, name string) (net.IP, error) {
func createDockerNetwork(clusterName string) (net.IP, error) { func createDockerNetwork(clusterName string) (net.IP, error) {
// check if the network already exists // check if the network already exists
subnet, gateway, mtu, err := dockerNetworkInspect(clusterName) info, err := dockerNetworkInspect(clusterName)
if err == nil { if err == nil {
klog.Infof("Found existing network with subnet %s and gateway %s.", subnet, gateway) klog.Infof("Found existing network %+v", info)
return gateway, nil return info.gateway, nil
} }
info, err = dockerNetworkInspect(dockerDefaultBridgeName)
if err != nil {
info.mtu = defaultMTU
}
attempts := 0 attempts := 0
subnetAddr := firstSubnetAddr subnetAddr := firstSubnetAddr
// Rather than iterate through all of the valid subnets, give up at 20 to avoid a lengthy user delay for something that is unlikely to work. // Rather than iterate through all of the valid subnets, give up at 20 to avoid a lengthy user delay for something that is unlikely to work.
// will be like 192.168.49.0/24 ,...,192.168.239.0/24 // will be like 192.168.49.0/24 ,...,192.168.239.0/24
for attempts < 20 { for attempts < 20 {
gateway, err = tryCreateDockerNetwork(subnetAddr, defaultSubnetMask, mtu, clusterName) info.gateway, err = tryCreateDockerNetwork(subnetAddr, defaultSubnetMask, info.mtu, clusterName)
if err == nil { if err == nil {
return gateway, nil return info.gateway, nil
} }
// don't retry if error is not adddress is taken // don't retry if error is not adddress is taken
@ -81,7 +88,7 @@ func createDockerNetwork(clusterName string) (net.IP, error) {
newSubnet[2] += byte(9 + attempts) newSubnet[2] += byte(9 + attempts)
subnetAddr = newSubnet.String() subnetAddr = newSubnet.String()
} }
return gateway, fmt.Errorf("failed to create network after 20 attempts") return info.gateway, fmt.Errorf("failed to create network after 20 attempts")
} }
func tryCreateDockerNetwork(subnetAddr string, subnetMask int, mtu int, name string) (net.IP, error) { func tryCreateDockerNetwork(subnetAddr string, subnetMask int, mtu int, name string) (net.IP, error) {
@ -104,40 +111,51 @@ func tryCreateDockerNetwork(subnetAddr string, subnetMask int, mtu int, name str
return gateway, nil return gateway, nil
} }
// returns subnet and gate if exists returns subnet, gateway and mtu // netInfo holds part of a docker or podman network information relevent to kic drivers
func dockerNetworkInspect(name string) (*net.IPNet, net.IP, int, error) { 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}
info.mtu = defaultMTU
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")`) 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) rr, err := runCmd(cmd)
if err != nil { if err != nil {
logDockerNetworkInspect(name) logDockerNetworkInspect(name)
if strings.Contains(rr.Output(), "No such network") { if strings.Contains(rr.Output(), "No such network") {
return nil, nil, defaultNetworkMTU, ErrNetworkNotFound
return info, ErrNetworkNotFound
} }
return nil, nil, defaultNetworkMTU, err return info, err
} }
// results looks like 172.17.0.0/16,172.17.0.1
// results looks like 172.17.0.0/16,172.17.0.1,1500
vals := strings.Split(strings.TrimSpace(rr.Stdout.String()), ",") vals := strings.Split(strings.TrimSpace(rr.Stdout.String()), ",")
if len(vals) == 0 { if len(vals) == 0 {
return nil, nil, defaultNetworkMTU, fmt.Errorf("empty IP list parsed from: %q", rr.Output()) return info, fmt.Errorf("empty list network inspect: %q", rr.Output())
} }
var gateway net.IP
mtu := defaultNetworkMTU
if len(vals) > 0 { if len(vals) > 0 {
gateway = net.ParseIP(vals[1]) info.gateway = net.ParseIP(vals[1])
mtu, err = strconv.Atoi(vals[2]) mtu, err := strconv.Atoi(vals[2])
if err != nil { if err != nil {
klog.Warningf("failed to parse docker network %s mtu, will use the default %d : %v", name, defaultNetworkMTU, err) klog.Warningf("failed to parse docker network %s mtu, will use the default %d : %v", name, defaultMTU, err)
mtu = defaultNetworkMTU } else {
info.mtu = mtu
} }
} }
_, subnet, err := net.ParseCIDR(vals[0]) _, info.subnet, err = net.ParseCIDR(vals[0])
if err != nil { if err != nil {
return nil, nil, defaultNetworkMTU, errors.Wrapf(err, "parse subnet for %s", name) return info, errors.Wrapf(err, "parse subnet for %s", name)
} }
return subnet, gateway, mtu, nil return info, nil
} }
func logDockerNetworkInspect(name string) { func logDockerNetworkInspect(name string) {
@ -170,7 +188,7 @@ func RemoveNetwork(name string) error {
} }
func networkExists(name string) bool { func networkExists(name string) bool {
_, _, _, err := dockerNetworkInspect(name) _, err := dockerNetworkInspect(name)
if err != nil && !errors.Is(err, ErrNetworkNotFound) { // log unexpected error if err != nil && !errors.Is(err, ErrNetworkNotFound) { // log unexpected error
klog.Warningf("Error inspecting docker network %s: %v", name, err) klog.Warningf("Error inspecting docker network %s: %v", name, err)
} }