Skip to content

Commit

Permalink
Add ability to resolve service FQDNs from host during 'minikube tunne…
Browse files Browse the repository at this point in the history
…l' (MacOS only for now)
  • Loading branch information
ceason committed Dec 18, 2018
1 parent 5d910e8 commit bd15020
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
12 changes: 9 additions & 3 deletions pkg/minikube/tunnel/cluster_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
7 changes: 5 additions & 2 deletions pkg/minikube/tunnel/cluster_inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package tunnel

import (
"k8s.io/minikube/pkg/util"
"testing"

"net"
Expand Down Expand Up @@ -78,10 +79,12 @@ func TestMinikubeCheckReturnsHostInformation(t *testing.T) {

ip := net.ParseIP("1.2.3.4")
_, ipNet, _ := net.ParseCIDR("96.0.0.0/12")
dnsIp, _ := util.GetDNSIP(ipNet.String())

expectedRoute := &Route{
Gateway: ip,
DestCIDR: ipNet,
Gateway: ip,
DestCIDR: ipNet,
ClusterDNSIP: dnsIp,
}

if s != Running {
Expand Down
37 changes: 37 additions & 0 deletions pkg/minikube/tunnel/route_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package tunnel

import (
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"regexp"
"strings"
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
7 changes: 5 additions & 2 deletions pkg/minikube/tunnel/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package tunnel

import (
"k8s.io/minikube/pkg/util"
"net"
"reflect"
"testing"
Expand Down Expand Up @@ -130,10 +131,12 @@ got
func unsafeParseRoute(gatewayIP string, destCIDR string) *Route {
ip := net.ParseIP(gatewayIP)
_, ipNet, _ := net.ParseCIDR(destCIDR)
dnsIp, _ := util.GetDNSIP(ipNet.String())

expectedRoute := &Route{
Gateway: ip,
DestCIDR: ipNet,
Gateway: ip,
DestCIDR: ipNet,
ClusterDNSIP: dnsIp,
}
return expectedRoute
}
6 changes: 4 additions & 2 deletions pkg/minikube/tunnel/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bd15020

Please sign in to comment.