11package controller
22
33import (
4+ "errors"
45 "fmt"
56 "reflect"
67 "slices"
@@ -28,6 +29,7 @@ import (
2829const (
2930 NetworkPolicyEnforcementStandard = "standard"
3031 NetworkPolicyEnforcementLax = "lax"
32+ policyForAnnotation = "ovn.kubernetes.io/policy-for"
3133)
3234
3335func (c * Controller ) enqueueAddNp (obj any ) {
@@ -121,6 +123,11 @@ func (c *Controller) handleUpdateNp(key string) error {
121123 }
122124 logRate := parseACLLogRate (np .Annotations )
123125
126+ providers , includeSvc , err := parsePolicyFor (np )
127+ if err != nil {
128+ return err
129+ }
130+
124131 npName := np .Name
125132 nameArray := []rune (np .Name )
126133 if ! unicode .IsLetter (nameArray [0 ]) {
@@ -142,7 +149,7 @@ func (c *Controller) handleUpdateNp(key string) error {
142149 }
143150
144151 namedPortMap := c .namedPort .GetNamedPortByNs (np .Namespace )
145- ports , subnetNames , err := c .fetchSelectedPorts (np .Namespace , & np .Spec .PodSelector )
152+ ports , subnetNames , err := c .fetchSelectedPorts (np .Namespace , & np .Spec .PodSelector , providers )
146153 if err != nil {
147154 klog .Errorf ("fetch ports belongs to np %s: %v" , key , err )
148155 return err
@@ -212,7 +219,7 @@ func (c *Controller) handleUpdateNp(key string) error {
212219 } else {
213220 var allow , except []string
214221 for _ , npp := range npr .From {
215- if allow , except , err = c .fetchPolicySelectedAddresses (np .Namespace , protocol , npp ); err != nil {
222+ if allow , except , err = c .fetchPolicySelectedAddresses (np .Namespace , protocol , npp , providers , includeSvc ); err != nil {
216223 klog .Errorf ("failed to fetch policy selected addresses, %v" , err )
217224 return err
218225 }
@@ -359,7 +366,7 @@ func (c *Controller) handleUpdateNp(key string) error {
359366 } else {
360367 var allow , except []string
361368 for _ , npp := range npr .To {
362- if allow , except , err = c .fetchPolicySelectedAddresses (np .Namespace , protocol , npp ); err != nil {
369+ if allow , except , err = c .fetchPolicySelectedAddresses (np .Namespace , protocol , npp , providers , includeSvc ); err != nil {
363370 klog .Errorf ("failed to fetch policy selected addresses, %v" , err )
364371 return err
365372 }
@@ -531,7 +538,54 @@ func (c *Controller) handleDeleteNp(key string) error {
531538 return nil
532539}
533540
534- func (c * Controller ) fetchSelectedPorts (namespace string , selector * metav1.LabelSelector ) ([]string , []string , error ) {
541+ func parsePolicyFor (np * netv1.NetworkPolicy ) (map [string ]struct {}, bool , error ) {
542+ raw := strings .TrimSpace (np .Annotations [policyForAnnotation ])
543+ if raw == "" {
544+ return nil , true , nil
545+ }
546+
547+ tokens := make ([]string , 0 )
548+ for _ , token := range strings .Split (raw , "," ) {
549+ t := strings .TrimSpace (token )
550+ if t == "" {
551+ continue
552+ }
553+ tokens = append (tokens , t )
554+ }
555+
556+ providers := map [string ]struct {}{}
557+ includeSvc := false
558+
559+ for _ , t := range tokens {
560+ switch strings .ToLower (t ) {
561+ case "primary" :
562+ providers [util .OvnProvider ] = struct {}{}
563+ includeSvc = true
564+ continue
565+ case "default" :
566+ return nil , false , fmt .Errorf ("invalid policy-for entry %q (use 'primary')" , t )
567+ case "all" :
568+ return nil , false , fmt .Errorf ("invalid policy-for entry %q (omit annotation for all)" , t )
569+ }
570+ if strings .Contains (t , "/" ) {
571+ parts := strings .SplitN (t , "/" , 2 )
572+ if len (parts ) != 2 || parts [0 ] == "" || parts [1 ] == "" {
573+ return nil , false , fmt .Errorf ("invalid policy-for entry %q" , t )
574+ }
575+ provider := fmt .Sprintf ("%s.%s.%s" , parts [1 ], parts [0 ], util .OvnProvider )
576+ providers [provider ] = struct {}{}
577+ continue
578+ }
579+ return nil , false , fmt .Errorf ("invalid policy-for entry %q" , t )
580+ }
581+
582+ if len (providers ) == 0 {
583+ return nil , false , errors .New ("policy-for annotation has no valid entries" )
584+ }
585+ return providers , includeSvc , nil
586+ }
587+
588+ func (c * Controller ) fetchSelectedPorts (namespace string , selector * metav1.LabelSelector , providers map [string ]struct {}) ([]string , []string , error ) {
535589 var subnets []string
536590 sel , err := metav1 .LabelSelectorAsSelector (selector )
537591 if err != nil {
@@ -557,6 +611,12 @@ func (c *Controller) fetchSelectedPorts(namespace string, selector *metav1.Label
557611 if ! isOvnSubnet (podNet .Subnet ) {
558612 continue
559613 }
614+ provider := podNet .ProviderName
615+ if providers != nil {
616+ if _ , ok := providers [provider ]; ! ok {
617+ continue
618+ }
619+ }
560620
561621 if pod .Annotations [fmt .Sprintf (util .AllocatedAnnotationTemplate , podNet .ProviderName )] == "true" {
562622 ports = append (ports , ovs .PodNameToPortName (podName , pod .Namespace , podNet .ProviderName ))
@@ -587,7 +647,7 @@ func hasEgressRule(np *netv1.NetworkPolicy) bool {
587647 return np .Spec .Egress != nil
588648}
589649
590- func (c * Controller ) fetchPolicySelectedAddresses (namespace , protocol string , npp netv1.NetworkPolicyPeer ) ([]string , []string , error ) {
650+ func (c * Controller ) fetchPolicySelectedAddresses (namespace , protocol string , npp netv1.NetworkPolicyPeer , providers map [ string ] struct {}, includeSvc bool ) ([]string , []string , error ) {
591651 selectedAddresses := []string {}
592652 exceptAddresses := []string {}
593653
@@ -644,14 +704,21 @@ func (c *Controller) fetchPolicySelectedAddresses(namespace, protocol string, np
644704 return nil , nil , err
645705 }
646706 for _ , podNet := range podNets {
707+ provider := podNet .ProviderName
708+ if providers != nil {
709+ if _ , ok := providers [provider ]; ! ok {
710+ continue
711+ }
712+ }
713+
647714 podIPAnnotation := pod .Annotations [fmt .Sprintf (util .IPAddressAnnotationTemplate , podNet .ProviderName )]
648715 podIPs := strings .SplitSeq (podIPAnnotation , "," )
649716 for podIP := range podIPs {
650717 if podIP != "" && util .CheckProtocol (podIP ) == protocol {
651718 selectedAddresses = append (selectedAddresses , podIP )
652719 }
653720 }
654- if len (svcs ) == 0 {
721+ if ! includeSvc || len (svcs ) == 0 {
655722 continue
656723 }
657724
0 commit comments