Skip to content

Commit 9a92164

Browse files
authored
Merge pull request #73 from erikh/env-or-default
Small patches to enable non-localhost controller development
2 parents 4acf569 + 71913f4 commit 9a92164

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

main.go

Lines changed: 38 additions & 5 deletions
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"
@@ -68,13 +69,13 @@ func main() {
6869
logJSON bool
6970
)
7071

71-
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
72-
flag.StringVar(&eventsAddr, "events-addr", "", "The address of the events receiver.")
72+
flag.StringVar(&metricsAddr, "metrics-addr", envOrDefault("METRICS_ADDR", ":8080"), "The address the metric endpoint binds to.")
73+
flag.StringVar(&eventsAddr, "events-addr", envOrDefault("EVENTS_ADDR", ""), "The address of the events receiver.")
7374
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
7475
"Enable leader election for controller manager. "+
7576
"Enabling this will ensure there is only one active controller manager.")
76-
flag.StringVar(&storagePath, "storage-path", "", "The local storage path.")
77-
flag.StringVar(&storageAddr, "storage-addr", ":9090", "The address the static file server binds to.")
77+
flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""), "The local storage path.")
78+
flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"), "The address the static file server binds to.")
7879
flag.IntVar(&concurrent, "concurrent", 2, "The number of concurrent reconciles per controller.")
7980
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
8081

@@ -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",
@@ -190,3 +214,12 @@ func mustInitStorage(path string, storageAddr string, l logr.Logger) *controller
190214

191215
return storage
192216
}
217+
218+
func envOrDefault(envName, dflt string) string {
219+
ret := os.Getenv(envName)
220+
if ret != "" {
221+
return ret
222+
}
223+
224+
return dflt
225+
}

0 commit comments

Comments
 (0)