Skip to content

Commit

Permalink
feat: iface support regexp
Browse files Browse the repository at this point in the history
(cherry picked from commit 240cd80)
  • Loading branch information
oilbeater committed Nov 5, 2020
1 parent bf860e2 commit 24b97cb
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"os/exec"
"regexp"
"strings"
"syscall"

Expand Down Expand Up @@ -130,8 +131,9 @@ func (config *Configuration) initNicConfig() error {
}
}

iface, err := net.InterfaceByName(config.Iface)
iface, err := findInterface(config.Iface)
if err != nil {
klog.Errorf("failed to find iface %s, %v", config.Iface, err)
return err
}
if config.MTU == 0 {
Expand All @@ -155,6 +157,27 @@ func (config *Configuration) initNicConfig() error {
return setEncapIP(strings.Split(addrs[0].String(), "/")[0])
}

func findInterface(ifaceStr string) (*net.Interface, error) {
ifaceRegex, err := regexp.Compile("(" + strings.Join(strings.Split(ifaceStr, ","), ")|(") + ")")
if err != nil {
klog.Errorf("Invalid interface regex %s", ifaceStr)
return nil, err
}
ifaces, err := net.Interfaces()
if err != nil {
klog.Errorf("failed to list interfaces, %v", err)
return nil, err
}
for _, iface := range ifaces {
if ifaceRegex.MatchString(iface.Name) {
klog.Infof("use %s as tunnel interface", iface.Name)
return &iface, nil
}
}
klog.Errorf("network interface %s not exist", ifaceStr)
return nil, fmt.Errorf("network interface %s not exist", ifaceStr)
}

func (config *Configuration) initKubeClient() error {
var cfg *rest.Config
var err error
Expand Down

0 comments on commit 24b97cb

Please sign in to comment.