@@ -144,9 +144,10 @@ func craftPartialIptablesCommentFromSelector(ns string, selector *metav1.LabelSe
144144
145145func translateIngress (ns string , targetSelector metav1.LabelSelector , rules []networkingv1.NetworkPolicyIngressRule ) ([]string , []string , []* iptm.IptEntry ) {
146146 var (
147- sets []string // ipsets with type: net:hash
148- lists []string // ipsets with type: list:set
149- entries []* iptm.IptEntry
147+ sets []string // ipsets with type: net:hash
148+ lists []string // ipsets with type: list:set
149+ entries []* iptm.IptEntry
150+ fromRuleEntries []* iptm.IptEntry
150151 addedIngressFromEntry , addedPortEntry bool // add drop entries at the end of the chain when there are non ALLOW-ALL* rules
151152 )
152153
@@ -264,7 +265,7 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
264265 "DROP-" + except +
265266 "-TO-" + targetSelectorComment ,
266267 )
267- entries = append (entries , exceptEntry )
268+ fromRuleEntries = append (fromRuleEntries , exceptEntry )
268269 }
269270 addedIngressFromEntry = true
270271 }
@@ -295,7 +296,7 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
295296 "-:-" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
296297 "-TO-" + targetSelectorComment ,
297298 )
298- entries = append (entries , entry )
299+ fromRuleEntries = append (fromRuleEntries , entry )
299300 }
300301 } else {
301302 cidrEntry := & iptm.IptEntry {
@@ -317,7 +318,7 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
317318 "ALLOW-" + fromRule .IPBlock .CIDR +
318319 "-TO-" + targetSelectorComment ,
319320 )
320- entries = append (entries , cidrEntry )
321+ fromRuleEntries = append (fromRuleEntries , cidrEntry )
321322 addedIngressFromEntry = true
322323 }
323324 }
@@ -569,6 +570,10 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
569570 }
570571 }
571572
573+ if len (fromRuleEntries ) > 0 {
574+ entries = append (entries , fromRuleEntries ... )
575+ }
576+
572577 if addedPortEntry && ! addedIngressFromEntry {
573578 entry := & iptm.IptEntry {
574579 Chain : util .IptablesAzureIngressPortChain ,
@@ -627,9 +632,10 @@ func translateIngress(ns string, targetSelector metav1.LabelSelector, rules []ne
627632
628633func translateEgress (ns string , targetSelector metav1.LabelSelector , rules []networkingv1.NetworkPolicyEgressRule ) ([]string , []string , []* iptm.IptEntry ) {
629634 var (
630- sets []string // ipsets with type: net:hash
631- lists []string // ipsets with type: list:set
632- entries []* iptm.IptEntry
635+ sets []string // ipsets with type: net:hash
636+ lists []string // ipsets with type: list:set
637+ entries []* iptm.IptEntry
638+ toRuleEntries []* iptm.IptEntry
633639 addedEgressToEntry , addedPortEntry bool // add drop entry when there are non ALLOW-ALL* rules
634640 )
635641
@@ -742,7 +748,7 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
742748 "DROP-" + except +
743749 "-FROM-" + targetSelectorComment ,
744750 )
745- entries = append (entries , exceptEntry )
751+ toRuleEntries = append (toRuleEntries , exceptEntry )
746752 }
747753 addedEgressToEntry = true
748754 }
@@ -773,7 +779,7 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
773779 "-:-" + craftPartialIptablesCommentFromPort (portRule , util .IptablesDstPortFlag )+
774780 "-FROM-" + targetSelectorComment ,
775781 )
776- entries = append (entries , entry )
782+ toRuleEntries = append (toRuleEntries , entry )
777783 }
778784 } else {
779785 cidrEntry := & iptm.IptEntry {
@@ -798,7 +804,7 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
798804 "ALLOW-" + toRule .IPBlock .CIDR +
799805 "-FROM-" + targetSelectorComment ,
800806 )
801- entries = append (entries , cidrEntry )
807+ toRuleEntries = append (toRuleEntries , cidrEntry )
802808 addedEgressToEntry = true
803809 }
804810 }
@@ -1051,6 +1057,10 @@ func translateEgress(ns string, targetSelector metav1.LabelSelector, rules []net
10511057 }
10521058 }
10531059
1060+ if len (toRuleEntries ) > 0 {
1061+ entries = append (entries , toRuleEntries ... )
1062+ }
1063+
10541064 if addedPortEntry && ! addedEgressToEntry {
10551065 entry := & iptm.IptEntry {
10561066 Chain : util .IptablesAzureEgressPortChain ,
0 commit comments