Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit 0b59cda

Browse files
chris-h-phillipspingles
authored andcommitted
Supports use of ! prefix for interface name (#54)
There are some CNI implementations that make use of multiple Elastic Network Interfaces and secondary IPs to assign pods ips directly from the VPC ranges. For example: https://github.com/aws/amazon-vpc-cni-k8s https://github.com/lyft/cni-ipvlan-vpc-k8s For kiam to function correctly, it is necessary to have an iptables rule that applies to all of the interfaces that pod traffic may come from. And since these interfaces may be added and removed on demand, it is necessary to have DNAT rules that will continue to work as interfaces come and go. iptables supports inverted matching for interface names which can be useful to include all but certain interfaces in rules. For example: iptables --append PREROUTING --protocol tcp \ --destination 169.254.169.254 --dport 80 \ \! -i loopback --jump DNAT --table nat \ --to-destination 10.100.100.3:8181 will apply the DNAT rule to all interfaces except the loopback. This change puts the "!" for inverting the interface before the name of the interface in the rules spec that inverted rules work as intended.
1 parent cca30c4 commit 0b59cda

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ Please also make note of how to configure IAM in your AWS account; notes in [doc
4242
Kiam is split into two processes that run independently.
4343

4444
### Agent
45-
This is the process that would typically be deployed as a DaemonSet to ensure that Pods have no access to the AWS Metadata API. Instead, the agent runs an HTTP proxy which intercepts credentials requests and passes on anything else.
45+
This is the process that would typically be deployed as a DaemonSet to ensure that Pods have no access to the AWS Metadata API. Instead, the agent runs an HTTP proxy which intercepts credentials requests and passes on anything else. An DNAT iptables [rule](cmd/agent/iptables.go) is required to intercept the traffic. The agent is capable of adding and removing the required rule for you through use of the `--iptables` [flag](cmd/agent/main.go). This is the name of the interface where pod traffic originates and it is different for the various CNI implementations. The flag also supports the `!` prefix for inverted matches should you need to match all but one interface.
46+
47+
##### Typical CNI Interface Names #####
48+
49+
| CNI | Interface | Notes |
50+
|-----|-----------|-------|
51+
| [cni-ipvlan-vpc-k8s](https://github.com/lyft/cni-ipvlan-vpc-k8s) | `!eth0` | This CNI plugin attaches multiple ENIs to the instance. Typically eth1-ethN (N depends on the instance type) are used for pods which leaves eth0 for the kubernetes control plane. The ! prefix on the interface name inverts the match so metadata service traffic from all interfaces except eth0 will be sent to the kiam agent. |
52+
| [weave](https://www.weave.works/docs/net/latest/kubernetes/kube-addon/) | `weave` | |
53+
4654

4755
### Server
4856
This process is responsible for connecting to the Kubernetes API Servers to watch Pods and communicating with AWS STS to request credentials. It also maintains a cache of credentials for roles currently in use by running pods- ensuring that credentials are refreshed every few minutes and stored in advance of Pods needing them.

cmd/agent/iptables.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616
import (
1717
"fmt"
1818
"github.com/coreos/go-iptables/iptables"
19+
"strings"
1920
)
2021

2122
type rules struct {
@@ -42,14 +43,19 @@ func (r *rules) Add() error {
4243
}
4344

4445
func (r *rules) ruleSpec() []string {
45-
return []string{
46+
rules := []string{
4647
"-p", "tcp",
4748
"-d", metadataAddress,
4849
"--dport", "80",
4950
"-j", "DNAT",
5051
"--to-destination", r.kiamAddress(),
51-
"-i", r.hostInterface,
5252
}
53+
if strings.HasPrefix(r.hostInterface, "!") {
54+
rules = append(rules, "!")
55+
}
56+
rules = append(rules, "-i", strings.TrimPrefix(r.hostInterface, "!"))
57+
58+
return rules
5359
}
5460

5561
func (r *rules) Remove() error {

0 commit comments

Comments
 (0)