diff --git a/agent/cmd/cmd.go b/agent/cmd/cmd.go index a5f5d9f2e..90d7cfd3f 100644 --- a/agent/cmd/cmd.go +++ b/agent/cmd/cmd.go @@ -39,14 +39,15 @@ import ( // Flags defines agent CLI flags. type Flags struct { - PeerIP string - PeerPort int - AgentServerPort int - AgentRegistryPort int - ConfigFile string - Zone string - KrakenCluster string - SecretsFile string + PeerIP string + PeerPort int + AgentServerPort int + AgentRegistryPort int + ConfigFile string + Zone string + KrakenCluster string + SecretsFile string + SupportedInterfaces string } // ParseFlags parses agent CLI flags. @@ -68,6 +69,9 @@ func ParseFlags() *Flags { &flags.KrakenCluster, "cluster", "", "cluster name (e.g. prod01-zone1)") flag.StringVar( &flags.SecretsFile, "secrets", "", "path to a secrets YAML file to load into configuration") + flag.StringVar( + &flags.SupportedInterfaces, "supported-interfaces", "eth0,ib0", + "an ordered csv list of ip interfaces from which host ip is determined (e.g. eth0,ib0)") flag.Parse() return &flags } @@ -148,7 +152,11 @@ func Run(flags *Flags, opts ...Option) { go metrics.EmitVersion(stats) if flags.PeerIP == "" { - localIP, err := netutil.GetLocalIP() + supportedInterfaces, err := configutil.ReadAsCSV(flags.SupportedInterfaces) + if err != nil { + log.Fatalf("Error parsing supported interfaces: %s", err) + } + localIP, err := netutil.GetLocalIP(supportedInterfaces) if err != nil { log.Fatalf("Error getting local ip: %s", err) } diff --git a/origin/cmd/cmd.go b/origin/cmd/cmd.go index 29a1a3a43..c78ed5fe5 100644 --- a/origin/cmd/cmd.go +++ b/origin/cmd/cmd.go @@ -50,14 +50,15 @@ import ( // Flags defines origin CLI flags. type Flags struct { - PeerIP string - PeerPort int - BlobServerHostName string - BlobServerPort int - ConfigFile string - Zone string - KrakenCluster string - SecretsFile string + PeerIP string + PeerPort int + BlobServerHostName string + BlobServerPort int + ConfigFile string + Zone string + KrakenCluster string + SecretsFile string + SupportedInterfaces string } // ParseFlags parses origin CLI flags. @@ -79,6 +80,9 @@ func ParseFlags() *Flags { &flags.KrakenCluster, "cluster", "", "cluster name (e.g. prod01-zone1)") flag.StringVar( &flags.SecretsFile, "secrets", "", "path to a secrets YAML file to load into configuration") + flag.StringVar( + &flags.SupportedInterfaces, "supported-interfaces", "eth0,ib0", + "an ordered csv list of ip interfaces from which host ip is determined (e.g. eth0,ib0)") flag.Parse() return &flags } @@ -168,7 +172,11 @@ func Run(flags *Flags, opts ...Option) { log.Infof("Configuring origin with hostname '%s'", hostname) if flags.PeerIP == "" { - localIP, err := netutil.GetLocalIP() + supportedInterfaces, err := configutil.ReadAsCSV(flags.SupportedInterfaces) + if err != nil { + log.Fatalf("Error parsing supported interfaces: %s", err) + } + localIP, err := netutil.GetLocalIP(supportedInterfaces) if err != nil { log.Fatalf("Error getting local ip: %s", err) } @@ -246,7 +254,11 @@ func Run(flags *Flags, opts ...Option) { if !hashRing.Contains(addr) { // When DNS is used for hash ring membership, the members will be IP // addresses instead of hostnames. - ip, err := netutil.GetLocalIP() + supportedInterfaces, err := configutil.ReadAsCSV(flags.SupportedInterfaces) + if err != nil { + log.Fatalf("Error parsing supported interfaces: %s", err) + } + ip, err := netutil.GetLocalIP(supportedInterfaces) if err != nil { log.Fatalf("Error getting local ip: %s", err) } diff --git a/utils/configutil/config.go b/utils/configutil/config.go index 81531523f..961aeb451 100644 --- a/utils/configutil/config.go +++ b/utils/configutil/config.go @@ -55,11 +55,13 @@ package configutil import ( "bytes" + "encoding/csv" "errors" "fmt" "io/ioutil" "path" "path/filepath" + "strings" "github.com/uber/kraken/utils/stringset" @@ -178,3 +180,13 @@ func loadFiles(config interface{}, fnames []string) error { } return nil } + +// ReadAsCSV reads a csv string and return the string slice. +func ReadAsCSV(val string) ([]string, error) { + if val == "" { + return []string{}, nil + } + stringReader := strings.NewReader(val) + csvReader := csv.NewReader(stringReader) + return csvReader.Read() +} diff --git a/utils/netutil/netutil.go b/utils/netutil/netutil.go index 7b518bd80..c37a9e1f6 100644 --- a/utils/netutil/netutil.go +++ b/utils/netutil/netutil.go @@ -20,10 +20,6 @@ import ( "time" ) -// _supportedInterfaces is an ordered list of ip interfaces from which -// host ip is determined. -var _supportedInterfaces = []string{"eth0", "ib0"} - func min(a, b time.Duration) time.Duration { if a < b { return a @@ -65,7 +61,7 @@ func GetIP(host string) (net.IP, error) { } // GetLocalIP returns the ip address of the local machine. -func GetLocalIP() (string, error) { +func GetLocalIP(supportedInterfaces []string) (string, error) { ifaces, err := net.Interfaces() if err != nil { return "", fmt.Errorf("interfaces: %s", err) @@ -95,7 +91,7 @@ func GetLocalIP() (string, error) { break } } - for _, i := range _supportedInterfaces { + for _, i := range supportedInterfaces { if ip, ok := ips[i]; ok { return ip, nil }