From 60a1c66430aace2ce559db737efd637675aba937 Mon Sep 17 00:00:00 2001 From: Akash Srivastava Date: Wed, 9 Jan 2019 16:14:19 +0530 Subject: [PATCH] Fix one pod to multiple PVC connection Read all the PVC source from a pod spec and add adjacency between the pod and its PVCs. Signed-off-by: Akash Srivastava --- probe/kubernetes/pod.go | 17 ++++++++++------- render/persistentvolume.go | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/probe/kubernetes/pod.go b/probe/kubernetes/pod.go index 0031fd9275..a703f5f6e0 100644 --- a/probe/kubernetes/pod.go +++ b/probe/kubernetes/pod.go @@ -2,6 +2,7 @@ package kubernetes import ( "strconv" + "strings" "github.com/weaveworks/scope/report" @@ -29,6 +30,7 @@ type Pod interface { GetNode(probeID string) report.Node RestartCount() uint ContainerNames() []string + VolumeClaimNames() []string } type pod struct { @@ -75,15 +77,14 @@ func (p *pod) RestartCount() uint { return count } -func (p *pod) VolumeClaimName() string { - var claimName string +func (p *pod) VolumeClaimNames() []string { + var claimNames []string for _, volume := range p.Spec.Volumes { if volume.VolumeSource.PersistentVolumeClaim != nil { - claimName = volume.VolumeSource.PersistentVolumeClaim.ClaimName - break + claimNames = append(claimNames, volume.VolumeSource.PersistentVolumeClaim.ClaimName) } } - return claimName + return claimNames } func (p *pod) GetNode(probeID string) report.Node { @@ -94,8 +95,10 @@ func (p *pod) GetNode(probeID string) report.Node { RestartCount: strconv.FormatUint(uint64(p.RestartCount()), 10), } - if p.VolumeClaimName() != "" { - latests[VolumeClaim] = p.VolumeClaimName() + if len(p.VolumeClaimNames()) > 0 { + // PVC name consist of lower case alphanumeric characters, "-" or "." + // and must start and end with an alphanumeric character. + latests[VolumeClaim] = strings.Join(p.VolumeClaimNames(), report.ScopeDelim) } if p.Pod.Spec.HostNetwork { diff --git a/render/persistentvolume.go b/render/persistentvolume.go index 58b90ff202..1785fdf788 100644 --- a/render/persistentvolume.go +++ b/render/persistentvolume.go @@ -2,6 +2,7 @@ package render import ( "context" + "strings" "github.com/weaveworks/scope/probe/kubernetes" "github.com/weaveworks/scope/report" @@ -53,17 +54,21 @@ type podToVolumesRenderer struct{} func (v podToVolumesRenderer) Render(ctx context.Context, rpt report.Report) Nodes { nodes := make(report.Nodes) for podID, podNode := range rpt.Pod.Nodes { - ClaimName, found := podNode.Latest.Lookup(kubernetes.VolumeClaim) + claimNames, found := podNode.Latest.Lookup(kubernetes.VolumeClaim) if !found { continue } podNamespace, _ := podNode.Latest.Lookup(kubernetes.Namespace) - for _, pvcNode := range rpt.PersistentVolumeClaim.Nodes { - pvcName, _ := pvcNode.Latest.Lookup(kubernetes.Name) - pvcNamespace, _ := pvcNode.Latest.Lookup(kubernetes.Namespace) - if (pvcName == ClaimName) && (podNamespace == pvcNamespace) { - podNode.Adjacency = podNode.Adjacency.Add(pvcNode.ID) - podNode.Children = podNode.Children.Add(pvcNode) + claimNameList := strings.Split(claimNames, report.ScopeDelim) + for _, ClaimName := range claimNameList { + for _, pvcNode := range rpt.PersistentVolumeClaim.Nodes { + pvcName, _ := pvcNode.Latest.Lookup(kubernetes.Name) + pvcNamespace, _ := pvcNode.Latest.Lookup(kubernetes.Namespace) + if (pvcName == ClaimName) && (podNamespace == pvcNamespace) { + podNode.Adjacency = podNode.Adjacency.Add(pvcNode.ID) + podNode.Children = podNode.Children.Add(pvcNode) + break + } } } nodes[podID] = podNode