Skip to content

Commit 547b17d

Browse files
authored
Merge pull request #131 from cloudnativelabs/112-ntwk-plcy
NetworkPolicy: Fix e2e test failures
2 parents 8bf6281 + 1af19cb commit 547b17d

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

app/controllers/network_policy_controller.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

app/watchers/namespace_watcher.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"k8s.io/client-go/kubernetes"
1111
api "k8s.io/client-go/pkg/api/v1"
1212
cache "k8s.io/client-go/tools/cache"
13+
listers "k8s.io/client-go/listers/core/v1"
14+
"k8s.io/apimachinery/pkg/labels"
1315
)
1416

1517
type NamespaceUpdate struct {
@@ -68,6 +70,16 @@ func (nsw *namespaceWatcher) List() []*api.Namespace {
6870
return namespace_instances
6971
}
7072

73+
func (nsw *namespaceWatcher) ListByLabels(set labels.Set) ([]*api.Namespace, error) {
74+
namespaceLister := listers.NewNamespaceLister(nsw.namespaceLister)
75+
matchedNamespaces, err := namespaceLister.List(set.AsSelector())
76+
if err != nil {
77+
return nil, err
78+
} else {
79+
return matchedNamespaces, nil
80+
}
81+
}
82+
7183
func (nsw *namespaceWatcher) RegisterHandler(handler NamespaceUpdatesHandler) {
7284
nsw.broadcaster.Add(utils.ListenerFunc(func(instance interface{}) {
7385
handler.OnNamespaceUpdate(instance.(*NamespaceUpdate))

0 commit comments

Comments
 (0)