-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow macOS to resolve service FQDNs during 'minikube tunnel' #3464
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make '/etc/resolver' a constant to share between functions, and use filepath.Join to create paths from it. Also, I prefer that the variable name uses path instead of file, since the latter references something specific in Go (file.File) |
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,9 +35,14 @@ func TestDarwinRouteFailsOnConflictIntegrationTest(t *testing.T) { | |
IP: net.IPv4(10, 96, 0, 0), | ||
Mask: net.IPv4Mask(255, 240, 0, 0), | ||
}, | ||
ClusterDomain: "cluster.local", | ||
ClusterDNSIP: net.IPv4(10, 96, 0, 10), | ||
} | ||
conflictingCfg := *cfg | ||
conflictingCfg.Gateway = net.IPv4(127, 0, 0, 2) | ||
|
||
addRoute(t, "10.96.0.0/12", "127.0.0.2") | ||
addRoute(t, &conflictingCfg) | ||
defer cleanRoute(t, &conflictingCfg) | ||
err := (&osRouter{}).EnsureRouteIsAdded(cfg) | ||
if err == nil { | ||
t.Errorf("add should have error, but it is nil") | ||
|
@@ -51,9 +56,11 @@ func TestDarwinRouteIdempotentIntegrationTest(t *testing.T) { | |
IP: net.IPv4(10, 96, 0, 0), | ||
Mask: net.IPv4Mask(255, 240, 0, 0), | ||
}, | ||
ClusterDomain: "cluster.local", | ||
ClusterDNSIP: net.IPv4(10, 96, 0, 10), | ||
} | ||
|
||
cleanRoute(t, "10.96.0.0/12") | ||
cleanRoute(t, cfg) | ||
err := (&osRouter{}).EnsureRouteIsAdded(cfg) | ||
if err != nil { | ||
t.Errorf("add error: %s", err) | ||
|
@@ -64,7 +71,7 @@ func TestDarwinRouteIdempotentIntegrationTest(t *testing.T) { | |
t.Errorf("add error: %s", err) | ||
} | ||
|
||
cleanRoute(t, "10.96.0.0/12") | ||
cleanRoute(t, cfg) | ||
} | ||
|
||
func TestDarwinRouteCleanupIdempontentIntegrationTest(t *testing.T) { | ||
|
@@ -75,10 +82,12 @@ func TestDarwinRouteCleanupIdempontentIntegrationTest(t *testing.T) { | |
IP: net.IPv4(10, 96, 0, 0), | ||
Mask: net.IPv4Mask(255, 240, 0, 0), | ||
}, | ||
ClusterDomain: "cluster.local", | ||
ClusterDNSIP: net.IPv4(10, 96, 0, 10), | ||
} | ||
|
||
cleanRoute(t, "10.96.0.0/12") | ||
addRoute(t, "10.96.0.0/12", "192.168.1.1") | ||
cleanRoute(t, cfg) | ||
addRoute(t, cfg) | ||
err := (&osRouter{}).Cleanup(cfg) | ||
if err != nil { | ||
t.Errorf("cleanup failed with %s", err) | ||
|
@@ -89,20 +98,32 @@ func TestDarwinRouteCleanupIdempontentIntegrationTest(t *testing.T) { | |
} | ||
} | ||
|
||
func addRoute(t *testing.T, cidr string, gw string) { | ||
func addRoute(t *testing.T, r *Route) { | ||
cidr := r.DestCIDR.String() | ||
gw := r.Gateway.String() | ||
command := exec.Command("sudo", "route", "-n", "add", cidr, gw) | ||
_, err := command.CombinedOutput() | ||
if err != nil { | ||
t.Logf("add route error (should be ok): %s", err) | ||
} | ||
err = writeResolverFile(r) | ||
if err != nil { | ||
t.Logf("add route DNS resolver error (should be ok): %s", err) | ||
} | ||
} | ||
|
||
func cleanRoute(t *testing.T, cidr string) { | ||
func cleanRoute(t *testing.T, r *Route) { | ||
cidr := r.DestCIDR.String() | ||
command := exec.Command("sudo", "route", "-n", "delete", cidr) | ||
_, err := command.CombinedOutput() | ||
if err != nil { | ||
t.Logf("cleanup error (should be ok): %s", err) | ||
} | ||
command = exec.Command("sudo", "rm", "-f", fmt.Sprintf("/etc/resolver/%s", r.ClusterDomain)) | ||
_, err = command.CombinedOutput() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're not using the output:
|
||
if err != nil { | ||
t.Logf("cleanup DNS resolver error (should be ok): %s", err) | ||
} | ||
} | ||
|
||
func TestCIDRPadding(t *testing.T) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't include the path name in the message, as EnsureRouteIsAdded doesn't authoritatively know what it is. My suggestion is: return fmt.Errorf("write resolver file: %v", err)