@@ -25,7 +25,6 @@ import (
2525 "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/gvk"
2626 "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
2727 "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/service"
28- "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/utils"
2928)
3029
3130const (
@@ -184,12 +183,12 @@ func GetMetadata(pod *corev1.Pod, mc *metadata.Store, logger *zap.Logger) map[ex
184183// collectPodJobProperties checks if pod owner of type Job is cached. Check owners reference
185184// on Job to see if it was created by a CronJob. Sync metadata accordingly.
186185func collectPodJobProperties (pod * corev1.Pod , jobStores map [string ]cache.Store , logger * zap.Logger ) map [string ]string {
187- jobRef := utils . FindOwnerWithKind (pod .OwnerReferences , constants .K8sKindJob )
186+ jobRef := findOwnerWithKind (pod .OwnerReferences , constants .K8sKindJob )
188187 if jobRef != nil {
189188 var job any
190189 var err error
191190
192- job , err = utils . GetObjectFromStore (pod .Namespace , jobRef .Name , jobStores )
191+ job , err = getObjectFromStore (pod .Namespace , jobRef .Name , jobStores )
193192 if err != nil {
194193 logError (err , jobRef , pod .UID , logger )
195194 return nil
@@ -204,7 +203,7 @@ func collectPodJobProperties(pod *corev1.Pod, jobStores map[string]cache.Store,
204203 logError (fmt .Errorf ("cannot cast %T to *batchv1.Job" , job ), jobRef , pod .UID , logger )
205204 return nil
206205 }
207- if cronJobRef := utils . FindOwnerWithKind (jobObj .OwnerReferences , constants .K8sKindCronJob ); cronJobRef != nil {
206+ if cronJobRef := findOwnerWithKind (jobObj .OwnerReferences , constants .K8sKindCronJob ); cronJobRef != nil {
208207 return getWorkloadProperties (cronJobRef , string (conventions .K8SCronJobNameKey ))
209208 }
210209 return getWorkloadProperties (jobRef , string (conventions .K8SJobNameKey ))
@@ -215,12 +214,12 @@ func collectPodJobProperties(pod *corev1.Pod, jobStores map[string]cache.Store,
215214// collectPodReplicaSetProperties checks if pod owner of type ReplicaSet is cached. Check owners reference
216215// on ReplicaSet to see if it was created by a Deployment. Sync metadata accordingly.
217216func collectPodReplicaSetProperties (pod * corev1.Pod , replicaSetStores map [string ]cache.Store , logger * zap.Logger ) map [string ]string {
218- rsRef := utils . FindOwnerWithKind (pod .OwnerReferences , constants .K8sKindReplicaSet )
217+ rsRef := findOwnerWithKind (pod .OwnerReferences , constants .K8sKindReplicaSet )
219218 if rsRef != nil {
220219 var replicaSet any
221220 var err error
222221
223- replicaSet , err = utils . GetObjectFromStore (pod .Namespace , rsRef .Name , replicaSetStores )
222+ replicaSet , err = getObjectFromStore (pod .Namespace , rsRef .Name , replicaSetStores )
224223 if err != nil {
225224 logError (err , rsRef , pod .UID , logger )
226225 return nil
@@ -235,7 +234,7 @@ func collectPodReplicaSetProperties(pod *corev1.Pod, replicaSetStores map[string
235234 logError (fmt .Errorf ("cannot cast %T to *appsv1.ReplicaSet" , replicaSet ), rsRef , pod .UID , logger )
236235 return nil
237236 }
238- if deployRef := utils . FindOwnerWithKind (replicaSetObj .OwnerReferences , constants .K8sKindDeployment ); deployRef != nil {
237+ if deployRef := findOwnerWithKind (replicaSetObj .OwnerReferences , constants .K8sKindDeployment ); deployRef != nil {
239238 return getWorkloadProperties (deployRef , string (conventions .K8SDeploymentNameKey ))
240239 }
241240 return getWorkloadProperties (rsRef , string (conventions .K8SReplicaSetNameKey ))
@@ -280,3 +279,38 @@ func getPodContainerProperties(pod *corev1.Pod, logger *zap.Logger) map[experime
280279 }
281280 return km
282281}
282+
283+ // getIDForCache returns keys to lookup resources from the cache exposed
284+ // by shared informers.
285+ func getIDForCache (namespace , resourceName string ) string {
286+ return fmt .Sprintf ("%s/%s" , namespace , resourceName )
287+ }
288+
289+ // getObjectFromStore retrieves the requested object from the given stores.
290+ // first, the object is attempted to be retrieved from the store for all namespaces,
291+ // and if it is not found there, the namespace-specific store is used
292+ func getObjectFromStore (namespace , objName string , stores map [string ]cache.Store ) (any , error ) {
293+ for _ , storeKey := range [2 ]string {metadata .ClusterWideInformerKey , namespace } {
294+ if store , ok := stores [storeKey ]; ok {
295+ obj , exists , err := store .GetByKey (getIDForCache (namespace , objName ))
296+ if err != nil {
297+ return nil , err
298+ }
299+ if exists {
300+ return obj , nil
301+ }
302+ }
303+ }
304+ return nil , nil
305+ }
306+
307+ // findOwnerWithKind returns the OwnerReference of the matching kind from
308+ // the provided list of owner references.
309+ func findOwnerWithKind (ors []v1.OwnerReference , kind string ) * v1.OwnerReference {
310+ for _ , or := range ors {
311+ if or .Kind == kind {
312+ return & or
313+ }
314+ }
315+ return nil
316+ }
0 commit comments