summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorArne Juul <arnej@yahooinc.com>2022-08-26 10:00:27 +0000
committerArne Juul <arnej@yahooinc.com>2022-08-26 10:19:16 +0000
commit274cefec7f9e65309b52b2dc6ca665c4ded9f1b5 (patch)
treeb357529f3dcdb586100d226733bcd8418df72c74 /client
parentdfbddf368eccd05cb2608a89c9252b2d103ead56 (diff)
slight improvements for FindOurHostname
Diffstat (limited to 'client')
-rw-r--r--client/go/vespa/detect_hostname.go32
-rw-r--r--client/go/vespa/detect_hostname_test.go13
2 files changed, 29 insertions, 16 deletions
diff --git a/client/go/vespa/detect_hostname.go b/client/go/vespa/detect_hostname.go
index 2cd1e44db1f..33e5ca21a8a 100644
--- a/client/go/vespa/detect_hostname.go
+++ b/client/go/vespa/detect_hostname.go
@@ -12,10 +12,8 @@ import (
// detect if this host is IPv6-only, in which case we want to pass
// the flag "-Djava.net.preferIPv6Addresses=true" to any java command
-
func HasOnlyIpV6() bool {
- hostname, err := os.Hostname()
- hostname, err = FindOurHostname(hostname)
+ hostname, err := FindOurHostname()
if hostname == "" || err != nil {
return false
}
@@ -38,21 +36,38 @@ func HasOnlyIpV6() bool {
return foundV6 && !foundV4
}
-func FindOurHostname(name string) (string, error) {
+// Find a good name for the host we're running on.
+// We need something that *other* hosts can use for connnecting back
+// to our services, preferably the canonical DNS name.
+// If automatic detection fails, "localhost" will be returned, so
+// single-node setups still have a good chance of working.
+// Use the enviroment variable VESPA_HOSTNAME to override.
+func FindOurHostname() (string, error) {
env := os.Getenv("VESPA_HOSTNAME")
if env != "" {
// assumes: env var is already validated and OK
- fmt.Fprintln(os.Stderr, "from env:", env)
return env, nil
}
+ name, err := os.Hostname()
+ if err != nil {
+ return findOurHostnameFrom("localhost")
+ }
+ name, err = findOurHostnameFrom(name)
+ if strings.HasSuffix(name, ".") {
+ name = name[:len(name)-1]
+ }
+ return name, err
+}
+
+func findOurHostnameFrom(name string) (string, error) {
ifAddrs, _ := net.InterfaceAddrs()
- fmt.Fprintln(os.Stderr, "interface addrs:", ifAddrs)
var checkIsMine = func(addr string) bool {
if len(ifAddrs) == 0 {
// no validation possible, assume OK
return true
}
for _, ifAddr := range ifAddrs {
+ // note: ifAddr.String() is typically "127.0.0.1/8"
if ipnet, ok := ifAddr.(*net.IPNet); ok {
if ipnet.IP.String() == addr {
return true
@@ -63,7 +78,6 @@ func FindOurHostname(name string) (string, error) {
}
if name != "" {
ipAddrs, _ := net.LookupIP(name)
- fmt.Fprintln(os.Stderr, "LookupIP", name, "->", ipAddrs)
for _, addr := range ipAddrs {
switch {
case addr.IsLoopback():
@@ -73,13 +87,11 @@ func FindOurHostname(name string) (string, error) {
reverseNames, _ := net.LookupAddr(addr.String())
for _, reverse := range reverseNames {
if strings.HasPrefix(reverse, name) {
- fmt.Fprintln(os.Stderr, "hostname", name, "->", addr, "-> ", reverse)
return reverse, nil
}
}
if len(reverseNames) > 0 {
reverse := reverseNames[0]
- fmt.Fprintln(os.Stderr, "hostname", name, "->", addr, "-> ", reverse)
return reverse, nil
}
}
@@ -89,14 +101,12 @@ func FindOurHostname(name string) (string, error) {
for _, ifAddr := range ifAddrs {
if ipnet, ok := ifAddr.(*net.IPNet); ok {
ip := ipnet.IP
- fmt.Fprintln(os.Stderr, "converted IP", ifAddr, "->", ip)
if ip == nil || ip.IsLoopback() {
continue
}
reverseNames, _ := net.LookupAddr(ip.String())
if len(reverseNames) > 0 {
reverse := reverseNames[0]
- fmt.Fprintln(os.Stderr, "interface", ifAddr, "->", reverse)
return reverse, nil
}
}
diff --git a/client/go/vespa/detect_hostname_test.go b/client/go/vespa/detect_hostname_test.go
index bb61e157b26..2692abae4d2 100644
--- a/client/go/vespa/detect_hostname_test.go
+++ b/client/go/vespa/detect_hostname_test.go
@@ -12,16 +12,19 @@ import (
func TestDetectHostname(t *testing.T) {
os.Setenv("VESPA_HOSTNAME", "foo.bar")
- got, err := FindOurHostname("bar.foo")
+ got, err := FindOurHostname()
assert.Equal(t, "foo.bar", got)
os.Unsetenv("VESPA_HOSTNAME")
- got, err = FindOurHostname("bar.foo.123")
- fmt.Fprintln(os.Stderr, "FindOurHostname() returns: ", got, "with error:", err)
+ got, err = findOurHostnameFrom("bar.foo.123")
+ fmt.Fprintln(os.Stderr, "findOurHostname from bar.foo.123 returns:", got, "with error:", err)
assert.NotEqual(t, "", got)
parts := strings.Split(got, ".")
if len(parts) > 1 {
- expanded, err2 := FindOurHostname(parts[0])
- fmt.Fprintln(os.Stderr, "FindOurHostname(", parts[0], ") returns: ", expanded, "with error:", err2)
+ expanded, err2 := findOurHostnameFrom(parts[0])
+ fmt.Fprintln(os.Stderr, "findOurHostname from", parts[0], "returns:", expanded, "with error:", err2)
assert.Equal(t, got, expanded)
}
+ got, err = FindOurHostname()
+ assert.NotEqual(t, "", got)
+ fmt.Fprintln(os.Stderr, "FindOurHostname() returns:", got, "with error:", err)
}