Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: convert NetworkPolicy empty namespaceSelector to select all s… #488

Merged
merged 1 commit into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/backend/k8s/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ func (c converter) k8sSelectorToCalico(s *metav1.LabelSelector, ns *string) stri
}
}

// If namespace selector is empty then we select all namespaces.
if len(selectors) == 0 && ns == nil {
selectors = []string{"has(calico/k8s_ns)"}
}

return strings.Join(selectors, " && ")
}

Expand Down
42 changes: 42 additions & 0 deletions lib/backend/k8s/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,48 @@ var _ = Describe("Test NetworkPolicy conversion", func() {
Expect(pol.Value.(*model.Policy).OutboundRules[0]).To(Equal(model.Rule{Action: "allow"}))
})

It("should parse a NetworkPolicy with an empty namespaceSelector", func() {
np := extensions.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "testPolicy",
Namespace: "default",
},
Spec: extensions.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
MatchLabels: map[string]string{"label": "value"},
},
Ingress: []extensions.NetworkPolicyIngressRule{
extensions.NetworkPolicyIngressRule{
From: []extensions.NetworkPolicyPeer{
extensions.NetworkPolicyPeer{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{},
},
},
},
},
},
},
}

// Parse the policy.
pol, err := c.networkPolicyToPolicy(&np)
Expect(err).NotTo(HaveOccurred())

// Assert key fields are correct.
Expect(pol.Key.(model.PolicyKey).Name).To(Equal("np.projectcalico.org/default.testPolicy"))

// Assert value fields are correct.
Expect(int(*pol.Value.(*model.Policy).Order)).To(Equal(1000))
Expect(pol.Value.(*model.Policy).Selector).To(Equal("calico/k8s_ns == 'default' && label == 'value'"))
Expect(len(pol.Value.(*model.Policy).InboundRules)).To(Equal(1))
Expect(pol.Value.(*model.Policy).InboundRules[0].SrcSelector).To(Equal("has(calico/k8s_ns)"))

// OutboundRules should only have one rule and it should be allow.
Expect(len(pol.Value.(*model.Policy).OutboundRules)).To(Equal(1))
Expect(pol.Value.(*model.Policy).OutboundRules[0]).To(Equal(model.Rule{Action: "allow"}))
})

It("should parse a NetworkPolicy with podSelector.MatchExpressions", func() {
np := extensions.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Expand Down