@@ -68,8 +68,10 @@ type podInfo struct {
6868}
6969
7070type ingressRule struct {
71- ports []protocolAndPort
72- srcPods []podInfo
71+ matchAllPorts bool
72+ ports []protocolAndPort
73+ matchAllSource bool
74+ srcPods []podInfo
7375}
7476
7577type protocolAndPort struct {
@@ -304,7 +306,7 @@ func (npc *NetworkPolicyController) syncNetworkPolicyChains() (map[string]bool,
304306
305307 // case where only 'ports' details specified but no 'from' details in the ingress rule
306308 // so match on all sources, with specified port and protocol
307- if len ( ingressRule .srcPods ) == 0 && len ( ingressRule .ports ) != 0 {
309+ if ingressRule .matchAllSource && ! ingressRule .matchAllPorts {
308310 for _ , portProtocol := range ingressRule .ports {
309311 comment := "rule to ACCEPT traffic from source pods to dest pods selected by policy name: " +
310312 policy .name + " namespace " + policy .namespace
@@ -322,7 +324,14 @@ func (npc *NetworkPolicyController) syncNetworkPolicyChains() (map[string]bool,
322324
323325 // case where nether ports nor from details are speified in the ingress rule
324326 // so match on all ports, protocol, source IP's
325- if len (ingressRule .srcPods ) == 0 && len (ingressRule .ports ) == 0 {
327+ if ingressRule .matchAllSource && ingressRule .matchAllPorts {
328+
329+ // if no ports or source information is present in spec this is specical case
330+ // where network policy does not allow any traffic
331+ if npc .v1NetworkPolicy {
332+ continue
333+ }
334+
326335 comment := "rule to ACCEPT traffic from source pods to dest pods selected by policy name: " +
327336 policy .name + " namespace " + policy .namespace
328337 args := []string {"-m" , "comment" , "--comment" , comment ,
@@ -655,24 +664,39 @@ func buildNetworkPoliciesInfo() (*[]networkPolicyInfo, error) {
655664 ingressRule := ingressRule {}
656665
657666 ingressRule .ports = make ([]protocolAndPort , 0 )
658- for _ , port := range specIngressRule .Ports {
659- protocolAndPort := protocolAndPort {protocol : string (* port .Protocol ), port : port .Port .String ()}
660- ingressRule .ports = append (ingressRule .ports , protocolAndPort )
667+
668+ // If this field is empty or missing in the spec, this rule matches all ports
669+ if len (specIngressRule .Ports ) == 0 {
670+ ingressRule .matchAllPorts = true
671+ } else {
672+ ingressRule .matchAllPorts = false
673+ for _ , port := range specIngressRule .Ports {
674+ protocolAndPort := protocolAndPort {protocol : string (* port .Protocol ), port : port .Port .String ()}
675+ ingressRule .ports = append (ingressRule .ports , protocolAndPort )
676+ }
661677 }
662678
663679 ingressRule .srcPods = make ([]podInfo , 0 )
664- for _ , peer := range specIngressRule .From {
665- matchingPods , err := watchers .PodWatcher .ListByNamespaceAndLabels (policy .Namespace , peer .PodSelector .MatchLabels )
666- if err == nil {
667- for _ , matchingPod := range matchingPods {
668- ingressRule .srcPods = append (ingressRule .srcPods ,
669- podInfo {ip : matchingPod .Status .PodIP ,
670- name : matchingPod .ObjectMeta .Name ,
671- namespace : matchingPod .ObjectMeta .Namespace ,
672- labels : matchingPod .ObjectMeta .Labels })
680+
681+ // If this field is empty or missing in the spec, this rule matches all sources
682+ if len (specIngressRule .From ) == 0 {
683+ ingressRule .matchAllSource = true
684+ } else {
685+ ingressRule .matchAllSource = false
686+ for _ , peer := range specIngressRule .From {
687+ matchingPods , err := watchers .PodWatcher .ListByNamespaceAndLabels (policy .Namespace , peer .PodSelector .MatchLabels )
688+ if err == nil {
689+ for _ , matchingPod := range matchingPods {
690+ ingressRule .srcPods = append (ingressRule .srcPods ,
691+ podInfo {ip : matchingPod .Status .PodIP ,
692+ name : matchingPod .ObjectMeta .Name ,
693+ namespace : matchingPod .ObjectMeta .Namespace ,
694+ labels : matchingPod .ObjectMeta .Labels })
695+ }
673696 }
674697 }
675698 }
699+
676700 newPolicy .ingressRules = append (newPolicy .ingressRules , ingressRule )
677701 }
678702 NetworkPolicies = append (NetworkPolicies , newPolicy )
0 commit comments