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 10, 2021
1 parent 0674908 commit d94b75a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 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
17 changes: 14 additions & 3 deletions iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,32 @@ 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...,
)
}

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

if strings.Contains(hostInterface, "+") {
// wildcard networks ignored
return nil
}

if strings.HasPrefix(hostInterface, "!") {
// negated networks ignored
return nil
}

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

func TestCheckInterfaceExistsPassesWithNegated(t *testing.T) {
ifc := "!eth0"
if err := checkInterfaceExists(ifc); err != nil {
t.Error("Should pass with negated interface(s). Interface received:", ifc)
}
}

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

0 comments on commit d94b75a

Please sign in to comment.