@@ -242,31 +242,6 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
242242 for _ , fromRule := range rule .From {
243243 // Handle IPBlock field of NetworkPolicyPeer
244244 if fromRule .IPBlock != nil {
245- if len (fromRule .IPBlock .Except ) > 0 {
246- for _ , except := range fromRule .IPBlock .Except {
247- exceptEntry := & iptm.IptEntry {
248- Chain : util .IptablesAzureIngressFromChain ,
249- }
250- exceptEntry .Specs = append (
251- exceptEntry .Specs ,
252- util .IptablesSFlag ,
253- except ,
254- )
255- exceptEntry .Specs = append (exceptEntry .Specs , targetSelectorIptEntrySpec ... )
256- exceptEntry .Specs = append (
257- exceptEntry .Specs ,
258- util .IptablesJumpFlag ,
259- util .IptablesDrop ,
260- util .IptablesModuleFlag ,
261- util .IptablesCommentModuleFlag ,
262- util .IptablesCommentFlag ,
263- "DROP-" + except +
264- "-TO-" + targetSelectorComment ,
265- )
266- fromRuleEntries = append (fromRuleEntries , exceptEntry )
267- }
268- addedIngressFromEntry = true
269- }
270245 if len (fromRule .IPBlock .CIDR ) > 0 {
271246 if portRuleExists {
272247 for _ , portRule := range rule .Ports {
@@ -319,6 +294,31 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
319294 fromRuleEntries = append (fromRuleEntries , cidrEntry )
320295 addedIngressFromEntry = true
321296 }
297+ if len (fromRule .IPBlock .Except ) > 0 {
298+ for _ , except := range fromRule .IPBlock .Except {
299+ exceptEntry := & iptm.IptEntry {
300+ Chain : util .IptablesAzureIngressFromChain ,
301+ }
302+ exceptEntry .Specs = append (
303+ exceptEntry .Specs ,
304+ util .IptablesSFlag ,
305+ except ,
306+ )
307+ exceptEntry .Specs = append (exceptEntry .Specs , targetSelectorIptEntrySpec ... )
308+ exceptEntry .Specs = append (
309+ exceptEntry .Specs ,
310+ util .IptablesJumpFlag ,
311+ util .IptablesDrop ,
312+ util .IptablesModuleFlag ,
313+ util .IptablesCommentModuleFlag ,
314+ util .IptablesCommentFlag ,
315+ "DROP-" + except +
316+ "-TO-" + targetSelectorComment ,
317+ )
318+ fromRuleEntries = append (fromRuleEntries , exceptEntry )
319+ }
320+ addedIngressFromEntry = true
321+ }
322322 }
323323 continue
324324 }
@@ -568,14 +568,17 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
568568 }
569569 }
570570
571+ // prepending fromRuleEntries (which is in reverse order) so that they will retain correct ordering
572+ // of drop->allow... when the rules are beind prepended to their corresponding chain
571573 if len (fromRuleEntries ) > 0 {
572- entries = append (entries , fromRuleEntries ... )
574+ entries = append (fromRuleEntries , entries ... )
573575 }
574576
575577 if addedPortEntry && ! addedIngressFromEntry {
576578 entry := & iptm.IptEntry {
577- Chain : util .IptablesAzureIngressPortChain ,
578- Specs : targetSelectorIptEntrySpec ,
579+ Chain : util .IptablesAzureIngressPortChain ,
580+ Specs : targetSelectorIptEntrySpec ,
581+ IsJumpEntry : true ,
579582 }
580583 entry .Specs = append (
581584 entry .Specs ,
@@ -591,8 +594,9 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
591594 entries = append (entries , entry )
592595 } else if addedIngressFromEntry {
593596 portEntry := & iptm.IptEntry {
594- Chain : util .IptablesAzureIngressPortChain ,
595- Specs : targetSelectorIptEntrySpec ,
597+ Chain : util .IptablesAzureIngressPortChain ,
598+ Specs : targetSelectorIptEntrySpec ,
599+ IsJumpEntry : true ,
596600 }
597601 portEntry .Specs = append (
598602 portEntry .Specs ,
@@ -607,8 +611,9 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
607611 )
608612 entries = append (entries , portEntry )
609613 entry := & iptm.IptEntry {
610- Chain : util .IptablesAzureIngressFromChain ,
611- Specs : targetSelectorIptEntrySpec ,
614+ Chain : util .IptablesAzureIngressFromChain ,
615+ Specs : targetSelectorIptEntrySpec ,
616+ IsJumpEntry : true ,
612617 }
613618 entry .Specs = append (
614619 entry .Specs ,
@@ -724,31 +729,6 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
724729 for _ , toRule := range rule .To {
725730 // Handle IPBlock field of NetworkPolicyPeer
726731 if toRule .IPBlock != nil {
727- if len (toRule .IPBlock .Except ) > 0 {
728- for _ , except := range toRule .IPBlock .Except {
729- exceptEntry := & iptm.IptEntry {
730- Chain : util .IptablesAzureEgressToChain ,
731- Specs : targetSelectorIptEntrySpec ,
732- }
733- exceptEntry .Specs = append (
734- exceptEntry .Specs ,
735- util .IptablesDFlag ,
736- except ,
737- )
738- exceptEntry .Specs = append (
739- exceptEntry .Specs ,
740- util .IptablesJumpFlag ,
741- util .IptablesDrop ,
742- util .IptablesModuleFlag ,
743- util .IptablesCommentModuleFlag ,
744- util .IptablesCommentFlag ,
745- "DROP-" + except +
746- "-FROM-" + targetSelectorComment ,
747- )
748- toRuleEntries = append (toRuleEntries , exceptEntry )
749- }
750- addedEgressToEntry = true
751- }
752732 if len (toRule .IPBlock .CIDR ) > 0 {
753733 if portRuleExists {
754734 for _ , portRule := range rule .Ports {
@@ -804,6 +784,31 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
804784 toRuleEntries = append (toRuleEntries , cidrEntry )
805785 addedEgressToEntry = true
806786 }
787+ if len (toRule .IPBlock .Except ) > 0 {
788+ for _ , except := range toRule .IPBlock .Except {
789+ exceptEntry := & iptm.IptEntry {
790+ Chain : util .IptablesAzureEgressToChain ,
791+ Specs : targetSelectorIptEntrySpec ,
792+ }
793+ exceptEntry .Specs = append (
794+ exceptEntry .Specs ,
795+ util .IptablesDFlag ,
796+ except ,
797+ )
798+ exceptEntry .Specs = append (
799+ exceptEntry .Specs ,
800+ util .IptablesJumpFlag ,
801+ util .IptablesDrop ,
802+ util .IptablesModuleFlag ,
803+ util .IptablesCommentModuleFlag ,
804+ util .IptablesCommentFlag ,
805+ "DROP-" + except +
806+ "-FROM-" + targetSelectorComment ,
807+ )
808+ toRuleEntries = append (toRuleEntries , exceptEntry )
809+ }
810+ addedEgressToEntry = true
811+ }
807812 }
808813 continue
809814 }
@@ -1054,14 +1059,17 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
10541059 }
10551060 }
10561061
1062+ // prepending toRuleEntries (which is in reverse order) so that they will retain correct ordering
1063+ // of drop->allow... when the rules are beind prepended to their corresponding chain
10571064 if len (toRuleEntries ) > 0 {
1058- entries = append (entries , toRuleEntries ... )
1065+ entries = append (toRuleEntries , entries ... )
10591066 }
10601067
10611068 if addedPortEntry && ! addedEgressToEntry {
10621069 entry := & iptm.IptEntry {
1063- Chain : util .IptablesAzureEgressPortChain ,
1064- Specs : targetSelectorIptEntrySpec ,
1070+ Chain : util .IptablesAzureEgressPortChain ,
1071+ Specs : targetSelectorIptEntrySpec ,
1072+ IsJumpEntry : true ,
10651073 }
10661074 entry .Specs = append (
10671075 entry .Specs ,
@@ -1077,8 +1085,9 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
10771085 entries = append (entries , entry )
10781086 } else if addedEgressToEntry {
10791087 portEntry := & iptm.IptEntry {
1080- Chain : util .IptablesAzureEgressPortChain ,
1081- Specs : targetSelectorIptEntrySpec ,
1088+ Chain : util .IptablesAzureEgressPortChain ,
1089+ Specs : targetSelectorIptEntrySpec ,
1090+ IsJumpEntry : true ,
10821091 }
10831092 portEntry .Specs = append (
10841093 portEntry .Specs ,
@@ -1093,8 +1102,9 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
10931102 )
10941103 entries = append (entries , portEntry )
10951104 entry := & iptm.IptEntry {
1096- Chain : util .IptablesAzureEgressToChain ,
1097- Specs : targetSelectorIptEntrySpec ,
1105+ Chain : util .IptablesAzureEgressToChain ,
1106+ Specs : targetSelectorIptEntrySpec ,
1107+ IsJumpEntry : true ,
10981108 }
10991109 entry .Specs = append (
11001110 entry .Specs ,
0 commit comments