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

Allow negation of interfaces for iptables rules #320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ different than `docker0` depending on which virtual network you use e.g.
* for kops (on kubenet), use `cbr0`
* for CNI, use `cni0`
* for [EKS](https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html)/[amazon-vpc-cni-k8s](https://github.com/aws/amazon-vpc-cni-k8s), even with calico installed uses `eni+`. (Each pod gets an interface like `eni4c0e15dfb05`)
* If using security groups per pod however, you will need to instead use `!eth0` as pods making use of security groups per pod [will use](https://aws.amazon.com/blogs/containers/introducing-security-groups-for-pods/) `vlan` interfaces as well as the `eni+` interfaces used for other pods.
* for weave use `weave`
* for flannel use `cni0`
* for [kube-router](https://github.com/cloudnativelabs/kube-router) use `kube-bridge`
Expand Down
14 changes: 12 additions & 2 deletions iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ func AddRule(appPort, metadataAddress, hostInterface, hostIP string) error {
return err
}

ruleSpec := []string{"-p", "tcp", "-d", metadataAddress, "--dport", "80",
"-j", "DNAT", "--to-destination", hostIP + ":" + appPort,
}
if strings.HasPrefix(hostInterface, "!") {
ruleSpec = append(ruleSpec, "!")
}
ruleSpec = append(ruleSpec, "-i", strings.TrimPrefix(hostInterface, "!"))
return ipt.AppendUnique(
"nat", "PREROUTING", "-p", "tcp", "-d", metadataAddress, "--dport", "80",
"-j", "DNAT", "--to-destination", hostIP+":"+appPort, "-i", hostInterface,
"nat", "PREROUTING", ruleSpec...,
)
}

Expand All @@ -39,6 +45,10 @@ func checkInterfaceExists(hostInterface string) error {
return nil
}

if strings.HasPrefix(hostInterface, "!") {
hostInterface = strings.TrimPrefix(hostInterface, "!")
}

_, err := net.InterfaceByName(hostInterface)
return err
}
34 changes: 34 additions & 0 deletions iptables/iptables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ func TestCheckInterfaceExistsPassesWithPlus(t *testing.T) {
}
}

func TestCheckInterfaceExistsPassesWithNegated(t *testing.T) {
var ifc string
switch os := runtime.GOOS; os {
case "darwin":
ifc = "!lo0"
case "linux":
ifc = "!lo"
default:
// everything else that we don't know or care about...fail
ifc = "!unknown"
t.Fatalf("%s OS '%s'\n", ifc, os)
}
if err := checkInterfaceExists(ifc); err != nil {
t.Error("Should pass with negated interface(s). Interface received:", ifc)
}
}

func TestCheckInterfaceExistsFailsWithDoubleNegated(t *testing.T) {
var ifc string
switch os := runtime.GOOS; os {
case "darwin":
ifc = "!!lo0"
case "linux":
ifc = "!!lo"
default:
// everything else that we don't know or care about...fail
ifc = "!!unknown"
t.Fatalf("%s OS '%s'\n", ifc, os)
}
if err := checkInterfaceExists(ifc); err == nil {
t.Error("Should fail with invalid interface. Interface received:", ifc)
}
}

func TestAddRule(t *testing.T) {
t.Skip()
}