Skip to content
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

Add -resolve-hostnames flag #271

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions subcommand/service-address/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ type Command struct {
flags *flag.FlagSet
k8sFlags *k8sflags.K8SFlags

flagNamespace string
flagServiceName string
flagOutputFile string
flagNamespace string
flagServiceName string
flagOutputFile string
flagResolveHostnames bool

retryDuration time.Duration
k8sClient kubernetes.Interface
Expand All @@ -45,6 +46,8 @@ func (c *Command) init() {
"Name of the service")
c.flags.StringVar(&c.flagOutputFile, "output-file", "",
"Path to file to write load balancer address")
c.flags.BoolVar(&c.flagResolveHostnames, "resolve-hostnames", false,
"If true we will resolve any hostnames and use their first IP address")

c.k8sFlags = &k8sflags.K8SFlags{}
flags.Merge(c.flags, c.k8sFlags.Flags())
Expand Down Expand Up @@ -104,13 +107,11 @@ func (c *Command) Run(args []string) int {
address = ingr.IP
return nil
} else if ingr.Hostname != "" {
// todo: for now we're resolving this hostname to an IP
// because Consul doesn't yet support hostnames for its mesh
// gateway addresses. We will want to remove this when it's
// supported because in the case of EKS (the only cloud
// that returns hostnames for its LBs) the IPs may change
// so only the hostname is safe.
address, unretryableErr = resolveHostname(ingr.Hostname)
if c.flagResolveHostnames {
address, unretryableErr = resolveHostname(ingr.Hostname)
} else {
address = ingr.Hostname
}
return nil
}
}
Expand Down
19 changes: 15 additions & 4 deletions subcommand/service-address/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func TestRun_UnresolvableHostname(t *testing.T) {
"-k8s-namespace", k8sNS,
"-name", svcName,
"-output-file", outputFile,
"-resolve-hostnames=true",
})
require.Equal(1, responseCode)
require.Contains(ui.ErrorWriter.String(), "Unable to get service address: unable to resolve hostname:")
Expand All @@ -118,6 +119,7 @@ func TestRun_ServiceTypes(t *testing.T) {
cases := map[string]struct {
Service *v1.Service
ServiceModificationF func(*v1.Service)
ResolveHostnames bool
ExpErr string
ExpAddress string
}{
Expand All @@ -133,9 +135,14 @@ func TestRun_ServiceTypes(t *testing.T) {
Service: kubeLoadBalancerSvc("service-name", "1.2.3.4", ""),
ExpAddress: "1.2.3.4",
},
"LoadBalancer Hostname": {
"LoadBalancer hostname": {
Service: kubeLoadBalancerSvc("service-name", "", "localhost"),
ExpAddress: "127.0.0.1",
ExpAddress: "localhost",
},
"LoadBalancer hostname with resolve-hostnames=true": {
Service: kubeLoadBalancerSvc("service-name", "", "localhost"),
ResolveHostnames: true,
ExpAddress: "127.0.0.1",
},
"LoadBalancer IP and hostname": {
Service: kubeLoadBalancerSvc("service-name", "1.2.3.4", "example.com"),
Expand Down Expand Up @@ -195,11 +202,15 @@ func TestRun_ServiceTypes(t *testing.T) {
defer os.RemoveAll(tmpDir)
outputFile := filepath.Join(tmpDir, "address.txt")

responseCode := cmd.Run([]string{
args := []string{
"-k8s-namespace", k8sNS,
"-name", svcName,
"-output-file", outputFile,
})
}
if c.ResolveHostnames {
args = append(args, "-resolve-hostnames=true")
}
responseCode := cmd.Run(args)
if c.ExpErr != "" {
require.Equal(1, responseCode)
require.Contains(ui.ErrorWriter.String(), c.ExpErr)
Expand Down