Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/containernetworking/cni v0.8.1
github.com/containernetworking/plugins v0.8.6
github.com/k8snetworkplumbingwg/multi-networkpolicy v0.0.0-20200903074708-7b3ce95ae804
github.com/k8snetworkplumbingwg/multi-networkpolicy v1.0.1
github.com/k8snetworkplumbingwg/network-attachment-definition-client v0.0.0-20200528071255-22c819bc6e7e
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.27.6
Expand Down
1,805 changes: 15 additions & 1,790 deletions go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions pkg/server/policyrules.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,16 @@ func (ipt *iptableBuffer) renderIngressPorts(_ *Server, podInfo *controllers.Pod
dport := ""
if port.Port != nil {
dport = "--dport " + port.Port.String()
if port.EndPort != nil {
dport = fmt.Sprintf("--dport %s:%d", port.Port.String(), *port.EndPort)
}
}

writeLine(ipt.ingressPorts, "-A", chainName,
"-i", podIntf.InterfaceName,
"-m", proto, "-p", proto, dport,
"-j", "MARK", "--set-xmark", "0x10000/0x10000")

validPorts++
}
}
Expand Down Expand Up @@ -493,6 +497,9 @@ func (ipt *iptableBuffer) renderEgressPorts(_ *Server, podInfo *controllers.PodI
dport := ""
if port.Port != nil {
dport = "--dport " + port.Port.String()
if port.EndPort != nil {
dport = fmt.Sprintf("--dport %s:%d", port.Port.String(), *port.EndPort)
}
}

writeLine(ipt.egressPorts, "-A", chainName,
Expand Down
173 changes: 173 additions & 0 deletions pkg/server/policyrules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,93 @@ COMMIT
Expect(buf.filterRules.String()).To(Equal(finalizedRules))
})

It("ingress rules endport", func() {
port0 := intstr.FromInt(8888)
port1 := intstr.FromInt(9999)
endport := int32(11111)
protoTCP := v1.ProtocolTCP
ingressPolicies1 := &multiv1beta1.MultiNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "ingressPolicies1",
Namespace: "testns1",
},
Spec: multiv1beta1.MultiNetworkPolicySpec{
Ingress: []multiv1beta1.MultiNetworkPolicyIngressRule{
{
Ports: []multiv1beta1.MultiNetworkPolicyPort{
{
Protocol: &protoTCP,
Port: &port0,
},
{
Protocol: &protoTCP,
Port: &port1,
EndPort: &endport,
},
},
},
},
},
}

ipt := fakeiptables.NewFake()
Expect(ipt).NotTo(BeNil())
buf := newIptableBuffer()
Expect(buf).NotTo(BeNil())

// verify buf initialized at init
buf.Init(ipt)
s := NewFakeServer("samplehost")
Expect(s).NotTo(BeNil())

Expect(s.netdefChanges.Update(
nil,
NewNetDef("testns1", "net-attach1", NewCNIConfig("testCNI", "multi")))).To(BeTrue())
Expect(s.netdefChanges.GetPluginType(types.NamespacedName{Namespace: "testns1", Name: "net-attach1"})).To(Equal("multi"))

pod1 := NewFakePodWithNetAnnotation(
"testns1",
"testpod1",
"net-attach1",
NewFakeNetworkStatus("testns1", "net-attach1", "192.168.1.1", "10.1.1.1"),
nil)
AddPod(s, pod1)
podInfo1, err := s.podMap.GetPodInfo(pod1)
Expect(err).NotTo(HaveOccurred())

buf.renderIngress(s, podInfo1, 0, ingressPolicies1, []string{"testns1/net-attach1"})

portRules :=
`-A MULTI-0-INGRESS-0-PORTS -i net1 -m tcp -p tcp --dport 8888 -j MARK --set-xmark 0x10000/0x10000
-A MULTI-0-INGRESS-0-PORTS -i net1 -m tcp -p tcp --dport 9999:11111 -j MARK --set-xmark 0x10000/0x10000
`

Expect(buf.ingressPorts.String()).To(Equal(portRules))

buf.FinalizeRules()
finalizedRules :=
`*filter
:MULTI-INGRESS - [0:0]
:MULTI-INGRESS-COMMON - [0:0]
:MULTI-EGRESS - [0:0]
:MULTI-EGRESS-COMMON - [0:0]
:MULTI-0-INGRESS - [0:0]
:MULTI-0-INGRESS-0-PORTS - [0:0]
:MULTI-0-INGRESS-0-FROM - [0:0]
-A MULTI-INGRESS -m comment --comment "policy:ingressPolicies1 net-attach-def:testns1/net-attach1" -i net1 -j MULTI-0-INGRESS
-A MULTI-INGRESS -m mark --mark 0x30000/0x30000 -j RETURN
-A MULTI-0-INGRESS -j MARK --set-xmark 0x0/0x30000
-A MULTI-0-INGRESS -j MULTI-0-INGRESS-0-PORTS
-A MULTI-0-INGRESS -j MULTI-0-INGRESS-0-FROM
-A MULTI-0-INGRESS -m mark --mark 0x30000/0x30000 -j RETURN
-A MULTI-0-INGRESS-0-PORTS -i net1 -m tcp -p tcp --dport 8888 -j MARK --set-xmark 0x10000/0x10000
-A MULTI-0-INGRESS-0-PORTS -i net1 -m tcp -p tcp --dport 9999:11111 -j MARK --set-xmark 0x10000/0x10000
-A MULTI-0-INGRESS-0-FROM -m comment --comment "no ingress from, skipped" -j MARK --set-xmark 0x20000/0x20000
COMMIT
`
Expect(buf.filterRules.String()).To(Equal(finalizedRules))
})

