Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make supported interfaces configurable #323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions agent/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
32 changes: 22 additions & 10 deletions origin/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions utils/configutil/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ package configutil

import (
"bytes"
"encoding/csv"
"errors"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strings"

"github.com/uber/kraken/utils/stringset"

Expand Down Expand Up @@ -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()
}
8 changes: 2 additions & 6 deletions utils/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down