Skip to content

Commit 5cdcc1f

Browse files
committed
modify imds iptables rule
1 parent c7fd5d4 commit 5cdcc1f

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

cns/restserver/internalapi_linux.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ func (service *HTTPRestService) programSNATRules(req *cns.CreateNetworkContainer
2121
defer service.Unlock()
2222

2323
// Parse primary ip and ipnet from nnc
24-
ncPrimaryIP, ncIPNet, _ := net.ParseCIDR(req.IPConfiguration.IPSubnet.IPAddress + "/" + fmt.Sprintf("%d", req.IPConfiguration.IPSubnet.PrefixLength))
24+
// in podsubnet case, ncPrimaryIP is the pod subnet's primary ip
25+
// in vnet scale case, ncPrimaryIP is the node's ip
26+
ncPrimaryIP, _, _ := net.ParseCIDR(req.IPConfiguration.IPSubnet.IPAddress + "/" + fmt.Sprintf("%d", req.IPConfiguration.IPSubnet.PrefixLength))
2527
ipt, err := goiptables.New()
2628
if err != nil {
2729
return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to create iptables interface : %v", err)
@@ -56,9 +58,7 @@ func (service *HTTPRestService) programSNATRules(req *cns.CreateNetworkContainer
5658
}
5759
}
5860

59-
// use any secondary ip + the nnc prefix length to get an iptables rule to allow dns traffic
60-
// in podsubnet case, ncPrimaryIP is the pod subnet's primary ip
61-
// in vnet scale case, ncPrimaryIP is the node's ip
61+
// use any secondary ip + the nnc prefix length to get an iptables rule to allow dns and imds traffic from the pods
6262
for _, v := range req.SecondaryIPConfigs {
6363
// put the ip address in standard cidr form (where we zero out the parts that are not relevant)
6464
_, podSubnet, _ := net.ParseCIDR(v.IPAddress + "/" + fmt.Sprintf("%d", req.IPConfiguration.IPSubnet.PrefixLength))
@@ -87,20 +87,20 @@ func (service *HTTPRestService) programSNATRules(req *cns.CreateNetworkContainer
8787
}
8888
}
8989

90-
// we only need to run this code once as the iptable rule applies to all secondary ip configs in the same subnet
91-
break
92-
}
93-
94-
snatIMDSRuleexist, err := ipt.Exists(iptables.Nat, SWIFT, "-m", "addrtype", "!", "--dst-type", "local", "-s", ncIPNet.String(), "-d", networkutils.AzureIMDS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.HTTPPort), "-j", iptables.Snat, "--to", req.HostPrimaryIP)
95-
if err != nil {
96-
return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to check for existence of SNAT IMDS rule : %v", err)
97-
}
98-
if !snatIMDSRuleexist {
99-
logger.Printf("[Azure CNS] Inserting SNAT IMDS rule ...")
100-
err = ipt.Insert(iptables.Nat, SWIFT, 1, "-m", "addrtype", "!", "--dst-type", "local", "-s", ncIPNet.String(), "-d", networkutils.AzureIMDS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.HTTPPort), "-j", iptables.Snat, "--to", req.HostPrimaryIP)
90+
snatIMDSRuleexist, err := ipt.Exists(iptables.Nat, SWIFT, "-m", "addrtype", "!", "--dst-type", "local", "-s", podSubnet.String(), "-d", networkutils.AzureIMDS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.HTTPPort), "-j", iptables.Snat, "--to", req.HostPrimaryIP)
10191
if err != nil {
102-
return types.FailedToRunIPTableCmd, "[Azure CNS] failed to insert SNAT IMDS rule : " + err.Error()
92+
return types.UnexpectedError, fmt.Sprintf("[Azure CNS] Error. Failed to check for existence of pod SNAT IMDS rule : %v", err)
93+
}
94+
if !snatIMDSRuleexist {
95+
logger.Printf("[Azure CNS] Inserting pod SNAT IMDS rule ...")
96+
err = ipt.Insert(iptables.Nat, SWIFT, 1, "-m", "addrtype", "!", "--dst-type", "local", "-s", podSubnet.String(), "-d", networkutils.AzureIMDS, "-p", iptables.TCP, "--dport", strconv.Itoa(iptables.HTTPPort), "-j", iptables.Snat, "--to", req.HostPrimaryIP)
97+
if err != nil {
98+
return types.FailedToRunIPTableCmd, "[Azure CNS] failed to insert pod SNAT IMDS rule : " + err.Error()
99+
}
103100
}
101+
102+
// we only need to run this code once as the iptable rule applies to all secondary ip configs in the same subnet
103+
break
104104
}
105105

106106
return types.Success, ""

0 commit comments

Comments
 (0)