Skip to content

Commit

Permalink
Allow negation of interfaces for iptables rules
Browse files Browse the repository at this point in the history
Based on matching functionality in Kiam
Introduced by uswitch/kiam#54
  • Loading branch information
gjtempleton committed Aug 11, 2021
1 parent 0674908 commit 75ba9b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
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()
}

0 comments on commit 75ba9b4

Please sign in to comment.