From 3531efda7b8291bbcdf7fc04452720bdc5e0e819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Fri, 1 May 2020 15:01:42 +0200 Subject: [PATCH 1/2] Get the gateway by inspecting container network --- pkg/drivers/kic/oci/network.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/drivers/kic/oci/network.go b/pkg/drivers/kic/oci/network.go index 2ba57b06be57..fac383e59b92 100644 --- a/pkg/drivers/kic/oci/network.go +++ b/pkg/drivers/kic/oci/network.go @@ -31,14 +31,19 @@ import ( // RoutableHostIPFromInside returns the ip/dns of the host that container lives on // is routable from inside the container func RoutableHostIPFromInside(ociBin string, containerName string) (net.IP, error) { - if ociBin != Docker { - return nil, fmt.Errorf("RoutableHostIPFromInside is currently only implemented for docker https://github.com/containers/libpod/issues/5205") + if ociBin == Docker { + if runtime.GOOS == "linux" { + return dockerGatewayIP() + } + // for windows and mac, the gateway ip is not routable so we use dns trick. + return digDNS(ociBin, containerName, "host.docker.internal") } + if runtime.GOOS == "linux" { - return dockerGatewayIP() + return containerGatewayIP(ociBin, containerName) } - // for windows and mac, the gateway ip is not routable so we use dns trick. - return digDNS(ociBin, containerName, "host.docker.internal") + + return nil, fmt.Errorf("RoutableHostIPFromInside is currently only implemented for linux") } // digDNS will get the IP record for a dns @@ -73,6 +78,17 @@ func dockerGatewayIP() (net.IP, error) { return ip, nil } +// containerGatewayIP gets the default gateway ip for the container +func containerGatewayIP(ociBin, containerName string) (net.IP, error) { + rr, err := runCmd(exec.Command(ociBin, "inspect", "--format", "{{.NetworkSettings.Gateway}}", containerName)) + if err != nil { + return nil, errors.Wrapf(err, "inspect gateway") + } + ip := net.ParseIP(strings.TrimSpace(rr.Stdout.String())) + glog.Infof("got host ip for mount in container by inspecting container: %s", ip.String()) + return ip, nil +} + // ForwardedPort will return port mapping for a container using cli. // example : ForwardedPort("docker", "minikube", "22") // will return the docker assigned port: From cc6506d2c951b06c001d074d6f08c838791925f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sun, 3 May 2020 09:42:32 +0200 Subject: [PATCH 2/2] Remove logging statement from containerGateway --- pkg/drivers/kic/oci/network.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/drivers/kic/oci/network.go b/pkg/drivers/kic/oci/network.go index fac383e59b92..3cb1cf859c70 100644 --- a/pkg/drivers/kic/oci/network.go +++ b/pkg/drivers/kic/oci/network.go @@ -85,7 +85,6 @@ func containerGatewayIP(ociBin, containerName string) (net.IP, error) { return nil, errors.Wrapf(err, "inspect gateway") } ip := net.ParseIP(strings.TrimSpace(rr.Stdout.String())) - glog.Infof("got host ip for mount in container by inspecting container: %s", ip.String()) return ip, nil }