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