Skip to content

Commit a980a1f

Browse files
committed
Add e2e test to test Except clause in NetworkPolicy
Add a new e2e test to test the Except clauses in IPBlock CIDR based NetworkPolicies. This test adds an egress rule which allows client to connect to a CIDR which includes the ServerPod's IP, however carves an except subnet which excludes this ServerPod.
1 parent 72b04ef commit a980a1f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

test/e2e/network/network_policy.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,71 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
13061306
})
13071307
})
13081308

1309+
ginkgo.It("should enforce except clause while egress access to server in CIDR block [Feature:NetworkPolicy]", func() {
1310+
protocolUDP := v1.ProtocolUDP
1311+
1312+
// Getting podServer's status to get podServer's IP, to create the CIDR with except clause
1313+
podServerStatus, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(context.TODO(), podServer.Name, metav1.GetOptions{})
1314+
if err != nil {
1315+
framework.ExpectNoError(err, "Error occurred while getting pod status.")
1316+
}
1317+
1318+
podServerAllowCIDR := fmt.Sprintf("%s/24", podServerStatus.Status.PodIP)
1319+
// Exclude podServer's IP with an Except clause
1320+
podServerExceptList := []string{fmt.Sprintf("%s/32", podServerStatus.Status.PodIP)}
1321+
1322+
// client-a can connect to server prior to applying the NetworkPolicy
1323+
ginkgo.By("Creating client-a which should be able to contact the server.", func() {
1324+
testCanConnect(f, f.Namespace, "client-a", service, 80)
1325+
})
1326+
1327+
policyAllowCIDRWithExcept := &networkingv1.NetworkPolicy{
1328+
ObjectMeta: metav1.ObjectMeta{
1329+
Namespace: f.Namespace.Name,
1330+
Name: "deny-client-a-via-except-cidr-egress-rule",
1331+
},
1332+
Spec: networkingv1.NetworkPolicySpec{
1333+
// Apply this policy to the client.
1334+
PodSelector: metav1.LabelSelector{
1335+
MatchLabels: map[string]string{
1336+
"pod-name": "client-a",
1337+
},
1338+
},
1339+
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeEgress},
1340+
// Allow traffic to only one CIDR block except subnet which includes Server.
1341+
Egress: []networkingv1.NetworkPolicyEgressRule{
1342+
{
1343+
Ports: []networkingv1.NetworkPolicyPort{
1344+
// Allow DNS look-ups
1345+
{
1346+
Protocol: &protocolUDP,
1347+
Port: &intstr.IntOrString{Type: intstr.Int, IntVal: 53},
1348+
},
1349+
},
1350+
},
1351+
{
1352+
To: []networkingv1.NetworkPolicyPeer{
1353+
{
1354+
IPBlock: &networkingv1.IPBlock{
1355+
CIDR: podServerAllowCIDR,
1356+
Except: podServerExceptList,
1357+
},
1358+
},
1359+
},
1360+
},
1361+
},
1362+
},
1363+
}
1364+
1365+
policyAllowCIDRWithExcept, err = f.ClientSet.NetworkingV1().NetworkPolicies(f.Namespace.Name).Create(context.TODO(), policyAllowCIDRWithExcept, metav1.CreateOptions{})
1366+
framework.ExpectNoError(err, "Error occurred while creating policy: policyAllowCIDRWithExcept.")
1367+
defer cleanupNetworkPolicy(f, policyAllowCIDRWithExcept)
1368+
1369+
ginkgo.By("Creating client-a which should no longer be able to contact the server.", func() {
1370+
testCannotConnect(f, f.Namespace, "client-a", service, 80)
1371+
})
1372+
})
1373+
13091374
ginkgo.It("should enforce policies to check ingress and egress policies can be controlled independently based on PodSelector [Feature:NetworkPolicy]", func() {
13101375
var serviceA, serviceB *v1.Service
13111376
var podA, podB *v1.Pod

0 commit comments

Comments
 (0)