@@ -162,14 +162,14 @@ func craftPartialIptablesCommentFromSelector(ns string, selector *metav1.LabelSe
162162
163163func translateIngress (ns string , policyName string , targetSelector metav1.LabelSelector , rules []networkingv1.NetworkPolicyIngressRule ) ([]string , []string , []string , [][]string , []* iptm.IptEntry ) {
164164 var (
165- sets []string // ipsets with type: net:hash
166- namedPorts []string // ipsets with type: hash:ip,port
167- lists []string // ipsets with type: list:set
168- ipCidrs [][]string
169- entries []* iptm.IptEntry
170- fromRuleEntries []* iptm.IptEntry
171- addedCidrEntry bool // all cidr entry will be added in one set per from/to rule
172- addedIngressFromEntry , addedPortEntry bool // add drop entries at the end of the chain when there are non ALLOW-ALL* rules
165+ sets []string // ipsets with type: net:hash
166+ namedPorts []string // ipsets with type: hash:ip,port
167+ lists []string // ipsets with type: list:set
168+ ipCidrs [][]string
169+ entries []* iptm.IptEntry
170+ fromRuleEntries []* iptm.IptEntry
171+ addedCidrEntry bool // all cidr entry will be added in one set per from/to rule
172+ addedPortEntry bool // add drop entries at the end of the chain when there are non ALLOW-ALL* rules
173173 )
174174
175175 log .Logf ("started parsing ingress rule" )
@@ -308,7 +308,6 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
308308 // TODO move IP cidrs rule to allow based only
309309 ipCidrs [i ] = append (ipCidrs [i ], except + util .IpsetNomatch )
310310 }
311- addedIngressFromEntry = true
312311 }
313312 if j != 0 && addedCidrEntry {
314313 continue
@@ -408,7 +407,6 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
408407 "-TO-" + targetSelectorComment ,
409408 )
410409 fromRuleEntries = append (fromRuleEntries , entry )
411- addedIngressFromEntry = true
412410 }
413411 addedCidrEntry = true
414412 }
@@ -524,7 +522,6 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
524522 "-TO-" + targetSelectorComment ,
525523 )
526524 entries = append (entries , entry )
527- addedIngressFromEntry = true
528525 }
529526 continue
530527 }
@@ -627,7 +624,6 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
627624 "-TO-" + targetSelectorComment ,
628625 )
629626 entries = append (entries , entry )
630- addedIngressFromEntry = true
631627 }
632628 continue
633629 }
@@ -758,7 +754,6 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
758754 "-TO-" + targetSelectorComment ,
759755 )
760756 entries = append (entries , entry )
761- addedIngressFromEntry = true
762757 }
763758 }
764759
@@ -790,75 +785,20 @@ func translateIngress(ns string, policyName string, targetSelector metav1.LabelS
790785 entries = append (fromRuleEntries , entries ... )
791786 }
792787
793- if addedPortEntry && ! addedIngressFromEntry {
794- entry := & iptm.IptEntry {
795- Chain : util .IptablesAzureIngressPortChain ,
796- Specs : append ([]string (nil ), targetSelectorIptEntrySpec ... ),
797- IsJumpEntry : true ,
798- }
799- entry .Specs = append (
800- entry .Specs ,
801- util .IptablesJumpFlag ,
802- util .IptablesAzureIngressDropsChain ,
803- util .IptablesModuleFlag ,
804- util .IptablesCommentModuleFlag ,
805- util .IptablesCommentFlag ,
806- "ALLOW-ALL-TO-" +
807- targetSelectorComment +
808- "-TO-JUMP-TO-" + util .IptablesAzureIngressDropsChain ,
809- )
810- entries = append (entries , entry )
811- } else if addedIngressFromEntry {
812- portEntry := & iptm.IptEntry {
813- Chain : util .IptablesAzureIngressPortChain ,
814- Specs : append ([]string (nil ), targetSelectorIptEntrySpec ... ),
815- IsJumpEntry : true ,
816- }
817- portEntry .Specs = append (
818- portEntry .Specs ,
819- util .IptablesJumpFlag ,
820- util .IptablesAzureIngressFromChain ,
821- util .IptablesModuleFlag ,
822- util .IptablesCommentModuleFlag ,
823- util .IptablesCommentFlag ,
824- "ALLOW-ALL-TO-" +
825- targetSelectorComment +
826- "-TO-JUMP-TO-" + util .IptablesAzureIngressFromChain ,
827- )
828- entries = append (entries , portEntry )
829- entry := & iptm.IptEntry {
830- Chain : util .IptablesAzureIngressFromChain ,
831- Specs : append ([]string (nil ), targetSelectorIptEntrySpec ... ),
832- IsJumpEntry : true ,
833- }
834- entry .Specs = append (
835- entry .Specs ,
836- util .IptablesJumpFlag ,
837- util .IptablesAzureIngressDropsChain ,
838- util .IptablesModuleFlag ,
839- util .IptablesCommentModuleFlag ,
840- util .IptablesCommentFlag ,
841- "ALLOW-ALL-TO-" +
842- targetSelectorComment +
843- "-TO-JUMP-TO-" + util .IptablesAzureIngressDropsChain ,
844- )
845- entries = append (entries , entry )
846- }
847-
848788 log .Logf ("finished parsing ingress rule" )
849789 return util .DropEmptyFields (sets ), util .DropEmptyFields (namedPorts ), util .DropEmptyFields (lists ), ipCidrs , entries
850790}
851791
852792func translateEgress (ns string , policyName string , targetSelector metav1.LabelSelector , rules []networkingv1.NetworkPolicyEgressRule ) ([]string , []string , []string , [][]string , []* iptm.IptEntry ) {
853793 var (
854- sets []string // ipsets with type: net:hash
855- namedPorts []string // ipsets with type: hash:ip,port
856- lists []string // ipsets with type: list:set
857- ipCidrs [][]string
858- entries []* iptm.IptEntry
859- toRuleEntries []* iptm.IptEntry
860- addedCidrEntry bool // all cidr entry will be added in one set per from/to rule
861- addedEgressToEntry , addedPortEntry bool // add drop entry when there are non ALLOW-ALL* rules
794+ sets []string // ipsets with type: net:hash
795+ namedPorts []string // ipsets with type: hash:ip,port
796+ lists []string // ipsets with type: list:set
797+ ipCidrs [][]string
798+ entries []* iptm.IptEntry
799+ toRuleEntries []* iptm.IptEntry
800+ addedCidrEntry bool // all cidr entry will be added in one set per from/to rule
801+ addedPortEntry bool // add drop entry when there are non ALLOW-ALL* rules
862802 )
863803
864804 log .Logf ("started parsing egress rule" )
@@ -993,7 +933,6 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
993933 // TODO move IP cidrs rule to allow based only
994934 ipCidrs [i ] = append (ipCidrs [i ], except + util .IpsetNomatch )
995935 }
996- addedEgressToEntry = true
997936 }
998937 if j != 0 && addedCidrEntry {
999938 continue
@@ -1099,7 +1038,6 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
10991038 "-FROM-" + targetSelectorComment ,
11001039 )
11011040 toRuleEntries = append (toRuleEntries , entry )
1102- addedEgressToEntry = true
11031041 }
11041042 addedCidrEntry = true
11051043 }
@@ -1215,7 +1153,6 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
12151153 "-TO-" + iptPartialNsComment ,
12161154 )
12171155 entries = append (entries , entry )
1218- addedEgressToEntry = true
12191156 }
12201157 continue
12211158 }
@@ -1318,7 +1255,6 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
13181255 "-TO-" + iptPartialPodComment ,
13191256 )
13201257 entries = append (entries , entry )
1321- addedEgressToEntry = true
13221258 }
13231259 continue
13241260 }
@@ -1449,7 +1385,6 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
14491385 "-AND-" + iptPartialPodComment ,
14501386 )
14511387 entries = append (entries , entry )
1452- addedEgressToEntry = true
14531388 }
14541389 }
14551390
@@ -1482,61 +1417,6 @@ func translateEgress(ns string, policyName string, targetSelector metav1.LabelSe
14821417 entries = append (toRuleEntries , entries ... )
14831418 }
14841419
1485- if addedPortEntry && ! addedEgressToEntry {
1486- entry := & iptm.IptEntry {
1487- Chain : util .IptablesAzureEgressPortChain ,
1488- Specs : append ([]string (nil ), targetSelectorIptEntrySpec ... ),
1489- IsJumpEntry : true ,
1490- }
1491- entry .Specs = append (
1492- entry .Specs ,
1493- util .IptablesJumpFlag ,
1494- util .IptablesAzureEgressDropsChain ,
1495- util .IptablesModuleFlag ,
1496- util .IptablesCommentModuleFlag ,
1497- util .IptablesCommentFlag ,
1498- "ALLOW-ALL-FROM-" +
1499- targetSelectorComment +
1500- "-TO-JUMP-TO-" + util .IptablesAzureEgressDropsChain ,
1501- )
1502- entries = append (entries , entry )
1503- } else if addedEgressToEntry {
1504- portEntry := & iptm.IptEntry {
1505- Chain : util .IptablesAzureEgressPortChain ,
1506- Specs : append ([]string (nil ), targetSelectorIptEntrySpec ... ),
1507- IsJumpEntry : true ,
1508- }
1509- portEntry .Specs = append (
1510- portEntry .Specs ,
1511- util .IptablesJumpFlag ,
1512- util .IptablesAzureEgressToChain ,
1513- util .IptablesModuleFlag ,
1514- util .IptablesCommentModuleFlag ,
1515- util .IptablesCommentFlag ,
1516- "ALLOW-ALL-FROM-" +
1517- targetSelectorComment +
1518- "-TO-JUMP-TO-" + util .IptablesAzureEgressToChain ,
1519- )
1520- entries = append (entries , portEntry )
1521- entry := & iptm.IptEntry {
1522- Chain : util .IptablesAzureEgressToChain ,
1523- Specs : append ([]string (nil ), targetSelectorIptEntrySpec ... ),
1524- IsJumpEntry : true ,
1525- }
1526- entry .Specs = append (
1527- entry .Specs ,
1528- util .IptablesJumpFlag ,
1529- util .IptablesAzureEgressDropsChain ,
1530- util .IptablesModuleFlag ,
1531- util .IptablesCommentModuleFlag ,
1532- util .IptablesCommentFlag ,
1533- "ALLOW-ALL-FROM-" +
1534- targetSelectorComment +
1535- "-TO-JUMP-TO-" + util .IptablesAzureEgressDropsChain ,
1536- )
1537- entries = append (entries , entry )
1538- }
1539-
15401420 log .Logf ("finished parsing egress rule" )
15411421 return util .DropEmptyFields (sets ), util .DropEmptyFields (namedPorts ), util .DropEmptyFields (lists ), ipCidrs , entries
15421422}
0 commit comments