@@ -1306,6 +1306,71 @@ var _ = SIGDescribe("NetworkPolicy [LinuxOnly]", func() {
1306
1306
})
1307
1307
})
1308
1308
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
+
1309
1374
ginkgo .It ("should enforce policies to check ingress and egress policies can be controlled independently based on PodSelector [Feature:NetworkPolicy]" , func () {
1310
1375
var serviceA , serviceB * v1.Service
1311
1376
var podA , podB * v1.Pod
0 commit comments