@@ -19,6 +19,7 @@ package netpol
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "net"
22
23
"time"
23
24
24
25
"k8s.io/apimachinery/pkg/util/intstr"
@@ -1022,16 +1023,15 @@ var _ = common.SIGDescribe("Netpol", func() {
1022
1023
ports := []int32 {80 }
1023
1024
k8s = initializeResources (ctx , f , protocols , ports )
1024
1025
nsX , _ , _ := getK8sNamespaces (k8s )
1025
- podList , err := f .ClientSet .CoreV1 ().Pods (nsX ).List (ctx , metav1.ListOptions {LabelSelector : "pod=a" })
1026
- framework .ExpectNoError (err , "Failing to find pod x/a" )
1027
- podA := podList .Items [0 ]
1028
1026
1029
- podServerAllowCIDR := fmt .Sprintf ("%s/4" , podA .Status .PodIP )
1030
-
1031
- podList , err = f .ClientSet .CoreV1 ().Pods (nsX ).List (ctx , metav1.ListOptions {LabelSelector : "pod=b" })
1027
+ podList , err := f .ClientSet .CoreV1 ().Pods (nsX ).List (ctx , metav1.ListOptions {LabelSelector : "pod=b" })
1032
1028
framework .ExpectNoError (err , "Failing to find pod x/b" )
1033
1029
podB := podList .Items [0 ]
1034
1030
1031
+ // Create a rule that allows egress to a large set of IPs around
1032
+ // podB, but not podB itself.
1033
+
1034
+ podServerAllowCIDR := makeLargeCIDRForIP (podB .Status .PodIP )
1035
1035
hostMask := 32
1036
1036
if utilnet .IsIPv6String (podB .Status .PodIP ) {
1037
1037
hostMask = 128
@@ -1056,21 +1056,19 @@ var _ = common.SIGDescribe("Netpol", func() {
1056
1056
ports := []int32 {80 }
1057
1057
k8s = initializeResources (ctx , f , protocols , ports )
1058
1058
nsX , _ , _ := getK8sNamespaces (k8s )
1059
- podList , err := f .ClientSet .CoreV1 ().Pods (nsX ).List (ctx , metav1.ListOptions {LabelSelector : "pod=a" })
1060
- framework .ExpectNoError (err , "Failing to find pod x/a" )
1061
- podA := podList .Items [0 ]
1062
1059
1063
- podList , err = f .ClientSet .CoreV1 ().Pods (nsX ).List (ctx , metav1.ListOptions {LabelSelector : "pod=b" })
1060
+ podList , err : = f .ClientSet .CoreV1 ().Pods (nsX ).List (ctx , metav1.ListOptions {LabelSelector : "pod=b" })
1064
1061
framework .ExpectNoError (err , "Failing to find pod x/b" )
1065
1062
podB := podList .Items [0 ]
1066
1063
1067
- // Exclude podServer's IP with an Except clause
1064
+ // Create a rule that allows egress to a large set of IPs around
1065
+ // podB, but not podB itself.
1066
+
1067
+ podServerAllowCIDR := makeLargeCIDRForIP (podB .Status .PodIP )
1068
1068
hostMask := 32
1069
1069
if utilnet .IsIPv6String (podB .Status .PodIP ) {
1070
1070
hostMask = 128
1071
1071
}
1072
-
1073
- podServerAllowCIDR := fmt .Sprintf ("%s/4" , podA .Status .PodIP )
1074
1072
podServerExceptList := []string {fmt .Sprintf ("%s/%d" , podB .Status .PodIP , hostMask )}
1075
1073
egressRule1 := networkingv1.NetworkPolicyEgressRule {}
1076
1074
egressRule1 .To = append (egressRule1 .To , networkingv1.NetworkPolicyPeer {IPBlock : & networkingv1.IPBlock {CIDR : podServerAllowCIDR , Except : podServerExceptList }})
@@ -1083,8 +1081,8 @@ var _ = common.SIGDescribe("Netpol", func() {
1083
1081
1084
1082
ValidateOrFail (k8s , & TestCase {ToPort : 80 , Protocol : v1 .ProtocolTCP , Reachability : reachability })
1085
1083
1084
+ // Create a second NetworkPolicy which allows access to podB
1086
1085
podBIP := fmt .Sprintf ("%s/%d" , podB .Status .PodIP , hostMask )
1087
- //// Create NetworkPolicy which allows access to the podServer using podServer's IP in allow CIDR.
1088
1086
egressRule3 := networkingv1.NetworkPolicyEgressRule {}
1089
1087
egressRule3 .To = append (egressRule3 .To , networkingv1.NetworkPolicyPeer {IPBlock : & networkingv1.IPBlock {CIDR : podBIP }})
1090
1088
allowPolicy := GenNetworkPolicyWithNameAndPodMatchLabel ("allow-client-a-via-cidr-egress-rule" ,
@@ -1465,3 +1463,14 @@ func initializeResources(ctx context.Context, f *framework.Framework, protocols
1465
1463
framework .ExpectNoError (err , "unable to initialize resources" )
1466
1464
return k8s
1467
1465
}
1466
+
1467
+ // makeLargeCIDRForIP returns a CIDR that matches the given IP and many many many other
1468
+ // IPs. (Specifically, it returns the /4 that contains the IP.)
1469
+ func makeLargeCIDRForIP (ip string ) string {
1470
+ podIP := utilnet .ParseIPSloppy (ip )
1471
+ if ip4 := podIP .To4 (); ip4 != nil {
1472
+ podIP = ip4
1473
+ }
1474
+ cidrBase := podIP .Mask (net .CIDRMask (4 , 8 * len (podIP )))
1475
+ return fmt .Sprintf ("%s/4" , cidrBase .String ())
1476
+ }
0 commit comments