@@ -159,6 +159,7 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
159159 ipCidrs [][]string
160160 entries []* iptm.IptEntry
161161 fromRuleEntries []* iptm.IptEntry
162+ addedCidrEntry bool // all cidr entry will be added in one set per from/to rule
162163 addedIngressFromEntry , addedPortEntry bool // add drop entries at the end of the chain when there are non ALLOW-ALL* rules
163164 )
164165
@@ -291,7 +292,7 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
291292 }
292293 addedIngressFromEntry = true
293294 }
294- if j != 0 {
295+ if j != 0 && addedCidrEntry {
295296 continue
296297 }
297298 if portRuleExists {
@@ -324,7 +325,7 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
324325 util .IptablesCommentModuleFlag ,
325326 util .IptablesCommentFlag ,
326327 "ALLOW-" + cidrIpsetName +
327- "-: -" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
328+ "-AND -" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
328329 "-TO-" + targetSelectorComment ,
329330 )
330331 fromRuleEntries = append (fromRuleEntries , entry )
@@ -353,19 +354,19 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
353354 util .IptablesCommentModuleFlag ,
354355 util .IptablesCommentFlag ,
355356 "ALLOW-" + cidrIpsetName +
356- "-: -" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
357+ "-AND -" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
357358 "-TO-" + targetSelectorComment ,
358359 )
359360 fromRuleEntries = append (fromRuleEntries , entry )
360361 }
361362 }
362363 } else {
363- cidrEntry := & iptm.IptEntry {
364+ entry := & iptm.IptEntry {
364365 Chain : util .IptablesAzureIngressFromChain ,
365366 Specs : append ([]string (nil ), targetSelectorIptEntrySpec ... ),
366367 }
367- cidrEntry .Specs = append (
368- cidrEntry .Specs ,
368+ entry .Specs = append (
369+ entry .Specs ,
369370 util .IptablesModuleFlag ,
370371 util .IptablesSetModuleFlag ,
371372 util .IptablesMatchSetFlag ,
@@ -379,9 +380,10 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
379380 "ALLOW-" + cidrIpsetName +
380381 "-TO-" + targetSelectorComment ,
381382 )
382- fromRuleEntries = append (fromRuleEntries , cidrEntry )
383+ fromRuleEntries = append (fromRuleEntries , entry )
383384 addedIngressFromEntry = true
384385 }
386+ addedCidrEntry = true
385387 }
386388 continue
387389 }
@@ -799,6 +801,7 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
799801 ipCidrs [][]string
800802 entries []* iptm.IptEntry
801803 toRuleEntries []* iptm.IptEntry
804+ addedCidrEntry bool // all cidr entry will be added in one set per from/to rule
802805 addedEgressToEntry , addedPortEntry bool // add drop entry when there are non ALLOW-ALL* rules
803806 )
804807
@@ -927,7 +930,7 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
927930 }
928931 addedEgressToEntry = true
929932 }
930- if j != 0 {
933+ if j != 0 && addedCidrEntry {
931934 continue
932935 }
933936 if portRuleExists {
@@ -960,7 +963,7 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
960963 util .IptablesCommentModuleFlag ,
961964 util .IptablesCommentFlag ,
962965 "ALLOW-" + cidrIpsetName +
963- "-: -" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
966+ "-AND -" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
964967 "-FROM-" + targetSelectorComment ,
965968 )
966969 toRuleEntries = append (toRuleEntries , entry )
@@ -989,30 +992,30 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
989992 util .IptablesCommentModuleFlag ,
990993 util .IptablesCommentFlag ,
991994 "ALLOW-" + cidrIpsetName +
992- "-: -" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
995+ "-AND -" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
993996 "-FROM-" + targetSelectorComment ,
994997 )
995998 toRuleEntries = append (toRuleEntries , entry )
996999 }
9971000 }
9981001 } else {
999- cidrEntry := & iptm.IptEntry {
1002+ entry := & iptm.IptEntry {
10001003 Chain : util .IptablesAzureEgressToChain ,
10011004 }
1002- cidrEntry .Specs = append (
1003- cidrEntry .Specs ,
1005+ entry .Specs = append (
1006+ entry .Specs ,
10041007 targetSelectorIptEntrySpec ... ,
10051008 )
1006- cidrEntry .Specs = append (
1007- cidrEntry .Specs ,
1009+ entry .Specs = append (
1010+ entry .Specs ,
10081011 util .IptablesModuleFlag ,
10091012 util .IptablesSetModuleFlag ,
10101013 util .IptablesMatchSetFlag ,
10111014 util .GetHashedName (cidrIpsetName ),
10121015 util .IptablesDstFlag ,
10131016 )
1014- cidrEntry .Specs = append (
1015- cidrEntry .Specs ,
1017+ entry .Specs = append (
1018+ entry .Specs ,
10161019 util .IptablesJumpFlag ,
10171020 util .IptablesAccept ,
10181021 util .IptablesModuleFlag ,
@@ -1021,9 +1024,10 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
10211024 "ALLOW-" + cidrIpsetName +
10221025 "-FROM-" + targetSelectorComment ,
10231026 )
1024- toRuleEntries = append (toRuleEntries , cidrEntry )
1027+ toRuleEntries = append (toRuleEntries , entry )
10251028 addedEgressToEntry = true
10261029 }
1030+ addedCidrEntry = true
10271031 }
10281032 continue
10291033 }
0 commit comments