@@ -20,6 +20,7 @@ import (
2020 "github.com/golang/glog"
2121 "github.com/janeczku/go-ipset/ipset"
2222 "k8s.io/client-go/kubernetes"
23+ api "k8s.io/client-go/pkg/api/v1"
2324 apiv1 "k8s.io/client-go/pkg/api/v1"
2425 apiextensions "k8s.io/client-go/pkg/apis/extensions/v1beta1"
2526 networking "k8s.io/client-go/pkg/apis/networking/v1"
@@ -252,6 +253,12 @@ func (npc *NetworkPolicyController) syncNetworkPolicyChains() (map[string]bool,
252253 return nil , fmt .Errorf ("Failed to run iptables command: %s" , err .Error ())
253254 }
254255
256+ // From network policy spec: "If field 'Ingress' is empty then this NetworkPolicy does not allow any traffic "
257+ // so no whitelist rules to be added to the network policy
258+ if policy .ingressRules == nil {
259+ continue
260+ }
261+
255262 // run through all the ingress rules in the spec and create iptable rules
256263 // in the chain for the network policy
257264 for i , ingressRule := range policy .ingressRules {
@@ -325,13 +332,6 @@ func (npc *NetworkPolicyController) syncNetworkPolicyChains() (map[string]bool,
325332 // case where nether ports nor from details are speified in the ingress rule
326333 // so match on all ports, protocol, source IP's
327334 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-
335335 comment := "rule to ACCEPT traffic from source pods to dest pods selected by policy name: " +
336336 policy .name + " namespace " + policy .namespace
337337 args := []string {"-m" , "comment" , "--comment" , comment ,
@@ -660,7 +660,6 @@ func buildNetworkPoliciesInfo() (*[]networkPolicyInfo, error) {
660660 }
661661 matchingPods , err := watchers .PodWatcher .ListByNamespaceAndLabels (policy .Namespace , policy .Spec .PodSelector .MatchLabels )
662662 newPolicy .destPods = make (map [string ]podInfo )
663- newPolicy .ingressRules = make ([]ingressRule , 0 )
664663 if err == nil {
665664 for _ , matchingPod := range matchingPods {
666665 newPolicy .destPods [matchingPod .Status .PodIP ] = podInfo {ip : matchingPod .Status .PodIP ,
@@ -670,6 +669,12 @@ func buildNetworkPoliciesInfo() (*[]networkPolicyInfo, error) {
670669 }
671670 }
672671
672+ if policy .Spec .Ingress == nil {
673+ newPolicy .ingressRules = nil
674+ } else {
675+ newPolicy .ingressRules = make ([]ingressRule , 0 )
676+ }
677+
673678 for _ , specIngressRule := range policy .Spec .Ingress {
674679 ingressRule := ingressRule {}
675680
@@ -693,8 +698,26 @@ func buildNetworkPoliciesInfo() (*[]networkPolicyInfo, error) {
693698 ingressRule .matchAllSource = true
694699 } else {
695700 ingressRule .matchAllSource = false
701+ var matchingPods []* api.Pod
702+ var err error
696703 for _ , peer := range specIngressRule .From {
697- matchingPods , err := watchers .PodWatcher .ListByNamespaceAndLabels (policy .Namespace , peer .PodSelector .MatchLabels )
704+ // spec must have either of PodSelector or NamespaceSelector
705+ if peer .PodSelector != nil {
706+ matchingPods , err = watchers .PodWatcher .ListByNamespaceAndLabels (policy .Namespace ,
707+ peer .PodSelector .MatchLabels )
708+ } else if peer .NamespaceSelector != nil {
709+ namespaces , err := watchers .NamespaceWatcher .ListByLabels (peer .NamespaceSelector .MatchLabels )
710+ if err != nil {
711+ return nil , errors .New ("Failed to build network policies info due to " + err .Error ())
712+ }
713+ for _ , namespace := range namespaces {
714+ namespacePods , err := watchers .PodWatcher .ListByNamespaceAndLabels (namespace .Name , nil )
715+ if err != nil {
716+ return nil , errors .New ("Failed to build network policies info due to " + err .Error ())
717+ }
718+ matchingPods = append (matchingPods , namespacePods ... )
719+ }
720+ }
698721 if err == nil {
699722 for _ , matchingPod := range matchingPods {
700723 ingressRule .srcPods = append (ingressRule .srcPods ,
0 commit comments