Skip to content

Commit 77788d6

Browse files
author
Erik Hollensbe
committed
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 <[email protected]>
1 parent 4acf569 commit 77788d6

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ func main() {
6868
logJSON bool
6969
)
7070

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.")
71+
flag.StringVar(&metricsAddr, "metrics-addr", envOrDefault("METRICS_ADDR", ":8080"), "The address the metric endpoint binds to.")
72+
flag.StringVar(&eventsAddr, "events-addr", envOrDefault("EVENTS_ADDR", ""), "The address of the events receiver.")
7373
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
7474
"Enable leader election for controller manager. "+
7575
"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.")
76+
flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""), "The local storage path.")
77+
flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"), "The address the static file server binds to.")
7878
flag.IntVar(&concurrent, "concurrent", 2, "The number of concurrent reconciles per controller.")
7979
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
8080

@@ -190,3 +190,12 @@ func mustInitStorage(path string, storageAddr string, l logr.Logger) *controller
190190

191191
return storage
192192
}
193+
194+
func envOrDefault(envName, dflt string) string {
195+
ret := os.Getenv(envName)
196+
if ret != "" {
197+
return ret
198+
}
199+
200+
return dflt
201+
}

0 commit comments

Comments
 (0)