@@ -89,15 +89,17 @@ func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC) {
89
89
// https://github.com/jedib0t/go-pretty/tree/v6.0.4/table
90
90
t := table .NewWriter ()
91
91
92
- t .AppendHeader (table.Row {"PVC" , "Namespace " , "Pod" , "Size" , "Used" , "Available" , "%Used" , "iused" , "ifree" , "%iused" })
92
+ t .AppendHeader (table.Row {"Namespace" , " PVC Name " , "PV Name " , "Pod Name" , "Volume Mount Name " , "Size" , "Used" , "Available" , "%Used" , "iused" , "ifree" , "%iused" })
93
93
hiWhiteColor := text .FgHiWhite
94
94
for _ , pvcRow := range sliceOfOutputRowPVC {
95
95
percentageUsedColor := GetColorFromPercentageUsed (pvcRow .PercentageUsed )
96
96
percentageIUsedColor := GetColorFromPercentageUsed (pvcRow .PercentageIUsed )
97
97
t .AppendRow ([]interface {}{
98
- hiWhiteColor .Sprintf ("%s" , pvcRow .PVCName ),
99
98
hiWhiteColor .Sprintf ("%s" , pvcRow .Namespace ),
99
+ hiWhiteColor .Sprintf ("%s" , pvcRow .PVCName ),
100
+ hiWhiteColor .Sprintf ("%s" , pvcRow .PVName ),
100
101
hiWhiteColor .Sprintf ("%s" , pvcRow .PodName ),
102
+ hiWhiteColor .Sprintf ("%s" , pvcRow .VolumeMountName ),
101
103
percentageUsedColor .Sprintf ("%s" , ConvertQuantityValueToHumanReadableIECString (pvcRow .CapacityBytes )),
102
104
percentageUsedColor .Sprintf ("%s" , ConvertQuantityValueToHumanReadableIECString (pvcRow .UsedBytes )),
103
105
percentageUsedColor .Sprintf ("%s" , ConvertQuantityValueToHumanReadableIECString (pvcRow .AvailableBytes )),
@@ -321,22 +323,19 @@ func ConvertQuantityValueToHumanReadableDecimalString(quantity *resource.Quantit
321
323
// }
322
324
323
325
type OutputRowPVC struct {
324
- PodName string `json:"podName"`
325
- Namespace string `json:"namespace"`
326
-
327
- PVCName string `json:"pvcName"`
328
-
329
- AvailableBytes * resource.Quantity `json:"availableBytes"`
330
- CapacityBytes * resource.Quantity `json:"capacityBytes"`
331
- UsedBytes * resource.Quantity `json:"usedBytes"`
332
- PercentageUsed float64
333
-
326
+ Namespace string `json:"namespace"`
327
+ PVCName string `json:"pvcName"`
328
+ PVName string `json:"pvName"`
329
+ PodName string `json:"podName"`
330
+ VolumeMountName string `json:"volumeMountName"`
331
+ AvailableBytes * resource.Quantity `json:"availableBytes"`
332
+ CapacityBytes * resource.Quantity `json:"capacityBytes"`
333
+ UsedBytes * resource.Quantity `json:"usedBytes"`
334
+ PercentageUsed float64
334
335
InodesFree int64 `json:"inodesFree"`
335
336
Inodes int64 `json:"inodes"`
336
337
InodesUsed int64 `json:"inodesUsed"`
337
338
PercentageIUsed float64
338
-
339
- VolumeMountName string `json:"volumeMountName"`
340
339
}
341
340
342
341
type ServerResponseStruct struct {
@@ -506,13 +505,17 @@ func GetOutputRowPVCFromNodeChan(ctx context.Context, clientset *kubernetes.Clie
506
505
507
506
// for trace logging only
508
507
var nodeRespBody interface {}
509
- _ = json .Unmarshal (responseRawArrayOfBytes , & nodeRespBody )
508
+ err = json .Unmarshal (responseRawArrayOfBytes , & nodeRespBody )
509
+ if err != nil {
510
+ return errors .Wrapf (err , "unable to unmarshal json into an interface (this really shouldn't happen)" )
511
+ }
510
512
// log.Tracef("response from node: %+v\n", nodeRespBody)
511
- jsonText , err := json .MarshalIndent (nodeRespBody , "" , " " )
513
+ jsonText , err := json .Marshal (nodeRespBody )
514
+ // jsonText, err := json.MarshalIndent(nodeRespBody, "", " ")
512
515
if err != nil {
513
516
return errors .Wrapf (err , "unable to marshal json (this really shouldn't happen)" )
514
517
}
515
- log .Tracef ("response from node: %s\n " , jsonText )
518
+ log .Tracef ("response from node: %s" , jsonText )
516
519
517
520
var jsonConvertedIntoStruct ServerResponseStruct
518
521
err = json .Unmarshal (responseRawArrayOfBytes , & jsonConvertedIntoStruct )
@@ -522,7 +525,7 @@ func GetOutputRowPVCFromNodeChan(ctx context.Context, clientset *kubernetes.Clie
522
525
523
526
for _ , pod := range jsonConvertedIntoStruct .Pods {
524
527
for _ , vol := range pod .ListOfVolumes {
525
- outputRowPVC := GetOutputRowPVCFromPodAndVolume (pod , vol , desiredNamespace )
528
+ outputRowPVC := GetOutputRowPVCFromPodAndVolume (ctx , clientset , pod , vol , desiredNamespace )
526
529
if nil == outputRowPVC {
527
530
log .Tracef ("no pvc found for pod: '%s', vol: '%s', desiredNamespace: '%s'; continuing..." , pod .PodRef .Name , vol .PvcRef .PvcName , desiredNamespace )
528
531
continue
@@ -539,7 +542,7 @@ func GetOutputRowPVCFromNodeChan(ctx context.Context, clientset *kubernetes.Clie
539
542
return nil
540
543
}
541
544
542
- func GetOutputRowPVCFromPodAndVolume (pod * Pod , vol * Volume , desiredNamespace string ) * OutputRowPVC {
545
+ func GetOutputRowPVCFromPodAndVolume (ctx context. Context , clientset * kubernetes. Clientset , pod * Pod , vol * Volume , desiredNamespace string ) * OutputRowPVC {
543
546
var outputRowPVC * OutputRowPVC
544
547
545
548
if 0 < len (desiredNamespace ) {
@@ -551,22 +554,28 @@ func GetOutputRowPVCFromPodAndVolume(pod *Pod, vol *Volume, desiredNamespace str
551
554
}
552
555
553
556
if 0 < len (vol .PvcRef .PvcName ) {
554
- outputRowPVC = & OutputRowPVC {
555
- PodName : pod .PodRef .Name ,
556
- Namespace : pod .PodRef .Namespace ,
557
-
558
- PVCName : vol .PvcRef .PvcName ,
559
- AvailableBytes : resource .NewQuantity (vol .AvailableBytes , resource .BinarySI ),
560
- CapacityBytes : resource .NewQuantity (vol .CapacityBytes , resource .BinarySI ),
561
- UsedBytes : resource .NewQuantity (vol .UsedBytes , resource .BinarySI ),
562
- PercentageUsed : (float64 (vol .UsedBytes ) / float64 (vol .CapacityBytes )) * 100.0 ,
557
+ namespace := pod .PodRef .Namespace
558
+ pvcName := vol .PvcRef .PvcName
559
+ pvName , _ := GetPVNameFromPVCName (ctx , clientset , namespace , pvcName )
560
+ // if err != nil {
561
+ // ctx.Err()
562
+ // return errors.Wrapf(err, "unable to get PV name from the PVC name")
563
+ // }
563
564
565
+ outputRowPVC = & OutputRowPVC {
566
+ Namespace : namespace ,
567
+ PVCName : pvcName ,
568
+ PVName : pvName ,
569
+ PodName : pod .PodRef .Name ,
570
+ VolumeMountName : vol .Name ,
571
+ AvailableBytes : resource .NewQuantity (vol .AvailableBytes , resource .BinarySI ),
572
+ CapacityBytes : resource .NewQuantity (vol .CapacityBytes , resource .BinarySI ),
573
+ UsedBytes : resource .NewQuantity (vol .UsedBytes , resource .BinarySI ),
574
+ PercentageUsed : (float64 (vol .UsedBytes ) / float64 (vol .CapacityBytes )) * 100.0 ,
564
575
Inodes : vol .Inodes ,
565
576
InodesFree : vol .InodesFree ,
566
577
InodesUsed : vol .InodesUsed ,
567
578
PercentageIUsed : (float64 (vol .InodesUsed ) / float64 (vol .Inodes )) * 100.0 ,
568
-
569
- VolumeMountName : vol .Name ,
570
579
}
571
580
}
572
581
return outputRowPVC
@@ -582,6 +591,16 @@ func ListNodes(ctx context.Context, clientset *kubernetes.Clientset) (*corev1.No
582
591
return clientset .CoreV1 ().Nodes ().List (ctx , metav1.ListOptions {})
583
592
}
584
593
594
+ func GetPVNameFromPVCName (ctx context.Context , clientset * kubernetes.Clientset , namespace string , pvcName string ) (string , error ) {
595
+ var pvName string
596
+ pvc , err := clientset .CoreV1 ().PersistentVolumeClaims (namespace ).Get (ctx , pvcName , metav1.GetOptions {})
597
+ if err != nil {
598
+ return pvName , err
599
+ }
600
+ pvName = pvc .Spec .VolumeName
601
+ return pvName , err
602
+ }
603
+
585
604
func KubeConfigPath () (string , error ) {
586
605
log .Debugf ("getting kubeconfig path based on user's home dir" )
587
606
home , err := os .UserHomeDir ()
0 commit comments