Skip to content

Commit 211a1e4

Browse files
fix: containers are missing from app-details page in argocd app (#4973)
* fixed * renaming done * refactoring done * code review comments incorporated - added comments * code review comments incorporated * code review comments incorporated * comments added
1 parent 8e616e2 commit 211a1e4

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

client/argocdServer/application/Application.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,21 @@ func (c ServiceClientImpl) buildPodMetadata(resp *v1alpha1.ApplicationTree, resp
332332
}
333333
}
334334

335-
//podMetaData := make([]*PodMetadata, 0)
336-
duplicateCheck := make(map[string]bool)
335+
//duplicatePodToReplicasetMapping can contain following data {Pod1: RS1, Pod2: RS1, Pod4: RS2, Pod5:RS2}, it contains pod
336+
//to replica set mapping, where key is podName and value is its respective replicaset name, multiple keys(podName) can have
337+
//single value (replicasetName)
338+
duplicatePodToReplicasetMapping := make(map[string]string)
337339
if len(newReplicaSets) > 0 {
338-
results := buildPodMetadataFromReplicaSet(resp, newReplicaSets, replicaSetManifests)
340+
results, duplicateMapping := buildPodMetadataFromReplicaSet(resp, newReplicaSets, replicaSetManifests)
339341
for _, meta := range results {
340-
duplicateCheck[meta.Name] = true
341342
podMetaData = append(podMetaData, meta)
342343
}
344+
duplicatePodToReplicasetMapping = duplicateMapping
343345
}
346+
344347
if newPodNames != nil {
345-
results := buildPodMetadataFromPod(resp, podManifests, newPodNames)
346-
for _, meta := range results {
347-
if _, ok := duplicateCheck[meta.Name]; !ok {
348-
podMetaData = append(podMetaData, meta)
349-
}
350-
}
348+
podsMetadataFromPods := buildPodMetadataFromPod(resp, podManifests, newPodNames)
349+
podMetaData = updateMetadataOfDuplicatePods(podsMetadataFromPods, duplicatePodToReplicasetMapping, podMetaData)
351350
}
352351
return
353352
}

client/argocdServer/application/ApplicationUtil.go

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

432436
func 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

Comments
 (0)