@@ -52,11 +52,11 @@ func parseResult(resp *v1alpha1.ApplicationTree, query *application.ResourcesQue
52
52
}
53
53
needPods := false
54
54
queryNodes := make ([]v1alpha1.ResourceNode , 0 )
55
- podParents := make ([] string , 0 )
55
+ podParents := make (map [ string ]v1alpha1. ResourceNode )
56
56
for _ , node := range resp .Nodes {
57
57
if node .Kind == "Pod" {
58
58
for _ , pr := range node .ParentRefs {
59
- podParents = append ( podParents , pr .Name )
59
+ podParents [ pr .Name ] = node
60
60
}
61
61
}
62
62
}
@@ -65,11 +65,8 @@ func parseResult(resp *v1alpha1.ApplicationTree, query *application.ResourcesQue
65
65
queryNodes = append (queryNodes , node )
66
66
}
67
67
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 )
73
70
}
74
71
}
75
72
if node .Kind == "StatefulSet" || node .Kind == "DaemonSet" || node .Kind == "Workflow" {
@@ -84,6 +81,10 @@ func parseResult(resp *v1alpha1.ApplicationTree, query *application.ResourcesQue
84
81
85
82
c .logger .Debugw ("needPods" , "pods" , needPods )
86
83
84
+ for _ , node := range podParents {
85
+ queryNodes = append (queryNodes , node )
86
+ }
87
+
87
88
if needPods {
88
89
for _ , node := range resp .Nodes {
89
90
if node .Kind == "Pod" {
@@ -398,8 +399,10 @@ func getPodInitContainers(resource map[string]interface{}) []*string {
398
399
return containers
399
400
}
400
401
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 ) {
402
403
replicaSets := make (map [string ]map [string ]interface {})
404
+ podToReplicasetMapping := make (map [string ]string )
405
+ var podMetadata []* argoApplication.PodMetadata
403
406
for _ , replicaSet := range replicaSetManifests {
404
407
replicaSets [getResourceName (replicaSet )] = replicaSet
405
408
}
@@ -421,12 +424,13 @@ func buildPodMetadataFromReplicaSet(resp *v1alpha1.ApplicationTree, newReplicaSe
421
424
}
422
425
replicaSet := replicaSets [parentName ]
423
426
containers , intContainers := getReplicaSetContainers (replicaSet )
427
+ podToReplicasetMapping [node .Name ] = parentName
424
428
metadata := argoApplication.PodMetadata {Name : node .Name , UID : node .UID , Containers : containers , InitContainers : intContainers , IsNew : isNew }
425
429
podMetadata = append (podMetadata , & metadata )
426
430
}
427
431
}
428
432
}
429
- return
433
+ return podMetadata , podToReplicasetMapping
430
434
}
431
435
432
436
func getReplicaSetContainers (resource map [string ]interface {}) (containers []* string , intContainers []* string ) {
@@ -511,3 +515,57 @@ func getJobsNewPods(jobManifest map[string]interface{}, podManifests []map[strin
511
515
//TODO - new or old logic
512
516
return
513
517
}
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