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

Don't print hairpin_mode error when not using Linux bridges #19490

Merged
merged 1 commit into from
Feb 1, 2016
Merged
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
15 changes: 13 additions & 2 deletions pkg/kubelet/network/hairpin/hairpin.go
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io/ioutil"
"net"
"os"
"path"
"regexp"
"strconv"
Expand All @@ -30,7 +31,8 @@ import (

const (
sysfsNetPath = "/sys/devices/virtual/net"
hairpinModeRelativePath = "brport/hairpin_mode"
brportRelativePath = "brport"
hairpinModeRelativePath = "hairpin_mode"
hairpinEnable = "1"
)

Expand Down Expand Up @@ -95,6 +97,15 @@ func setUpAllInterfaces() error {

func setUpInterface(ifName string) error {
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)
}
19 changes: 15 additions & 4 deletions pkg/kubelet/network/hairpin/hairpin_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"net"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -84,13 +85,23 @@ func TestFindPairInterfaceOfContainerInterface(t *testing.T) {
}
}

func TestSetUpInterface(t *testing.T) {
func TestSetUpInterfaceNonExistent(t *testing.T) {
err := setUpInterface("non-existent")
if err == nil {
t.Errorf("unexpected non-error")
}
hairpinModeFile := fmt.Sprintf("%s/%s/%s", sysfsNetPath, "non-existent", hairpinModeRelativePath)
if !strings.Contains(fmt.Sprintf("%v", err), hairpinModeFile) {
t.Errorf("should have tried to open %s", hairpinModeFile)
deviceDir := fmt.Sprintf("%s/%s", sysfsNetPath, "non-existent")
if !strings.Contains(fmt.Sprintf("%v", err), deviceDir) {
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)
}
}