Skip to content

Commit

Permalink
make supported interfaces configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Garrybest <[email protected]>
  • Loading branch information
Garrybest committed Jun 17, 2022
1 parent 3a10917 commit c13c3c0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
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

0 comments on commit c13c3c0

Please sign in to comment.