Skip to content

Commit 2e6a5f6

Browse files
authored
[Swift] Add snat rule to host IP for IMDS for CNS IPAM (#988)
* add snat rule to host IP for IMDS * add comment for rules
1 parent 96bec09 commit 2e6a5f6

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

cni/network/invoker_cns.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,18 @@ func setHostOptions(nwCfg *cni.NetworkConfig, hostSubnetPrefix *net.IPNet, ncSub
161161
}
162162

163163
azureDNSMatch := fmt.Sprintf(" -m addrtype ! --dst-type local -s %s -d %s -p %s --dport %d", ncSubnetPrefix.String(), iptables.AzureDNS, iptables.UDP, iptables.DNSPort)
164+
azureIMDSMatch := fmt.Sprintf(" -m addrtype ! --dst-type local -s %s -d %s -p %s --dport %d", ncSubnetPrefix.String(), iptables.AzureIMDS, iptables.TCP, iptables.HTTPPort)
164165

165-
// TODO remove this rule once we remove adding MASQUEARDE from AgentBaker, check below PR
166-
// https://github.com/Azure/AgentBaker/pull/367/files
167-
podTrafficAccept := fmt.Sprintf(" -m iprange ! --dst-range 168.63.129.16-168.63.129.16 -s %s ", ncSubnetPrefix.String())
168166
snatPrimaryIPJump := fmt.Sprintf("%s --to %s", iptables.Snat, info.ncPrimaryIP)
167+
// we need to snat IMDS traffic to node IP, this sets up snat '--to'
168+
snatHostIPJump := fmt.Sprintf("%s --to %s", iptables.Snat, info.hostPrimaryIP)
169169
options[network.IPTablesKey] = []iptables.IPTableEntry{
170170
iptables.GetCreateChainCmd(iptables.V4, iptables.Nat, iptables.Swift),
171-
iptables.GetInsertIptableRuleCmd(iptables.V4, iptables.Nat, iptables.Postrouting, podTrafficAccept, iptables.Accept),
172171
iptables.GetAppendIptableRuleCmd(iptables.V4, iptables.Nat, iptables.Postrouting, "", iptables.Swift),
172+
// add a snat rule to primary NC IP for DNS
173173
iptables.GetInsertIptableRuleCmd(iptables.V4, iptables.Nat, iptables.Swift, azureDNSMatch, snatPrimaryIPJump),
174+
// add a snat rule to node IP for IMDS http traffic
175+
iptables.GetInsertIptableRuleCmd(iptables.V4, iptables.Nat, iptables.Swift, azureIMDSMatch, snatHostIPJump),
174176
}
175177

176178
return nil

iptables/iptables.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ const (
6767

6868
// known ports
6969
const (
70-
DNSPort = 53
70+
DNSPort = 53
71+
HTTPPort = 80
7172
)
7273

7374
// known protocols
@@ -78,7 +79,8 @@ const (
7879

7980
// known IP's
8081
const (
81-
AzureDNS = "168.63.129.16"
82+
AzureDNS = "168.63.129.16"
83+
AzureIMDS = "169.254.169.254"
8284
)
8385

8486
var DisableIPTableLock bool

0 commit comments

Comments
 (0)