Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Fix the source of iptable rules generated for Ingress NetworkPolicy w…
Browse files Browse the repository at this point in the history
…hen both port and IP block specified
  • Loading branch information
kitt1987 committed Jun 28, 2019
1 parent b05c5f6 commit a547d45
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion npc/analyser.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (ns *ns) analysePolicy(policy *networkingv1.NetworkPolicy) (
rules[rule.key] = rule
} else {
withNormalisedProtoAndPort(ingressRule.Ports, func(proto, port string) {
rule := newRuleSpec(policyTypeIngress, &proto, srcSelector, targetSelector, &port)
rule := newRuleSpec(policyTypeIngress, &proto, srcRuleHost, targetSelector, &port)
rules[rule.key] = rule
})
}
Expand Down
76 changes: 76 additions & 0 deletions npc/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,79 @@ func TestEgressPolicyWithIPBlock(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(m.sets[exceptIPSetNameInNonDefault].subSets))
}

// Test case for https://github.com/weaveworks/weave/issues/3653
func TestIngressPolicyWithIPBlockAndPortSpecified(t *testing.T) {
const (
barPodIP = "10.32.0.11"
runBarIPSetName = "weave-bZ~x=yBgzH)Ht()K*Uv3z{M]Y"
)

m := newMockIPSet()
ipt := newMockIPTables()
client := fake.NewSimpleClientset()
controller := New("any", ipt, &m, client)

defaultNamespace := &coreapi.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
}

client.CoreV1().Namespaces().Create(defaultNamespace)

podBar := &coreapi.Pod{
ObjectMeta: metav1.ObjectMeta{
UID: "bar",
Namespace: "default",
Name: "bar",
Labels: map[string]string{"run": "bar"}},
Status: coreapi.PodStatus{PodIP: barPodIP}}
controller.AddPod(podBar)
defer controller.DeletePod(podBar)

portProtocol := coreapi.ProtocolTCP
port := intstr.FromInt(80)
netpolicty := &networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
UID: "ipblock-bar",
Name: "allow-ipblock-to-bar",
Namespace: "default",
},
Spec: networkingv1.NetworkPolicySpec{
PolicyTypes: []networkingv1.PolicyType{
networkingv1.PolicyTypeIngress,
},
PodSelector: metav1.LabelSelector{MatchLabels: podBar.Labels},
Ingress: []networkingv1.NetworkPolicyIngressRule{
{
Ports: []networkingv1.NetworkPolicyPort{
{
Protocol: &portProtocol,
Port: &port,
},
},
From: []networkingv1.NetworkPolicyPeer{
{
IPBlock: &networkingv1.IPBlock{
CIDR: "192.168.48.4/32",
},
},
},
},
},
},
}

err := controller.AddNetworkPolicy(netpolicty)
require.NoError(t, err)
defer controller.DeleteNetworkPolicy(netpolicty)

require.Equal(t, 1, len(m.sets[runBarIPSetName].subSets))
require.True(t, m.entryExists(runBarIPSetName, barPodIP))

require.Equal(t, 1, len(ipt.rules[IngressChain]))
for rule := range ipt.rules[IngressChain] {
require.Contains(t, rule, "-s 192.168.48.4/32 -m set --match-set "+runBarIPSetName+" dst --dport 80")
}
}

0 comments on commit a547d45

Please sign in to comment.