From 77788d6718cdb2d7eccd61d1d14762516aeabb54 Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Sun, 5 Jul 2020 17:36:22 +0000 Subject: [PATCH 1/2] Add envOrDefault call to main.go; handle flags with it This call simply accepts an environment variable or the default provided as the *default* to the flag variable. This is intended to be suitable for 12-factor situations as well as allow the commandline to still override it. Signed-off-by: Erik Hollensbe --- main.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 37dbfff8..832234df 100644 --- a/main.go +++ b/main.go @@ -68,13 +68,13 @@ func main() { logJSON bool ) - flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") - flag.StringVar(&eventsAddr, "events-addr", "", "The address of the events receiver.") + flag.StringVar(&metricsAddr, "metrics-addr", envOrDefault("METRICS_ADDR", ":8080"), "The address the metric endpoint binds to.") + flag.StringVar(&eventsAddr, "events-addr", envOrDefault("EVENTS_ADDR", ""), "The address of the events receiver.") flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") - flag.StringVar(&storagePath, "storage-path", "", "The local storage path.") - flag.StringVar(&storageAddr, "storage-addr", ":9090", "The address the static file server binds to.") + flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""), "The local storage path.") + flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"), "The address the static file server binds to.") flag.IntVar(&concurrent, "concurrent", 2, "The number of concurrent reconciles per controller.") flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.") @@ -190,3 +190,12 @@ func mustInitStorage(path string, storageAddr string, l logr.Logger) *controller return storage } + +func envOrDefault(envName, dflt string) string { + ret := os.Getenv(envName) + if ret != "" { + return ret + } + + return dflt +} From 71913f4d8861c746a0f44a7e064140fc00e902ac Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Sun, 5 Jul 2020 17:37:56 +0000 Subject: [PATCH 2/2] 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 --- main.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 832234df..9d4eea22 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ package main import ( "flag" "fmt" + "net" "net/http" "os" "path/filepath" @@ -175,7 +176,30 @@ func mustInitStorage(path string, storageAddr string, l logr.Logger) *controller os.MkdirAll(path, 0777) } - hostname := "localhost" + storageAddr + host, port, err := net.SplitHostPort(storageAddr) + if err != nil { + l.Error(err, "unable to parse storage address") + os.Exit(1) + } + + switch host { + case "": + host = "localhost" + case "0.0.0.0": + host = os.Getenv("HOSTNAME") + if host == "" { + hn, err := os.Hostname() + if err != nil { + l.Error(err, "0.0.0.0 specified in storage addr but hostname is invalid") + os.Exit(1) + } + + host = hn + } + } + + hostname := net.JoinHostPort(host, port) + if os.Getenv("RUNTIME_NAMESPACE") != "" { svcParts := strings.Split(os.Getenv("HOSTNAME"), "-") hostname = fmt.Sprintf("%s.%s",