@@ -78,10 +78,17 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(networkPoliciesInfo []
78
78
79
79
activePodFwChains := make (map [string ]bool )
80
80
81
- dropUnmarkedTrafficRules := func (podName , podNamespace , podFwChainName string ) {
82
- for _ , filterTableRules := range npc .filterTableRules {
81
+ dropUnmarkedTrafficRules := func (pod podInfo , podFwChainName string ) {
82
+ for ipFamily , filterTableRules := range npc .filterTableRules {
83
+ _ , err := getPodIPForFamily (pod , ipFamily )
84
+ if err != nil {
85
+ klog .V (2 ).Infof ("unable to get address for pod: %s -- skipping drop rules for pod " +
86
+ "(this is normal for pods that are not dual-stack)" , err .Error ())
87
+ continue
88
+ }
89
+
83
90
// add rule to log the packets that will be dropped due to network policy enforcement
84
- comment := "\" rule to log dropped traffic POD name:" + podName + " namespace: " + podNamespace + "\" "
91
+ comment := "\" rule to log dropped traffic POD name:" + pod . name + " namespace: " + pod . namespace + "\" "
85
92
args := []string {"-A" , podFwChainName , "-m" , "comment" , "--comment" , comment ,
86
93
"-m" , "mark" , "!" , "--mark" , "0x10000/0x10000" , "-j" , "NFLOG" ,
87
94
"--nflog-group" , "100" , "-m" , "limit" , "--limit" , "10/minute" , "--limit-burst" , "10" , "\n " }
@@ -93,7 +100,8 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(networkPoliciesInfo []
93
100
filterTableRules .WriteString (strings .Join (args , " " ))
94
101
95
102
// add rule to DROP if no applicable network policy permits the traffic
96
- comment = "\" rule to REJECT traffic destined for POD name:" + podName + " namespace: " + podNamespace + "\" "
103
+ comment = "\" rule to REJECT traffic destined for POD name:" + pod .name + " namespace: " +
104
+ pod .namespace + "\" "
97
105
args = []string {"-A" , podFwChainName , "-m" , "comment" , "--comment" , comment ,
98
106
"-m" , "mark" , "!" , "--mark" , "0x10000/0x10000" , "-j" , "REJECT" , "\n " }
99
107
filterTableRules .WriteString (strings .Join (args , " " ))
@@ -113,7 +121,17 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(networkPoliciesInfo []
113
121
114
122
// ensure pod specific firewall chain exist for all the pods that need ingress firewall
115
123
podFwChainName := podFirewallChainName (pod .namespace , pod .name , version )
116
- for _ , filterTableRules := range npc .filterTableRules {
124
+ for ipFamily , filterTableRules := range npc .filterTableRules {
125
+ _ , err := getPodIPForFamily (pod , ipFamily )
126
+ if err != nil {
127
+ // If the pod doesn't have an address in this family we skip it here and all the various places below
128
+ // because there won't be a valid source or destination address for iptables, and it will stop iptables
129
+ // restore actions from completing successfully
130
+ klog .Infof ("unable to get address for pod: %s -- skipping pod chain for pod " +
131
+ "(this is normal for pods that are not dual-stack)" , err .Error ())
132
+ continue
133
+ }
134
+
117
135
filterTableRules .WriteString (":" + podFwChainName + "\n " )
118
136
}
119
137
@@ -128,9 +146,16 @@ func (npc *NetworkPolicyController) syncPodFirewallChains(networkPoliciesInfo []
128
146
// setup rules to intercept inbound traffic to the pods
129
147
npc .interceptPodOutboundTraffic (pod , podFwChainName )
130
148
131
- dropUnmarkedTrafficRules (pod .name , pod .namespace , podFwChainName )
149
+ dropUnmarkedTrafficRules (pod , podFwChainName )
150
+
151
+ for ipFamily , filterTableRules := range npc .filterTableRules {
152
+ _ , err := getPodIPForFamily (pod , ipFamily )
153
+ if err != nil {
154
+ klog .V (2 ).Infof ("unable to get address for pod: %s -- skipping accept rules for pod " +
155
+ "(this is normal for pods that are not dual-stack)" , err .Error ())
156
+ continue
157
+ }
132
158
133
- for _ , filterTableRules := range npc .filterTableRules {
134
159
// set mark to indicate traffic from/to the pod passed network policies.
135
160
// Mark will be checked to explicitly ACCEPT the traffic
136
161
comment := "\" set mark to ACCEPT traffic that comply to network policies\" "
@@ -151,13 +176,13 @@ func (npc *NetworkPolicyController) setupPodNetpolRules(pod podInfo, podFwChainN
151
176
hasEgressPolicy := false
152
177
153
178
for ipFamily , filterTableRules := range npc .filterTableRules {
154
- var ip string
155
- switch ipFamily {
156
- case api .IPv4Protocol :
157
- ip , _ = getPodIPv4Address (pod )
158
- case api .IPv6Protocol :
159
- ip , _ = getPodIPv6Address (pod )
179
+ ip , err := getPodIPForFamily (pod , ipFamily )
180
+ if err != nil {
181
+ klog .V (2 ).Infof ("unable to get address for pod: %s -- skipping iptables policy for pod " +
182
+ "(this is normal for pods that are not dual-stack)" , err .Error ())
183
+ continue
160
184
}
185
+
161
186
// add entries in pod firewall to run through applicable network policies
162
187
for _ , policy := range networkPoliciesInfo {
163
188
if _ , ok := policy .targetPods [pod .ip ]; ! ok {
@@ -187,7 +212,7 @@ func (npc *NetworkPolicyController) setupPodNetpolRules(pod podInfo, podFwChainN
187
212
// if pod does not have any network policy which applies rules for pod's ingress traffic
188
213
// then apply default network policy
189
214
if ! hasIngressPolicy {
190
- comment := "\" run through default ingress network policy chain\" "
215
+ comment := "\" run through default ingress network policy chain\" "
191
216
args := []string {"-I" , podFwChainName , "1" , "-d" , ip , "-m" , "comment" , "--comment" , comment ,
192
217
"-j" , kubeDefaultNetpolChain , "\n " }
193
218
filterTableRules .WriteString (strings .Join (args , " " ))
@@ -196,7 +221,7 @@ func (npc *NetworkPolicyController) setupPodNetpolRules(pod podInfo, podFwChainN
196
221
// if pod does not have any network policy which applies rules for pod's egress traffic
197
222
// then apply default network policy
198
223
if ! hasEgressPolicy {
199
- comment := "\" run through default egress network policy chain\" "
224
+ comment := "\" run through default egress network policy chain\" "
200
225
args := []string {"-I" , podFwChainName , "1" , "-s" , ip , "-m" , "comment" , "--comment" , comment ,
201
226
"-j" , kubeDefaultNetpolChain , "\n " }
202
227
filterTableRules .WriteString (strings .Join (args , " " ))
@@ -228,12 +253,11 @@ func (npc *NetworkPolicyController) setupPodNetpolRules(pod podInfo, podFwChainN
228
253
229
254
func (npc * NetworkPolicyController ) interceptPodInboundTraffic (pod podInfo , podFwChainName string ) {
230
255
for ipFamily , filterTableRules := range npc .filterTableRules {
231
- var ip string
232
- switch ipFamily {
233
- case api .IPv4Protocol :
234
- ip , _ = getPodIPv4Address (pod )
235
- case api .IPv6Protocol :
236
- ip , _ = getPodIPv6Address (pod )
256
+ ip , err := getPodIPForFamily (pod , ipFamily )
257
+ if err != nil {
258
+ klog .V (2 ).Infof ("unable to get address for pod: %s -- skipping iptables inbound intercept " +
259
+ "policy for pod (this is normal for pods that are not dual-stack)" , err .Error ())
260
+ continue
237
261
}
238
262
239
263
// ensure there is rule in filter table and FORWARD chain to jump to pod specific firewall chain
@@ -266,12 +290,11 @@ func (npc *NetworkPolicyController) interceptPodInboundTraffic(pod podInfo, podF
266
290
// firewall chain corresponding to the pod so that egress network policies are enforced
267
291
func (npc * NetworkPolicyController ) interceptPodOutboundTraffic (pod podInfo , podFwChainName string ) {
268
292
for ipFamily , filterTableRules := range npc .filterTableRules {
269
- var ip string
270
- switch ipFamily {
271
- case api .IPv4Protocol :
272
- ip , _ = getPodIPv4Address (pod )
273
- case api .IPv6Protocol :
274
- ip , _ = getPodIPv6Address (pod )
293
+ ip , err := getPodIPForFamily (pod , ipFamily )
294
+ if err != nil {
295
+ klog .V (2 ).Infof ("unable to get address for pod: %s -- skipping iptables outbound intercept " +
296
+ "policy for pod (this is normal for pods that are not dual-stack)" , err .Error ())
297
+ continue
275
298
}
276
299
277
300
for _ , chain := range defaultChains {
0 commit comments