@@ -52,11 +52,11 @@ func parseResult(resp *v1alpha1.ApplicationTree, query *application.ResourcesQue
5252 }
5353 needPods := false
5454 queryNodes := make ([]v1alpha1.ResourceNode , 0 )
55- podParents := make ([] string , 0 )
55+ podParents := make (map [ string ]v1alpha1. ResourceNode )
5656 for _ , node := range resp .Nodes {
5757 if node .Kind == "Pod" {
5858 for _ , pr := range node .ParentRefs {
59- podParents = append ( podParents , pr .Name )
59+ podParents [ pr .Name ] = node
6060 }
6161 }
6262 }
@@ -65,11 +65,8 @@ func parseResult(resp *v1alpha1.ApplicationTree, query *application.ResourcesQue
6565 queryNodes = append (queryNodes , node )
6666 }
6767 if node .Kind == "ReplicaSet" {
68- for _ , pr := range podParents {
69- if pr == node .Name {
70- queryNodes = append (queryNodes , node )
71- break
72- }
68+ if _ , ok := podParents [node .Name ]; ok {
69+ queryNodes = append (queryNodes , node )
7370 }
7471 }
7572 if node .Kind == "StatefulSet" || node .Kind == "DaemonSet" || node .Kind == "Workflow" {
@@ -84,6 +81,10 @@ func parseResult(resp *v1alpha1.ApplicationTree, query *application.ResourcesQue
8481
8582 c .logger .Debugw ("needPods" , "pods" , needPods )
8683
84+ for _ , node := range podParents {
85+ queryNodes = append (queryNodes , node )
86+ }
87+
8788 if needPods {
8889 for _ , node := range resp .Nodes {
8990 if node .Kind == "Pod" {
@@ -398,8 +399,10 @@ func getPodInitContainers(resource map[string]interface{}) []*string {
398399 return containers
399400}
400401
401- func buildPodMetadataFromReplicaSet (resp * v1alpha1.ApplicationTree , newReplicaSets []string , replicaSetManifests []map [string ]interface {}) (podMetadata []* argoApplication.PodMetadata ) {
402+ func buildPodMetadataFromReplicaSet (resp * v1alpha1.ApplicationTree , newReplicaSets []string , replicaSetManifests []map [string ]interface {}) ([]* argoApplication.PodMetadata , map [ string ] string ) {
402403 replicaSets := make (map [string ]map [string ]interface {})
404+ podToReplicasetMapping := make (map [string ]string )
405+ var podMetadata []* argoApplication.PodMetadata
403406 for _ , replicaSet := range replicaSetManifests {
404407 replicaSets [getResourceName (replicaSet )] = replicaSet
405408 }
@@ -421,12 +424,13 @@ func buildPodMetadataFromReplicaSet(resp *v1alpha1.ApplicationTree, newReplicaSe
421424 }
422425 replicaSet := replicaSets [parentName ]
423426 containers , intContainers := getReplicaSetContainers (replicaSet )
427+ podToReplicasetMapping [node .Name ] = parentName
424428 metadata := argoApplication.PodMetadata {Name : node .Name , UID : node .UID , Containers : containers , InitContainers : intContainers , IsNew : isNew }
425429 podMetadata = append (podMetadata , & metadata )
426430 }
427431 }
428432 }
429- return
433+ return podMetadata , podToReplicasetMapping
430434}
431435
432436func getReplicaSetContainers (resource map [string ]interface {}) (containers []* string , intContainers []* string ) {
@@ -511,3 +515,57 @@ func getJobsNewPods(jobManifest map[string]interface{}, podManifests []map[strin
511515 //TODO - new or old logic
512516 return
513517}
518+
519+ func updateMetadataOfDuplicatePods (podsMetadataFromPods []* argoApplication.PodMetadata , duplicatePodToReplicasetMapping map [string ]string , podMetaData []* argoApplication.PodMetadata ) []* argoApplication.PodMetadata {
520+ // Initialize mappings for containers
521+ containersPodMapping := make (map [string ][]* string ) // Mapping from pod name to container names
522+ initContainersPodMapping := make (map [string ][]* string )
523+ // iterate over pod metadata extracted from pods' manifests
524+ for _ , podMetadataFromPod := range podsMetadataFromPods {
525+ // If pod is not a duplicate
526+ if _ , ok := duplicatePodToReplicasetMapping [podMetadataFromPod .Name ]; ! ok {
527+ // if pod is not a duplicate append pod metadata to the final result
528+ podMetaData = append (podMetaData , podMetadataFromPod )
529+ } else {
530+ // update init and sidecar container data into podsMetadataFromPods array's pods obj. if pod is a duplicate found in duplicatePodToReplicasetMapping,
531+ for _ , podMetadataFromReplicaSet := range podMetaData {
532+ if podMetadataFromReplicaSet .Name == podMetadataFromPod .Name {
533+ // Update containers mapping
534+ if podMetadataFromPod .Containers != nil {
535+ containersPodMapping [podMetadataFromPod .Name ] = podMetadataFromPod .Containers
536+ // Update containers mapping for other duplicate pods with the same replicaset
537+ // because we are only fetching manifest for one pod
538+ // and propagate to other pods having same parent
539+ currentPodParentName := duplicatePodToReplicasetMapping [podMetadataFromPod .Name ]
540+ for podName , podParentName := range duplicatePodToReplicasetMapping {
541+ if podParentName == currentPodParentName {
542+ containersPodMapping [podName ] = podMetadataFromPod .Containers
543+ }
544+ }
545+ }
546+ if podMetadataFromPod .InitContainers != nil {
547+ initContainersPodMapping [podMetadataFromPod .Name ] = podMetadataFromPod .InitContainers
548+ currentPodParentName := duplicatePodToReplicasetMapping [podMetadataFromPod .Name ]
549+ for podName , podParentName := range duplicatePodToReplicasetMapping {
550+ if podParentName == currentPodParentName {
551+ initContainersPodMapping [podName ] = podMetadataFromPod .InitContainers
552+ }
553+ }
554+ }
555+ }
556+ }
557+ }
558+ }
559+
560+ // Update pod metadata with containers mapping
561+ for _ , metadata := range podMetaData {
562+ if containers , ok := containersPodMapping [metadata .Name ]; ok {
563+ metadata .Containers = containers
564+ }
565+ if initContainers , ok := initContainersPodMapping [metadata .Name ]; ok {
566+ metadata .InitContainers = initContainers
567+ }
568+ }
569+ // Return updated pod metadata
570+ return podMetaData
571+ }
0 commit comments