11package controller
22
33import (
4- "errors"
54 "fmt"
65 "reflect"
76 "slices"
@@ -29,7 +28,6 @@ import (
2928const (
3029 NetworkPolicyEnforcementStandard = "standard"
3130 NetworkPolicyEnforcementLax = "lax"
32- policyForAnnotation = "ovn.kubernetes.io/policy-for"
3331)
3432
3533func (c * Controller ) enqueueAddNp (obj any ) {
@@ -123,7 +121,7 @@ func (c *Controller) handleUpdateNp(key string) error {
123121 }
124122 logRate := parseACLLogRate (np .Annotations )
125123
126- providers , includeSvc , err := parsePolicyFor (np )
124+ providers , err := parsePolicyFor (np )
127125 if err != nil {
128126 return err
129127 }
@@ -219,7 +217,7 @@ func (c *Controller) handleUpdateNp(key string) error {
219217 } else {
220218 var allow , except []string
221219 for _ , npp := range npr .From {
222- if allow , except , err = c .fetchPolicySelectedAddresses (np .Namespace , protocol , npp , providers , includeSvc ); err != nil {
220+ if allow , except , err = c .fetchPolicySelectedAddresses (np .Namespace , protocol , npp , providers ); err != nil {
223221 klog .Errorf ("failed to fetch policy selected addresses, %v" , err )
224222 return err
225223 }
@@ -366,7 +364,7 @@ func (c *Controller) handleUpdateNp(key string) error {
366364 } else {
367365 var allow , except []string
368366 for _ , npp := range npr .To {
369- if allow , except , err = c .fetchPolicySelectedAddresses (np .Namespace , protocol , npp , providers , includeSvc ); err != nil {
367+ if allow , except , err = c .fetchPolicySelectedAddresses (np .Namespace , protocol , npp , providers ); err != nil {
370368 klog .Errorf ("failed to fetch policy selected addresses, %v" , err )
371369 return err
372370 }
@@ -538,50 +536,53 @@ func (c *Controller) handleDeleteNp(key string) error {
538536 return nil
539537}
540538
541- func parsePolicyFor (np * netv1.NetworkPolicy ) (map [string ]struct {}, bool , error ) {
542- raw := strings .TrimSpace (np .Annotations [policyForAnnotation ])
539+ func parsePolicyFor (np * netv1.NetworkPolicy ) (set. Set [string ], error ) {
540+ raw := strings .TrimSpace (np .Annotations [util . NetworkPolicyForAnnotation ])
543541 if raw == "" {
544- return nil , true , nil
542+ return nil , nil
545543 }
546544
547- providers := map [string ]struct {}{}
548- includeSvc := false
545+ providers := set . New [string ]()
546+ invalidMsg := `ignore invalid network_policy_for entry %q, expect "ovn" or "<namespace>/<net-attach-def>"`
549547
550548 for _ , token := range strings .Split (raw , "," ) {
551549 t := strings .TrimSpace (token )
552550 if t == "" {
553551 continue
554552 }
555553
556- switch strings .ToLower (t ) {
557- case "primary" :
558- providers [util .OvnProvider ] = struct {}{}
559- includeSvc = true
554+ if strings .EqualFold (t , "ovn" ) {
555+ providers .Insert (util .OvnProvider )
560556 continue
561- case "default" :
562- return nil , false , fmt .Errorf ("invalid policy-for entry %q (use 'primary')" , t )
563- case "all" :
564- return nil , false , fmt .Errorf ("invalid policy-for entry %q (omit annotation for all)" , t )
565557 }
566558 if strings .Contains (t , "/" ) {
567559 parts := strings .SplitN (t , "/" , 2 )
568560 if len (parts ) != 2 || parts [0 ] == "" || parts [1 ] == "" {
569- return nil , false , fmt .Errorf ("invalid policy-for entry %q" , t )
561+ klog .Warningf (invalidMsg , t )
562+ continue
570563 }
571564 provider := fmt .Sprintf ("%s.%s.%s" , parts [1 ], parts [0 ], util .OvnProvider )
572- providers [ provider ] = struct {}{}
565+ providers . Insert ( provider )
573566 continue
574567 }
575- return nil , false , fmt . Errorf ( "invalid policy-for entry %q" , t )
568+ klog . Warningf ( invalidMsg , t )
576569 }
577570
578571 if len (providers ) == 0 {
579- return nil , false , errors .New ("policy-for annotation has no valid entries" )
572+ klog .Warning ("network_policy_for annotation has no valid entries; policy selects no pods" )
573+ return providers , nil
574+ }
575+ return providers , nil
576+ }
577+
578+ func netpolAppliesToProvider (provider string , providers set.Set [string ]) bool {
579+ if providers == nil {
580+ return true
580581 }
581- return providers , includeSvc , nil
582+ return providers . Has ( provider )
582583}
583584
584- func (c * Controller ) fetchSelectedPorts (namespace string , selector * metav1.LabelSelector , providers map [string ]struct {} ) ([]string , []string , error ) {
585+ func (c * Controller ) fetchSelectedPorts (namespace string , selector * metav1.LabelSelector , providers set. Set [string ]) ([]string , []string , error ) {
585586 var subnets []string
586587 sel , err := metav1 .LabelSelectorAsSelector (selector )
587588 if err != nil {
@@ -603,23 +604,26 @@ func (c *Controller) fetchSelectedPorts(namespace string, selector *metav1.Label
603604 return nil , nil , fmt .Errorf ("failed to get pod networks, %w" , err )
604605 }
605606
607+ matchedProvider := false
606608 for _ , podNet := range podNets {
607609 if ! isOvnSubnet (podNet .Subnet ) {
608610 continue
609611 }
610612 provider := podNet .ProviderName
611- if providers != nil {
612- if _ , ok := providers [provider ]; ! ok {
613- continue
614- }
613+ if ! netpolAppliesToProvider (provider , providers ) {
614+ continue
615615 }
616+ matchedProvider = true
616617
617618 if pod .Annotations [fmt .Sprintf (util .AllocatedAnnotationTemplate , podNet .ProviderName )] == "true" {
618619 ports = append (ports , ovs .PodNameToPortName (podName , pod .Namespace , podNet .ProviderName ))
619620 // Pod selected by networkpolicy has its own subnet which is not the default subnet
620621 subnets = append (subnets , podNet .Subnet .Name )
621622 }
622623 }
624+ if providers != nil && ! matchedProvider {
625+ klog .V (4 ).Infof ("skip pod %s/%s: no network attachment matches network_policy_for" , pod .Namespace , pod .Name )
626+ }
623627 }
624628 subnets = slices .Compact (subnets )
625629 return ports , subnets , nil
@@ -643,7 +647,7 @@ func hasEgressRule(np *netv1.NetworkPolicy) bool {
643647 return np .Spec .Egress != nil
644648}
645649
646- func (c * Controller ) fetchPolicySelectedAddresses (namespace , protocol string , npp netv1.NetworkPolicyPeer , providers map [string ]struct {}, includeSvc bool ) ([]string , []string , error ) {
650+ func (c * Controller ) fetchPolicySelectedAddresses (namespace , protocol string , npp netv1.NetworkPolicyPeer , providers set. Set [string ]) ([]string , []string , error ) {
647651 selectedAddresses := []string {}
648652 exceptAddresses := []string {}
649653
@@ -699,13 +703,13 @@ func (c *Controller) fetchPolicySelectedAddresses(namespace, protocol string, np
699703 klog .Errorf ("failed to get pod nets %v" , err )
700704 return nil , nil , err
701705 }
706+ matchedProvider := false
702707 for _ , podNet := range podNets {
703708 provider := podNet .ProviderName
704- if providers != nil {
705- if _ , ok := providers [provider ]; ! ok {
706- continue
707- }
709+ if ! netpolAppliesToProvider (provider , providers ) {
710+ continue
708711 }
712+ matchedProvider = true
709713
710714 podIPAnnotation := pod .Annotations [fmt .Sprintf (util .IPAddressAnnotationTemplate , podNet .ProviderName )]
711715 podIPs := strings .SplitSeq (podIPAnnotation , "," )
@@ -714,7 +718,10 @@ func (c *Controller) fetchPolicySelectedAddresses(namespace, protocol string, np
714718 selectedAddresses = append (selectedAddresses , podIP )
715719 }
716720 }
717- if ! includeSvc || len (svcs ) == 0 {
721+ if len (svcs ) == 0 {
722+ continue
723+ }
724+ if ! shouldIncludeServiceIPs (podNet ) {
718725 continue
719726 }
720727
@@ -724,11 +731,18 @@ func (c *Controller) fetchPolicySelectedAddresses(namespace, protocol string, np
724731 }
725732 selectedAddresses = append (selectedAddresses , svcIPs ... )
726733 }
734+ if providers != nil && ! matchedProvider {
735+ klog .V (4 ).Infof ("skip pod %s/%s: no network attachment matches network_policy_for" , pod .Namespace , pod .Name )
736+ }
727737 }
728738 }
729739 return selectedAddresses , exceptAddresses , nil
730740}
731741
742+ func shouldIncludeServiceIPs (podNet * kubeovnNet ) bool {
743+ return podNet != nil && podNet .Subnet != nil && podNet .Subnet .Spec .Vpc == util .DefaultVpc
744+ }
745+
732746func svcMatchPods (svcs []* corev1.Service , pod * corev1.Pod , protocol string ) ([]string , error ) {
733747 matchSvcs := []string {}
734748 // find svc ip by pod's info
0 commit comments