Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/coreos/go-iptables/iptables"
)

// AddRule adds the required rule to the host's nat table
// AddRule adds the required rule to the host's nat table.
func AddRule(appPort, metadataAddress, hostInterface, hostIP string) error {

if err := CheckInterfaceExists(hostInterface); err != nil {
if err := checkInterfaceExists(hostInterface); err != nil {
return err
}

Expand All @@ -24,18 +24,15 @@ func AddRule(appPort, metadataAddress, hostInterface, hostIP string) error {
return err
}

if err := ipt.AppendUnique(
return ipt.AppendUnique(
"nat", "PREROUTING", "-p", "tcp", "-d", metadataAddress, "--dport", "80",
"-j", "DNAT", "--to-destination", hostIP+":"+appPort, "-i", hostInterface,
); err != nil {
return err
}

return nil
)
}

// CheckInterfaceExists - validates the interface passed exists for the given system, ignores wildcard networks
func CheckInterfaceExists(hostInterface string) error {
// checkInterfaceExists validates the interface passed exists for the given system.
// checkInterfaceExists ignores wildcard networks.
func checkInterfaceExists(hostInterface string) error {

if strings.Contains(hostInterface, "+") {
// wildcard networks ignored
Expand Down
8 changes: 4 additions & 4 deletions iptables/iptables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func TestCheckInterfaceExistsFailsWithBogusInterface(t *testing.T) {
ifc := "bogus0"
if err := CheckInterfaceExists(ifc); err == nil {
if err := checkInterfaceExists(ifc); err == nil {
t.Error("Should fail with invalid interface. Interface received:", ifc)
}
}
Expand All @@ -22,16 +22,16 @@ func TestCheckInterfaceExistsPassesWithValidInterface(t *testing.T) {
default:
// everything else that we don't know or care about...fail
ifc = "unknown"
t.Error("%s OS '%s'\n", ifc, os)
t.Fatalf("%s OS '%s'\n", ifc, os)
}
if err := CheckInterfaceExists(ifc); err != nil {
if err := checkInterfaceExists(ifc); err != nil {
t.Error("Should pass with valid interface. Interface received:", ifc)
}
}

func TestCheckInterfaceExistsPassesWithPlus(t *testing.T) {
ifc := "cali+"
if err := CheckInterfaceExists(ifc); err != nil {
if err := checkInterfaceExists(ifc); err != nil {
t.Error("Should pass with external networking. Interface received:", ifc)
}
}
Expand Down