Skip to content

Commit 042ac41

Browse files
committed
[multinetpol] Update controller to handle endPort.
endPort was already supported by the NetworkPolicy, all we need to do is copy the new field for the netpol handler. Signed-off-by: Nadia Pinaeva <[email protected]>
1 parent ae7fdd9 commit 042ac41

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

go-controller/pkg/ovn/base_network_controller_multipolicy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func convertMultiNetPolicyToNetPolicy(mpolicy *mnpapi.MultiNetworkPolicy, allowP
7777
ingress.Ports[j] = knet.NetworkPolicyPort{
7878
Protocol: mport.Protocol,
7979
Port: mport.Port,
80+
EndPort: mport.EndPort,
8081
}
8182
}
8283
ingress.From = make([]knet.NetworkPolicyPeer, len(mingress.From))
@@ -104,6 +105,7 @@ func convertMultiNetPolicyToNetPolicy(mpolicy *mnpapi.MultiNetworkPolicy, allowP
104105
egress.Ports[j] = knet.NetworkPolicyPort{
105106
Protocol: mport.Protocol,
106107
Port: mport.Port,
108+
EndPort: mport.EndPort,
107109
}
108110
}
109111
egress.To = make([]knet.NetworkPolicyPeer, len(megress.To))

test/e2e/multihoming.go

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,45 @@ var _ = Describe("Multi Homing", func() {
11651165
metav1.LabelSelector{
11661166
MatchLabels: map[string]string{"role": "trusted"},
11671167
},
1168-
port,
1168+
multiNetPolicyPort(port),
1169+
),
1170+
),
1171+
ginkgo.Entry(
1172+
"using pod selectors and port range for a pure L2 overlay",
1173+
networkAttachmentConfigParams{
1174+
name: secondaryNetworkName,
1175+
topology: "layer2",
1176+
cidr: secondaryFlatL2NetworkCIDR,
1177+
},
1178+
podConfiguration{
1179+
attachments: []nadapi.NetworkSelectionElement{{Name: secondaryNetworkName}},
1180+
name: allowedClient(clientPodName),
1181+
labels: map[string]string{
1182+
"app": "client",
1183+
"role": "trusted",
1184+
},
1185+
},
1186+
podConfiguration{
1187+
attachments: []nadapi.NetworkSelectionElement{{Name: secondaryNetworkName}},
1188+
name: blockedClient(clientPodName),
1189+
labels: map[string]string{"app": "client"},
1190+
},
1191+
podConfiguration{
1192+
attachments: []nadapi.NetworkSelectionElement{{Name: secondaryNetworkName}},
1193+
name: podName,
1194+
containerCmd: httpServerContainerCmd(port),
1195+
labels: map[string]string{"app": "stuff-doer"},
1196+
},
1197+
multiNetIngressLimitingPolicy(
1198+
secondaryNetworkName,
1199+
metav1.LabelSelector{
1200+
MatchLabels: map[string]string{"app": "stuff-doer"},
1201+
},
1202+
metav1.LabelSelector{
1203+
MatchLabels: map[string]string{"role": "trusted"},
1204+
},
1205+
// build a random range around the port we are actually trying to allow without explicitly setting it
1206+
multiNetPolicyPortRange(port-3, port+5),
11691207
),
11701208
),
11711209
ginkgo.Entry(
@@ -1202,7 +1240,7 @@ var _ = Describe("Multi Homing", func() {
12021240
metav1.LabelSelector{
12031241
MatchLabels: map[string]string{"role": "trusted"},
12041242
},
1205-
port,
1243+
multiNetPolicyPort(port),
12061244
),
12071245
),
12081246
ginkgo.Entry(
@@ -1239,7 +1277,7 @@ var _ = Describe("Multi Homing", func() {
12391277
metav1.LabelSelector{
12401278
MatchLabels: map[string]string{"role": "trusted"},
12411279
},
1242-
port,
1280+
multiNetPolicyPort(port),
12431281
),
12441282
),
12451283
ginkgo.Entry(
@@ -1608,7 +1646,7 @@ var _ = Describe("Multi Homing", func() {
16081646
)
16091647

16101648
ginkgo.DescribeTable(
1611-
"deny all",
1649+
"deny traffic",
16121650
func(netConfigParams networkAttachmentConfigParams, clientPodConfig podConfiguration, serverPodConfig podConfiguration, policy *mnpapi.MultiNetworkPolicy) {
16131651
netConfig := newNetworkAttachmentConfig(netConfigParams)
16141652

@@ -1670,6 +1708,39 @@ var _ = Describe("Multi Homing", func() {
16701708
nil,
16711709
),
16721710
),
1711+
ginkgo.Entry(
1712+
"using pod selectors and wrong port range for a localnet topology",
1713+
networkAttachmentConfigParams{
1714+
name: secondaryNetworkName,
1715+
topology: "localnet",
1716+
cidr: secondaryLocalnetNetworkCIDR,
1717+
},
1718+
podConfiguration{
1719+
attachments: []nadapi.NetworkSelectionElement{{Name: secondaryNetworkName}},
1720+
name: allowedClient(clientPodName),
1721+
labels: map[string]string{
1722+
"app": "client",
1723+
"role": "trusted",
1724+
},
1725+
},
1726+
podConfiguration{
1727+
attachments: []nadapi.NetworkSelectionElement{{Name: secondaryNetworkName}},
1728+
name: podName,
1729+
containerCmd: httpServerContainerCmd(port),
1730+
labels: map[string]string{"app": "stuff-doer"},
1731+
},
1732+
multiNetIngressLimitingPolicy(
1733+
secondaryNetworkName,
1734+
metav1.LabelSelector{
1735+
MatchLabels: map[string]string{"app": "stuff-doer"},
1736+
},
1737+
metav1.LabelSelector{
1738+
MatchLabels: map[string]string{"role": "trusted"},
1739+
},
1740+
// build a port range that doesn't include server port
1741+
multiNetPolicyPortRange(port-10, port-1),
1742+
),
1743+
),
16731744
)
16741745
})
16751746
})

test/e2e/multihoming_utils.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,27 @@ func blockedClient(podName string) string {
289289
return "blocked-" + podName
290290
}
291291

292-
func multiNetIngressLimitingPolicy(policyFor string, appliesFor metav1.LabelSelector, allowForSelector metav1.LabelSelector, allowPorts ...int) *mnpapi.MultiNetworkPolicy {
292+
func multiNetPolicyPort(port int) mnpapi.MultiNetworkPolicyPort {
293+
tcp := v1.ProtocolTCP
294+
p := intstr.FromInt32(int32(port))
295+
return mnpapi.MultiNetworkPolicyPort{
296+
Protocol: &tcp,
297+
Port: &p,
298+
}
299+
}
300+
301+
func multiNetPolicyPortRange(port, endPort int) mnpapi.MultiNetworkPolicyPort {
302+
netpolPort := multiNetPolicyPort(port)
303+
endPort32 := int32(endPort)
304+
netpolPort.EndPort = &endPort32
305+
return netpolPort
306+
}
307+
308+
func multiNetIngressLimitingPolicy(policyFor string, appliesFor metav1.LabelSelector, allowForSelector metav1.LabelSelector, allowPorts ...mnpapi.MultiNetworkPolicyPort) *mnpapi.MultiNetworkPolicy {
293309
var (
294310
portAllowlist []mnpapi.MultiNetworkPolicyPort
295311
)
296-
tcp := v1.ProtocolTCP
297-
for _, port := range allowPorts {
298-
p := intstr.FromInt(port)
299-
portAllowlist = append(portAllowlist, mnpapi.MultiNetworkPolicyPort{
300-
Protocol: &tcp,
301-
Port: &p,
302-
})
303-
}
312+
portAllowlist = append(portAllowlist, allowPorts...)
304313
return &mnpapi.MultiNetworkPolicy{
305314
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
306315
PolicyForAnnotation: policyFor,

0 commit comments

Comments
 (0)