From f2c58e4fa5f6b7e2776d5455e35592b8bdc0e04e Mon Sep 17 00:00:00 2001 From: Chris Eason Date: Sun, 16 Dec 2018 17:03:35 -0500 Subject: [PATCH] Add ability to resolve service FQDNs from host during 'minikube tunnel' (MacOS only for now) --- pkg/minikube/tunnel/cluster_inspector.go | 12 ++++++-- pkg/minikube/tunnel/route_darwin.go | 37 ++++++++++++++++++++++++ pkg/minikube/tunnel/types.go | 6 ++-- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/pkg/minikube/tunnel/cluster_inspector.go b/pkg/minikube/tunnel/cluster_inspector.go index 57c8dcee7566..4906ef573294 100644 --- a/pkg/minikube/tunnel/cluster_inspector.go +++ b/pkg/minikube/tunnel/cluster_inspector.go @@ -26,6 +26,7 @@ import ( "github.com/pkg/errors" "k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/config" + "k8s.io/minikube/pkg/util" ) type clusterInspector struct { @@ -93,9 +94,14 @@ func getRoute(host *host.Host, clusterConfig config.Config) (*Route, error) { if ip == nil { return nil, fmt.Errorf("invalid IP for host %s", hostDriverIP) } - + dnsIp, err := util.GetDNSIP(ipNet.String()) + if err != nil { + return nil, err + } return &Route{ - Gateway: ip, - DestCIDR: ipNet, + Gateway: ip, + DestCIDR: ipNet, + ClusterDomain: clusterConfig.KubernetesConfig.DNSDomain, + ClusterDNSIP: dnsIp, }, nil } diff --git a/pkg/minikube/tunnel/route_darwin.go b/pkg/minikube/tunnel/route_darwin.go index fdf7fcbffee6..9873b7b0a949 100644 --- a/pkg/minikube/tunnel/route_darwin.go +++ b/pkg/minikube/tunnel/route_darwin.go @@ -18,7 +18,9 @@ package tunnel import ( "fmt" + "io/ioutil" "net" + "os" "os/exec" "regexp" "strings" @@ -34,6 +36,9 @@ func (router *osRouter) EnsureRouteIsAdded(route *Route) error { if exists { return nil } + if err := writeResolverFile(route); err != nil { + return fmt.Errorf("could not write /etc/resolver/{cluster_domain} file: %s", err) + } serviceCIDR := route.DestCIDR.String() gatewayIP := route.Gateway.String() @@ -162,5 +167,37 @@ func (router *osRouter) Cleanup(route *Route) error { if !re.MatchString(message) { return fmt.Errorf("error deleting route: %s, %d", message, len(strings.Split(message, "\n"))) } + // idempotent removal of cluster domain dns + resolverFile := fmt.Sprintf("/etc/resolver/%s", route.ClusterDomain) + command = exec.Command("sudo", "rm", "-f", resolverFile) + if err := command.Run(); err != nil { + return fmt.Errorf("could not remove %s: %s", resolverFile, err) + } + return nil +} + +func writeResolverFile(route *Route) error { + resolverFile := "/etc/resolver/" + route.ClusterDomain + content := fmt.Sprintf("nameserver %s\nsearch_order 1\n", route.ClusterDNSIP) + // write resolver content into tmpFile, then copy it to /etc/resolver/clusterDomain + tmpFile, err := ioutil.TempFile("", "minikube-tunnel-resolver-") + if err != nil { + return err + } + defer os.Remove(tmpFile.Name()) + if _, err = tmpFile.WriteString(content); err != nil { + return err + } + if err = tmpFile.Close(); err != nil { + return err + } + command := exec.Command("sudo", "mkdir", "-p", "/etc/resolver") + if err := command.Run(); err != nil { + return err + } + command = exec.Command("sudo", "cp", "-f", tmpFile.Name(), resolverFile) + if err := command.Run(); err != nil { + return err + } return nil } diff --git a/pkg/minikube/tunnel/types.go b/pkg/minikube/tunnel/types.go index a8540782e8f3..d1134af3bea7 100644 --- a/pkg/minikube/tunnel/types.go +++ b/pkg/minikube/tunnel/types.go @@ -58,8 +58,10 @@ func (t *Status) String() string { } type Route struct { - Gateway net.IP - DestCIDR *net.IPNet + Gateway net.IP + DestCIDR *net.IPNet + ClusterDomain string + ClusterDNSIP net.IP } func (r *Route) String() string {