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