Skip to content

Commit f3038d5

Browse files
committed
Add e2e test for stacked NetworkPolicies with overlapping CIDR
Add a new e2e test to test multiple stacked NetworkPolicies with Except clauses in IPBlock which overlaps with an allowed CIDR in another NetworkPolicy. This test ensures that the order of the creation of NetworkPolicies should not matter while evaluating a Pods access to another Pod.
1 parent 468af72 commit f3038d5

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

test/e2e/network/network_policy.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,130 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
13701370
})
13711371
})
13721372

1373+
ginkgo.It("should ensure an IP overlapping both IPBlock.CIDR and IPBlock.Except is allowed [Feature:NetworkPolicy]", func() {
1374+
protocolUDP := v1.ProtocolUDP
1375+
1376+
// Getting podServer's status to get podServer's IP, to create the CIDR with except clause
1377+
podServerStatus, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(context.TODO(), podServer.Name, metav1.GetOptions{})
1378+
if err != nil {
1379+
framework.ExpectNoError(err, "Error occurred while getting pod status.")
1380+
}
1381+
1382+
podServerAllowCIDR := fmt.Sprintf("%s/24", podServerStatus.Status.PodIP)
1383+
podServerIP := fmt.Sprintf("%s/32", podServerStatus.Status.PodIP)
1384+
// Exclude podServer's IP with an Except clause
1385+
podServerExceptList := []string{podServerIP}
1386+
1387+
// Create NetworkPolicy which blocks access to podServer with except clause.
1388+
policyAllowCIDRWithExceptServerPod := &networkingv1.NetworkPolicy{
1389+
ObjectMeta: metav1.ObjectMeta{
1390+
Namespace: f.Namespace.Name,
1391+
Name: "deny-client-a-via-except-cidr-egress-rule",
1392+
},
1393+
Spec: networkingv1.NetworkPolicySpec{
1394+
// Apply this policy to the client.
1395+
PodSelector: metav1.LabelSelector{
1396+
MatchLabels: map[string]string{
1397+
"pod-name": "client-a",
1398+
},
1399+
},
1400+
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeEgress},
1401+
// Allow traffic to only one CIDR block except subnet which includes Server.
1402+
Egress: []networkingv1.NetworkPolicyEgressRule{
1403+
{
1404+
Ports: []networkingv1.NetworkPolicyPort{
1405+
// Allow DNS look-ups
1406+
{
1407+
Protocol: &protocolUDP,
1408+
Port: &intstr.IntOrString{Type: intstr.Int, IntVal: 53},
1409+
},
1410+
},
1411+
},
1412+
{
1413+
To: []networkingv1.NetworkPolicyPeer{
1414+
{
1415+
IPBlock: &networkingv1.IPBlock{
1416+
CIDR: podServerAllowCIDR,
1417+
Except: podServerExceptList,
1418+
},
1419+
},
1420+
},
1421+
},
1422+
},
1423+
},
1424+
}
1425+
1426+
policyAllowCIDRWithExceptServerPodObj, err := f.ClientSet.NetworkingV1().NetworkPolicies(f.Namespace.Name).Create(context.TODO(), policyAllowCIDRWithExceptServerPod, metav1.CreateOptions{})
1427+
framework.ExpectNoError(err, "Error occurred while creating policy: policyAllowCIDRWithExceptServerPod.")
1428+
1429+
ginkgo.By("Creating client-a which should not be able to contact the server.", func() {
1430+
testCannotConnect(f, f.Namespace, "client-a", service, 80)
1431+
})
1432+
1433+
// Create NetworkPolicy which allows access to the podServer using podServer's IP in allow CIDR.
1434+
policyAllowCIDRServerPod := &networkingv1.NetworkPolicy{
1435+
ObjectMeta: metav1.ObjectMeta{
1436+
Namespace: f.Namespace.Name,
1437+
Name: "allow-client-a-via-cidr-egress-rule",
1438+
},
1439+
Spec: networkingv1.NetworkPolicySpec{
1440+
// Apply this policy to the client.
1441+
PodSelector: metav1.LabelSelector{
1442+
MatchLabels: map[string]string{
1443+
"pod-name": "client-a",
1444+
},
1445+
},
1446+
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeEgress},
1447+
// Allow traffic to only one CIDR block which includes Server.
1448+
Egress: []networkingv1.NetworkPolicyEgressRule{
1449+
{
1450+
Ports: []networkingv1.NetworkPolicyPort{
1451+
// Allow DNS look-ups
1452+
{
1453+
Protocol: &protocolUDP,
1454+
Port: &intstr.IntOrString{Type: intstr.Int, IntVal: 53},
1455+
},
1456+
},
1457+
},
1458+
{
1459+
To: []networkingv1.NetworkPolicyPeer{
1460+
{
1461+
IPBlock: &networkingv1.IPBlock{
1462+
CIDR: podServerIP,
1463+
},
1464+
},
1465+
},
1466+
},
1467+
},
1468+
},
1469+
}
1470+
1471+
policyAllowCIDRServerPod, err = f.ClientSet.NetworkingV1().NetworkPolicies(f.Namespace.Name).Create(context.TODO(), policyAllowCIDRServerPod, metav1.CreateOptions{})
1472+
framework.ExpectNoError(err, "Error occurred while creating policy: policyAllowCIDRServerPod.")
1473+
defer cleanupNetworkPolicy(f, policyAllowCIDRServerPod)
1474+
1475+
ginkgo.By("Creating client-a which should now be able to contact the server.", func() {
1476+
testCanConnect(f, f.Namespace, "client-a", service, 80)
1477+
})
1478+
1479+
ginkgo.By("Deleting the network policy with except podServer IP which disallows access to podServer.")
1480+
cleanupNetworkPolicy(f, policyAllowCIDRWithExceptServerPodObj)
1481+
1482+
ginkgo.By("Creating client-a which should still be able to contact the server after deleting the network policy with except clause.", func() {
1483+
testCanConnect(f, f.Namespace, "client-a", service, 80)
1484+
})
1485+
1486+
// Recreate the NetworkPolicy which contains the podServer's IP in the except list.
1487+
policyAllowCIDRWithExceptServerPod, err = f.ClientSet.NetworkingV1().NetworkPolicies(f.Namespace.Name).Create(context.TODO(), policyAllowCIDRWithExceptServerPod, metav1.CreateOptions{})
1488+
framework.ExpectNoError(err, "Error occurred while creating policy: policyAllowCIDRWithExceptServerPod.")
1489+
defer cleanupNetworkPolicy(f, policyAllowCIDRWithExceptServerPod)
1490+
1491+
ginkgo.By("Creating client-a which should still be able to contact the server after recreating the network policy with except clause.", func() {
1492+
testCanConnect(f, f.Namespace, "client-a", service, 80)
1493+
})
1494+
1495+
})
1496+
13731497
ginkgo.It("should enforce policies to check ingress and egress policies can be controlled independently based on PodSelector [Feature:NetworkPolicy]", func() {
13741498
var serviceA, serviceB *v1.Service
13751499
var podA, podB *v1.Pod

0 commit comments

Comments
 (0)