Skip to content

Commit 71913f4

Browse files
author
Erik Hollensbe
committed
main.go: Handle returning a non-localhost testing port
If provided, STORAGE_ADDR can specify 0.0.0.0 which means "look up the hostname and use that". Signed-off-by: Erik Hollensbe <[email protected]>
1 parent 77788d6 commit 71913f4

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

main.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"flag"
2121
"fmt"
22+
"net"
2223
"net/http"
2324
"os"
2425
"path/filepath"
@@ -175,7 +176,30 @@ func mustInitStorage(path string, storageAddr string, l logr.Logger) *controller
175176
os.MkdirAll(path, 0777)
176177
}
177178

178-
hostname := "localhost" + storageAddr
179+
host, port, err := net.SplitHostPort(storageAddr)
180+
if err != nil {
181+
l.Error(err, "unable to parse storage address")
182+
os.Exit(1)
183+
}
184+
185+
switch host {
186+
case "":
187+
host = "localhost"
188+
case "0.0.0.0":
189+
host = os.Getenv("HOSTNAME")
190+
if host == "" {
191+
hn, err := os.Hostname()
192+
if err != nil {
193+
l.Error(err, "0.0.0.0 specified in storage addr but hostname is invalid")
194+
os.Exit(1)
195+
}
196+
197+
host = hn
198+
}
199+
}
200+
201+
hostname := net.JoinHostPort(host, port)
202+
179203
if os.Getenv("RUNTIME_NAMESPACE") != "" {
180204
svcParts := strings.Split(os.Getenv("HOSTNAME"), "-")
181205
hostname = fmt.Sprintf("%s.%s",

0 commit comments

Comments
 (0)