Merge pull request #19490 from danwinship/hairpin-error

Auto commit by PR queue bot
This commit is contained in:
k8s-merge-robot 2016-02-01 11:20:26 -08:00
commit a68f952a61
2 changed files with 28 additions and 6 deletions

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"os"
"path" "path"
"regexp" "regexp"
"strconv" "strconv"
@ -30,7 +31,8 @@ import (
const ( const (
sysfsNetPath = "/sys/devices/virtual/net" sysfsNetPath = "/sys/devices/virtual/net"
hairpinModeRelativePath = "brport/hairpin_mode" brportRelativePath = "brport"
hairpinModeRelativePath = "hairpin_mode"
hairpinEnable = "1" hairpinEnable = "1"
) )
@ -95,6 +97,15 @@ func setUpAllInterfaces() error {
func setUpInterface(ifName string) error { func setUpInterface(ifName string) error {
glog.V(3).Infof("Enabling hairpin on interface %s", ifName) glog.V(3).Infof("Enabling hairpin on interface %s", ifName)
hairpinModeFile := path.Join(sysfsNetPath, ifName, hairpinModeRelativePath) ifPath := path.Join(sysfsNetPath, ifName)
if _, err := os.Stat(ifPath); err != nil {
return err
}
brportPath := path.Join(ifPath, brportRelativePath)
if _, err := os.Stat(brportPath); err != nil && os.IsNotExist(err) {
// Device is not on a bridge, so doesn't need hairpin mode
return nil
}
hairpinModeFile := path.Join(brportPath, hairpinModeRelativePath)
return ioutil.WriteFile(hairpinModeFile, []byte(hairpinEnable), 0644) return ioutil.WriteFile(hairpinModeFile, []byte(hairpinEnable), 0644)
} }

View File

@ -20,6 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"os"
"strings" "strings"
"testing" "testing"
@ -84,13 +85,23 @@ func TestFindPairInterfaceOfContainerInterface(t *testing.T) {
} }
} }
func TestSetUpInterface(t *testing.T) { func TestSetUpInterfaceNonExistent(t *testing.T) {
err := setUpInterface("non-existent") err := setUpInterface("non-existent")
if err == nil { if err == nil {
t.Errorf("unexpected non-error") t.Errorf("unexpected non-error")
} }
hairpinModeFile := fmt.Sprintf("%s/%s/%s", sysfsNetPath, "non-existent", hairpinModeRelativePath) deviceDir := fmt.Sprintf("%s/%s", sysfsNetPath, "non-existent")
if !strings.Contains(fmt.Sprintf("%v", err), hairpinModeFile) { if !strings.Contains(fmt.Sprintf("%v", err), deviceDir) {
t.Errorf("should have tried to open %s", hairpinModeFile) t.Errorf("should have tried to open %s", deviceDir)
}
}
func TestSetUpInterfaceNotBridged(t *testing.T) {
err := setUpInterface("lo")
if err != nil {
if os.IsNotExist(err) {
t.Skipf("'lo' device does not exist??? (%v)", err)
}
t.Errorf("unexpected error: %v", err)
} }
} }