@@ -75,7 +75,7 @@ type Datastore interface {
7575 PoolGet (name string ) (* poolutil.EndpointPool , error )
7676 PoolGetMetricsSource (name string ) source.MetricsSource
7777 PoolList () []* poolutil.EndpointPool
78- PoolGetFromLabels (labels map [string ]string ) (* poolutil.EndpointPool , error )
78+ PoolGetFromLabels (namespace string , labels map [string ]string ) (* poolutil.EndpointPool , error )
7979 PoolDelete (name string )
8080
8181 // Clears the store state, happens when the pool gets deleted.
@@ -117,38 +117,37 @@ func (ds *datastore) PoolSet(ctx context.Context, client client.Client, pool *po
117117 return errPoolIsNull
118118 }
119119
120- if ds .registry .Get (pool .Name ) == nil {
121- // Create pod source using the EPP metrics token read by getEPPMetricsToken()
122- // from the mounted token file at /var/run/secrets/epp-metrics/token. This token
123- // is mounted from the epp-metrics service account with minimal privileges and is
124- // used for authenticating with EPP pods when scraping metrics.
125- podConfig := pod.PodScrapingSourceConfig {
126- ServiceName : pool .EndpointPicker .ServiceName ,
127- ServiceNamespace : pool .EndpointPicker .Namespace ,
128- MetricsPort : pool .EndpointPicker .MetricsPortNumber ,
129- BearerToken : getEPPMetricsToken (),
130- }
120+ namespacedName := pool .Namespace + "/" + pool .Name
121+
122+ // Create or update pod source using the EPP metrics token read by getEPPMetricsToken()
123+ // from the mounted token file at /var/run/secrets/epp-metrics/token. This token
124+ // is mounted from the epp-metrics service account with minimal privileges and is
125+ // used for authenticating with EPP pods when scraping metrics.
126+ podConfig := pod.PodScrapingSourceConfig {
127+ ServiceName : pool .EndpointPicker .ServiceName ,
128+ ServiceNamespace : pool .EndpointPicker .Namespace ,
129+ MetricsPort : pool .EndpointPicker .MetricsPortNumber ,
130+ BearerToken : getEPPMetricsToken (),
131+ }
131132
132- podSource , err := pod .NewPodScrapingSource (ctx , client , podConfig )
133- if err != nil {
134- return err
135- }
133+ podSource , err := pod .NewPodScrapingSource (ctx , client , podConfig )
134+ if err != nil {
135+ return err
136+ }
136137
137- // Register in registry
138- // TODO: We need to be able to update or delete a pod source object in the registry at internal/collector/source/registry.go
139- if err := ds .registry .Register (pool .Name , podSource ); err != nil {
140- return err
141- }
138+ // Update or register in registry (idempotent operation)
139+ if err := ds .registry .Update (namespacedName , podSource ); err != nil {
140+ return err
142141 }
143142
144- // Store in the datastore
145- ds .pools .Store (pool .Name , pool )
143+ // Store in the datastore with namespace-scoped key
144+ ds .pools .Store (pool .Namespace + "/" + pool . Name , pool )
146145 return nil
147146}
148147
149- func (ds * datastore ) PoolGet (name string ) (* poolutil.EndpointPool , error ) {
148+ func (ds * datastore ) PoolGet (namespacedName string ) (* poolutil.EndpointPool , error ) {
150149
151- pool , exist := ds .pools .Load (name )
150+ pool , exist := ds .pools .Load (namespacedName )
152151 if ! exist {
153152 return nil , errPoolNotSynced
154153 }
@@ -157,22 +156,27 @@ func (ds *datastore) PoolGet(name string) (*poolutil.EndpointPool, error) {
157156 return epp , nil
158157}
159158
160- func (ds * datastore ) PoolGetMetricsSource (name string ) source.MetricsSource {
161- source := ds .registry .Get (name )
159+ func (ds * datastore ) PoolGetMetricsSource (namespacedName string ) source.MetricsSource {
160+ source := ds .registry .Get (namespacedName )
162161 return source
163162}
164163
165- func (ds * datastore ) PoolGetFromLabels (labels map [string ]string ) (* poolutil.EndpointPool , error ) {
164+ func (ds * datastore ) PoolGetFromLabels (namespace string , labels map [string ]string ) (* poolutil.EndpointPool , error ) {
166165 exist := false
167166 var ep * poolutil.EndpointPool
168167
169168 ds .pools .Range (func (k , v any ) bool {
170169 ep = v .(* poolutil.EndpointPool )
171170
171+ // Filter by namespace first to avoid cross-namespace matches
172+ if ep .Namespace != namespace {
173+ return true // Continue iteration
174+ }
175+
172176 found := poolutil .IsSubset (ep .Selector , labels )
173177 if found {
174178 exist = true
175- return false
179+ return false // Stop iteration - found a match
176180 }
177181 return true
178182 })
@@ -193,8 +197,12 @@ func (ds *datastore) PoolList() []*poolutil.EndpointPool {
193197 return res
194198}
195199
196- func (ds * datastore ) PoolDelete (name string ) {
197- ds .pools .Delete (name )
200+ func (ds * datastore ) PoolDelete (namespacedName string ) {
201+ ds .pools .Delete (namespacedName )
202+ // Clean up the metrics source from the registry
203+ if err := ds .registry .Unregister (namespacedName ); err != nil {
204+ ctrl .Log .V (logging .DEBUG ).Info ("Failed to unregister metrics source" , "namespacedName" , namespacedName , "error" , err )
205+ }
198206}
199207
200208func (ds * datastore ) Clear () {
0 commit comments