ソースを参照

Add support for configuration via env variables

All command line flags can be set via environment variables.
Given a cmd line flag like --etcd-endpoint, corresponding variable
becomes FLANNELD_ETCD_ENDPOINT. That is, FLANNELD_ prefix is appended
with upper case flag name that has dashes replaced with underscores.

Fixes #53
Eugene Yakubovich 10 年 前
コミット
bba5ec322c
1 ファイル変更24 行追加0 行削除
  1. 24 0
      main.go

+ 24 - 0
main.go

@@ -45,6 +45,28 @@ func init() {
 	flag.BoolVar(&opts.version, "version", false, "print version and exit")
 }
 
+// TODO: This is yet another copy (others found in etcd, fleet) -- Pull it out!
+// flagsFromEnv parses all registered flags in the given flagset,
+// and if they are not already set it attempts to set their values from
+// environment variables. Environment variables take the name of the flag but
+// are UPPERCASE, have the given prefix, and any dashes are replaced by
+// underscores - for example: some-flag => PREFIX_SOME_FLAG
+func flagsFromEnv(prefix string, fs *flag.FlagSet) {
+	alreadySet := make(map[string]bool)
+	fs.Visit(func(f *flag.Flag) {
+		alreadySet[f.Name] = true
+	})
+	fs.VisitAll(func(f *flag.Flag) {
+		if !alreadySet[f.Name] {
+			key := strings.ToUpper(prefix + "_" + strings.Replace(f.Name, "-", "_", -1))
+			val := os.Getenv(key)
+			if val != "" {
+				fs.Set(f.Name, val)
+			}
+		}
+	})
+}
+
 func writeSubnetFile(sn *backend.SubnetDef) error {
 	// Write out the first usable IP by incrementing
 	// sn.IP by one
@@ -195,6 +217,8 @@ func main() {
 		os.Exit(0)
 	}
 
+	flagsFromEnv("FLANNELD", flag.CommandLine)
+
 	be, err := newBackend()
 	if err != nil {
 		log.Info(err)