It("ingress rules podselector/matchlabels", func() {
port := intstr.FromInt(8888)
protoTCP := v1.ProtocolTCP
Expand Down Expand Up @@ -1521,6 +1608,92 @@ COMMIT
Expect(buf.filterRules.String()).To(Equal(finalizedRules))
})

It("egress rules endport", func() {
port0 := intstr.FromInt(8888)
port1 := intstr.FromInt(9999)
endport := int32(11111)
protoTCP := v1.ProtocolTCP
egressPolicies1 := &multiv1beta1.MultiNetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "EgressPolicies1",
Namespace: "testns1",
},
Spec: multiv1beta1.MultiNetworkPolicySpec{
Egress: []multiv1beta1.MultiNetworkPolicyEgressRule{
{
Ports: []multiv1beta1.MultiNetworkPolicyPort{
{
Protocol: &protoTCP,
Port: &port0,
},
{
Protocol: &protoTCP,
Port: &port1,
EndPort: &endport,
},
},
},
},
},
}

ipt := fakeiptables.NewFake()
Expect(ipt).NotTo(BeNil())
buf := newIptableBuffer()
Expect(buf).NotTo(BeNil())

// verify buf initialized at init
buf.Init(ipt)
s := NewFakeServer("samplehost")
Expect(s).NotTo(BeNil())

Expect(s.netdefChanges.Update(
nil,
NewNetDef("testns1", "net-attach1", NewCNIConfig("testCNI", "multi")))).To(BeTrue())
Expect(s.netdefChanges.GetPluginType(types.NamespacedName{Namespace: "testns1", Name: "net-attach1"})).To(Equal("multi"))

pod1 := NewFakePodWithNetAnnotation(
"testns1",
"testpod1",
"net-attach1",
NewFakeNetworkStatus("testns1", "net-attach1", "192.168.1.1", "10.1.1.1"),
nil)
AddPod(s, pod1)
podInfo1, err := s.podMap.GetPodInfo(pod1)
Expect(err).NotTo(HaveOccurred())

buf.renderEgress(s, podInfo1, 0, egressPolicies1, []string{"testns1/net-attach1"})

portRules :=
`-A MULTI-0-EGRESS-0-PORTS -o net1 -m tcp -p tcp --dport 8888 -j MARK --set-xmark 0x10000/0x10000
-A MULTI-0-EGRESS-0-PORTS -o net1 -m tcp -p tcp --dport 9999:11111 -j MARK --set-xmark 0x10000/0x10000
`
Expect(buf.egressPorts.String()).To(Equal(portRules))

buf.FinalizeRules()
finalizedRules :=
`*filter
:MULTI-INGRESS - [0:0]
:MULTI-INGRESS-COMMON - [0:0]
:MULTI-EGRESS - [0:0]
:MULTI-EGRESS-COMMON - [0:0]
:MULTI-0-EGRESS - [0:0]
:MULTI-0-EGRESS-0-PORTS - [0:0]
:MULTI-0-EGRESS-0-TO - [0:0]
-A MULTI-EGRESS -m comment --comment "policy:EgressPolicies1 net-attach-def:testns1/net-attach1" -o net1 -j MULTI-0-EGRESS
-A MULTI-EGRESS -m mark --mark 0x30000/0x30000 -j RETURN
-A MULTI-0-EGRESS -j MARK --set-xmark 0x0/0x30000
-A MULTI-0-EGRESS -j MULTI-0-EGRESS-0-PORTS
-A MULTI-0-EGRESS -j MULTI-0-EGRESS-0-TO
-A MULTI-0-EGRESS -m mark --mark 0x30000/0x30000 -j RETURN
-A MULTI-0-EGRESS-0-PORTS -o net1 -m tcp -p tcp --dport 8888 -j MARK --set-xmark 0x10000/0x10000
-A MULTI-0-EGRESS-0-PORTS -o net1 -m tcp -p tcp --dport 9999:11111 -j MARK --set-xmark 0x10000/0x10000
-A MULTI-0-EGRESS-0-TO -m comment --comment "no egress to, skipped" -j MARK --set-xmark 0x20000/0x20000
COMMIT
`
Expect(buf.filterRules.String()).To(Equal(finalizedRules))
})

It("egress rules podselector/matchlabels", func() {
port := intstr.FromInt(8888)
protoTCP := v1.ProtocolTCP
Expand Down
Loading
Loading