@@ -3,7 +3,7 @@ package network_policy
33import (
44 "context"
55 "fmt"
6- "math/rand/v2 "
6+ "math/rand"
77 "net"
88 "strconv"
99 "strings"
@@ -22,7 +22,7 @@ import (
2222)
2323
2424func isMultusInstalled (f * framework.Framework ) bool {
25- _ , err := f .ExtClientSet .ApiextensionsV1 ().CustomResourceDefinitions ().Get (context .TODO (), "network-attachment-definitions.k8s.cni.cncf.io" , metav1.GetOptions {})
25+ _ , err := f .ExtClientSet .ApiextensionsV1 ().CustomResourceDefinitions ().Get (context .Background (), "network-attachment-definitions.k8s.cni.cncf.io" , metav1.GetOptions {})
2626 return err == nil
2727}
2828
@@ -113,7 +113,7 @@ var _ = framework.SerialDescribe("[group:network-policy]", func() {
113113 ginkgo .By ("Creating server pod " + serverPodName )
114114 serverLabels := map [string ]string {"app" : "server" }
115115 annotations := map [string ]string {nadv1 .NetworkAttachmentAnnot : fmt .Sprintf ("%s/%s" , namespaceName , nadName )}
116- port := strconv .Itoa (8000 + rand .IntN (1000 ))
116+ port := strconv .Itoa (8000 + rand .Intn (1000 ))
117117 serverArgs := []string {"netexec" , "--http-port" , port }
118118 serverPod := framework .MakePod (namespaceName , serverPodName , serverLabels , annotations , framework .AgnhostImage , nil , serverArgs )
119119 serverPod = podClient .CreateSync (serverPod )
@@ -256,11 +256,79 @@ var _ = framework.SerialDescribe("[group:network-policy]", func() {
256256 })
257257
258258 framework .ConformanceIt ("should include Service ClusterIP for default VPC provider" , func () {
259- provider := fmt .Sprintf ("%s.%s.%s" , nadName , namespaceName , util .OvnProvider )
259+ ginkgo .By ("Creating server pod " + serverPodName )
260+ serverLabels := map [string ]string {"app" : "server" }
261+ port := 8080
262+ serverArgs := []string {"netexec" , "--http-port" , strconv .Itoa (port )}
263+ serverPod := framework .MakePod (namespaceName , serverPodName , serverLabels , nil , framework .AgnhostImage , nil , serverArgs )
264+ serverPod = podClient .CreateSync (serverPod )
260265
261- ginkgo .By ("Creating VPC " + vpcName )
262- vpc := framework .MakeVpc (vpcName , "" , false , false , nil )
263- _ = vpcClient .CreateSync (vpc )
266+ ginkgo .By ("Creating client pod " + clientPodName )
267+ clientLabels := map [string ]string {"app" : "client" }
268+ clientCmd := []string {"sleep" , "infinity" }
269+ clientPod := framework .MakePod (namespaceName , clientPodName , clientLabels , nil , f .KubeOVNImage , clientCmd , nil )
270+ _ = podClient .CreateSync (clientPod )
271+
272+ ginkgo .By ("Creating service " + serviceName )
273+ ports := []corev1.ServicePort {{Name : "http" , Port : int32 (port ), TargetPort : intstr .FromInt (port )}}
274+ svc := framework .MakeService (serviceName , corev1 .ServiceTypeClusterIP , nil , serverLabels , ports , corev1 .ServiceAffinityNone )
275+ svc = serviceClient .Create (svc )
276+
277+ ginkgo .By ("Creating network policy " + netpolName )
278+ netpol := & netv1.NetworkPolicy {
279+ ObjectMeta : metav1.ObjectMeta {
280+ Name : netpolName ,
281+ Annotations : map [string ]string {
282+ util .NetworkPolicyForAnnotation : "ovn" ,
283+ },
284+ },
285+ Spec : netv1.NetworkPolicySpec {
286+ PodSelector : metav1.LabelSelector {MatchLabels : clientLabels },
287+ PolicyTypes : []netv1.PolicyType {netv1 .PolicyTypeEgress },
288+ Egress : []netv1.NetworkPolicyEgressRule {
289+ {
290+ To : []netv1.NetworkPolicyPeer {
291+ {PodSelector : & metav1.LabelSelector {MatchLabels : serverLabels }},
292+ },
293+ },
294+ },
295+ },
296+ }
297+ _ = netpolClient .Create (netpol )
298+
299+ serverIPs := podIPsByProtocol (serverPod )
300+ if len (serverIPs ) == 0 {
301+ ginkgo .Skip ("no server IPs found" )
302+ }
303+
304+ for protocol , serverIP := range serverIPs {
305+ clusterIP := serviceClusterIPByProtocol (svc , protocol )
306+ asName := policyAddressSetName (netpolName , namespaceName , "egress" , protocol , 0 )
307+
308+ ginkgo .By (fmt .Sprintf ("Checking address set %s for protocol %s" , asName , protocol ))
309+ framework .WaitUntil (2 * time .Second , time .Minute , func (_ context.Context ) (bool , error ) {
310+ addresses , err := getAddressSetAddresses (asName )
311+ if err != nil {
312+ return false , err
313+ }
314+ for _ , addr := range addresses {
315+ if addr == serverIP {
316+ return true , nil
317+ }
318+ }
319+ return false , nil
320+ }, "" )
321+
322+ addresses , err := getAddressSetAddresses (asName )
323+ framework .ExpectNoError (err )
324+ framework .ExpectContainElement (addresses , serverIP )
325+ if clusterIP != "" {
326+ framework .ExpectContainElement (addresses , clusterIP )
327+ }
328+ }
329+ })
330+ framework .ConformanceIt ("should include Service ClusterIP for default VPC provider with multus default network" , func () {
331+ provider := fmt .Sprintf ("%s.%s.%s" , nadName , namespaceName , util .OvnProvider )
264332
265333 ginkgo .By ("Creating network attachment definition " + nadName )
266334 nad := framework .MakeOVNNetworkAttachmentDefinition (nadName , namespaceName , provider , nil )
@@ -270,9 +338,13 @@ var _ = framework.SerialDescribe("[group:network-policy]", func() {
270338 subnet := framework .MakeSubnet (subnetName , "" , cidr , "" , util .DefaultVpc , provider , nil , nil , nil )
271339 _ = subnetClient .CreateSync (subnet )
272340
341+ annotations := map [string ]string {
342+ util .DefaultNetworkAnnotation : fmt .Sprintf ("%s/%s" , namespaceName , nadName ),
343+ fmt .Sprintf (util .LogicalSwitchAnnotationTemplate , provider ): subnetName ,
344+ }
345+
273346 ginkgo .By ("Creating server pod " + serverPodName )
274347 serverLabels := map [string ]string {"app" : "server" }
275- annotations := map [string ]string {nadv1 .NetworkAttachmentAnnot : fmt .Sprintf ("%s/%s" , namespaceName , nadName )}
276348 port := 8080
277349 serverArgs := []string {"netexec" , "--http-port" , strconv .Itoa (port )}
278350 serverPod := framework .MakePod (namespaceName , serverPodName , serverLabels , annotations , framework .AgnhostImage , nil , serverArgs )
@@ -311,12 +383,12 @@ var _ = framework.SerialDescribe("[group:network-policy]", func() {
311383 }
312384 _ = netpolClient .Create (netpol )
313385
314- secondaryIPs := splitIPsByProtocol (serverPod .Annotations [fmt .Sprintf (util .IPAddressAnnotationTemplate , provider )])
315- if len (secondaryIPs ) == 0 {
316- ginkgo .Skip ("no secondary IPs found" )
386+ providerIPs := splitIPsByProtocol (serverPod .Annotations [fmt .Sprintf (util .IPAddressAnnotationTemplate , provider )])
387+ if len (providerIPs ) == 0 {
388+ ginkgo .Skip ("no provider IPs found" )
317389 }
318390
319- for protocol , secondaryIP := range secondaryIPs {
391+ for protocol , providerIP := range providerIPs {
320392 clusterIP := serviceClusterIPByProtocol (svc , protocol )
321393 asName := policyAddressSetName (netpolName , namespaceName , "egress" , protocol , 0 )
322394
@@ -327,7 +399,7 @@ var _ = framework.SerialDescribe("[group:network-policy]", func() {
327399 return false , err
328400 }
329401 for _ , addr := range addresses {
330- if addr == secondaryIP {
402+ if addr == providerIP {
331403 return true , nil
332404 }
333405 }
@@ -336,12 +408,13 @@ var _ = framework.SerialDescribe("[group:network-policy]", func() {
336408
337409 addresses , err := getAddressSetAddresses (asName )
338410 framework .ExpectNoError (err )
339- framework .ExpectContainElement (addresses , secondaryIP )
411+ framework .ExpectContainElement (addresses , providerIP )
340412 if clusterIP != "" {
341413 framework .ExpectContainElement (addresses , clusterIP )
342414 }
343415 }
344416 })
417+
345418})
346419
347420func splitIPsByProtocol (ipStr string ) map [string ]string {
@@ -410,10 +483,14 @@ func getAddressSetAddresses(asName string) ([]string, error) {
410483 if raw == "" {
411484 return nil , nil
412485 }
413- fields := strings .Fields (raw )
486+ raw = strings .Trim (raw , "[]" )
487+ raw = strings .ReplaceAll (raw , "\" " , "" )
488+ fields := strings .FieldsFunc (raw , func (r rune ) bool {
489+ return r == ',' || r == ' ' || r == '\n' || r == '\t'
490+ })
414491 addresses := make ([]string , 0 , len (fields ))
415492 for _ , field := range fields {
416- trimmed := strings .Trim (field , "[]{} \" ," )
493+ trimmed := strings .TrimSpace (field )
417494 if trimmed != "" {
418495 addresses = append (addresses , trimmed )
419496 }
0 commit comments