11package cni
22
33import (
4+ "context"
45 "encoding/json"
56 "fmt"
67 "strings"
78
89 "github.com/netobserv/flowlogs-pipeline/pkg/api"
910 "github.com/netobserv/flowlogs-pipeline/pkg/config"
11+ log "github.com/sirupsen/logrus"
1012 v1 "k8s.io/api/core/v1"
13+ "k8s.io/apimachinery/pkg/api/errors"
14+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+ "k8s.io/apimachinery/pkg/runtime/schema"
16+ "k8s.io/client-go/dynamic"
1117)
1218
1319const (
@@ -58,7 +64,7 @@ func (m *UDNHandler) BuildKeys(flow config.GenericMap, rule *api.K8sRule) []Seco
5864 return keys
5965}
6066
61- func (m * UDNHandler ) GetPodUniqueKeys (pod * v1.Pod ) ([]string , error ) {
67+ func (m * UDNHandler ) GetPodUniqueKeys (ctx context. Context , dynClient * dynamic. DynamicClient , pod * v1.Pod ) ([]string , error ) {
6268 // Example:
6369 // k8s.ovn.org/pod-networks: '{"default":{"ip_addresses":["10.128.2.20/23"],"mac_address":"0a:58:0a:80:02:14","routes":[{"dest":"10.128.0.0/14","nextHop":"10.128.2.1"},{"dest":"100.64.0.0/16","nextHop":"10.128.2.1"}],"ip_address":"10.128.2.20/23","role":"infrastructure-locked"},"mesh-arena/primary-udn":{"ip_addresses":["10.200.200.12/24"],"mac_address":"0a:58:0a:c8:c8:0c","gateway_ips":["10.200.200.1"],"routes":[{"dest":"172.30.0.0/16","nextHop":"10.200.200.1"},{"dest":"100.65.0.0/16","nextHop":"10.200.200.1"}],"ip_address":"10.200.200.12/24","gateway_ip":"10.200.200.1","tunnel_id":16,"role":"primary"}}'
6470 if statusAnnotationJSON , ok := pod .Annotations [ovnAnnotation ]; ok {
@@ -74,6 +80,9 @@ func (m *UDNHandler) GetPodUniqueKeys(pod *v1.Pod) ([]string, error) {
7480 // IP has a CIDR prefix (bug??)
7581 parts := strings .SplitN (ip , "/" , 2 )
7682 if len (parts ) > 0 {
83+ if dynClient != nil {
84+ label = disambiguateClusterUDN (ctx , dynClient , label )
85+ }
7786 key := UDNKey (label , parts [0 ])
7887 keys = append (keys , key .Key )
7988 }
@@ -86,3 +95,44 @@ func (m *UDNHandler) GetPodUniqueKeys(pod *v1.Pod) ([]string, error) {
8695 // Annotation not present => just ignore, no error
8796 return nil , nil
8897}
98+
99+ func disambiguateClusterUDN (ctx context.Context , dynClient * dynamic.DynamicClient , name string ) string {
100+ // "name" can look like this: "my-namespace/my-udn"; namespace included even for Cluster UDN
101+ parts := strings .SplitN (name , "/" , 2 )
102+ if len (parts ) < 2 {
103+ // no disambiguation
104+ return name
105+ }
106+ ns := parts [0 ]
107+ udnName := parts [1 ]
108+ // Does it exist as a namespaced-udn?
109+ _ , err := dynClient .
110+ Resource (schema.GroupVersionResource {
111+ Group : "k8s.ovn.org" ,
112+ Resource : "userdefinednetworks" ,
113+ Version : "v1" ,
114+ }).
115+ Namespace (ns ).
116+ Get (ctx , udnName , metav1.GetOptions {})
117+ if err == nil {
118+ // found => return as is
119+ return name
120+ } else if ! errors .IsNotFound (err ) {
121+ log .Errorf ("could not fetch UDN %s: %v" , name , err )
122+ }
123+ // Does it exist as a cluster-udn?
124+ _ , err = dynClient .
125+ Resource (schema.GroupVersionResource {
126+ Group : "k8s.ovn.org" ,
127+ Resource : "clusteruserdefinednetworks" ,
128+ Version : "v1" ,
129+ }).
130+ Get (ctx , udnName , metav1.GetOptions {})
131+ if err == nil {
132+ // found => return just the udn name part
133+ return udnName
134+ } else if ! errors .IsNotFound (err ) {
135+ log .Errorf ("could not fetch CUDN %s: %v" , udnName , err )
136+ }
137+ return name
138+ }
0 commit comments