6
6
"fmt"
7
7
"os"
8
8
"path"
9
+
9
10
// "github.com/fatih/color"
10
11
// "github.com/gookit/color"
11
12
// . "github.com/logrusorgru/aurora"
@@ -25,6 +26,7 @@ import (
25
26
"k8s.io/client-go/rest"
26
27
)
27
28
29
+ // InitAndExecute sets up and executes the cobra root command
28
30
func InitAndExecute () {
29
31
rootCmd := setupRootCommand ()
30
32
if err := errors .Wrapf (rootCmd .Execute (), "run df-pv root command" ); err != nil {
@@ -90,6 +92,7 @@ func runRootCommand(flags *flagpole) error {
90
92
return nil
91
93
}
92
94
95
+ // PrintUsingGoPretty prints a slice of output rows
93
96
func PrintUsingGoPretty (sliceOfOutputRowPVC []* OutputRowPVC , disableColor bool ) {
94
97
if disableColor {
95
98
text .DisableColors ()
@@ -132,6 +135,7 @@ func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC, disableColor bool)
132
135
fmt .Printf ("\n %s\n \n " , t .Render ())
133
136
}
134
137
138
+ // GetColorFromPercentageUsed gives a color based on percentage
135
139
func GetColorFromPercentageUsed (percentageUsed float64 ) text.Color {
136
140
if percentageUsed > 75 {
137
141
return text .FgRed
@@ -142,6 +146,7 @@ func GetColorFromPercentageUsed(percentageUsed float64) text.Color {
142
146
}
143
147
}
144
148
149
+ // ConvertQuantityValueToHumanReadableIECString converts value to human readable IEC format
145
150
// https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
146
151
func ConvertQuantityValueToHumanReadableIECString (quantity * resource.Quantity ) string {
147
152
var val = quantity .Value ()
@@ -177,6 +182,7 @@ func ConvertQuantityValueToHumanReadableIECString(quantity *resource.Quantity) s
177
182
}
178
183
}
179
184
185
+ // ConvertQuantityValueToHumanReadableDecimalString converts value to human readable decimal format
180
186
func ConvertQuantityValueToHumanReadableDecimalString (quantity * resource.Quantity ) string {
181
187
var val = quantity .Value ()
182
188
var suffix string
@@ -203,6 +209,7 @@ func ConvertQuantityValueToHumanReadableDecimalString(quantity *resource.Quantit
203
209
}
204
210
}
205
211
212
+ // OutputRowPVC represents the output row
206
213
type OutputRowPVC struct {
207
214
PVName string `json:"pvName"`
208
215
PVCName string `json:"pvcName"`
@@ -220,10 +227,12 @@ type OutputRowPVC struct {
220
227
PercentageIUsed float64 `json:"percentageIUsed"`
221
228
}
222
229
230
+ // ServerResponseStruct represents the response at the node endpoint
223
231
type ServerResponseStruct struct {
224
232
Pods []* Pod `json:"pods"`
225
233
}
226
234
235
+ // Pod represents pod spec in the server response
227
236
type Pod struct {
228
237
/*
229
238
EXAMPLE:
@@ -247,6 +256,7 @@ type Pod struct {
247
256
ListOfVolumes []* Volume `json:"volume"`
248
257
}
249
258
259
+ // Volume represents the volume struct
250
260
/*
251
261
EXAMPLE:
252
262
{
@@ -303,6 +313,7 @@ type Volume struct {
303
313
} `json:"pvcRef"`
304
314
}
305
315
316
+ // GetSliceOfOutputRowPVC gets the output row
306
317
func GetSliceOfOutputRowPVC (flags * flagpole ) ([]* OutputRowPVC , error ) {
307
318
308
319
ctx := context .Background ()
@@ -346,7 +357,7 @@ func GetSliceOfOutputRowPVC(flags *flagpole) ([]*OutputRowPVC, error) {
346
357
if err != nil {
347
358
return nil , err
348
359
}
349
- for nodeName , _ := range nodeNameToPodNames {
360
+ for nodeName := range nodeNameToPodNames {
350
361
sliceOfNodeName = append (sliceOfNodeName , nodeName )
351
362
}
352
363
}
@@ -381,7 +392,7 @@ func GetSliceOfOutputRowPVC(flags *flagpole) ([]*OutputRowPVC, error) {
381
392
return sliceOfOutputRowPVC , mainGroup .Run ()
382
393
}
383
394
384
- // consumer
395
+ // ConsumeOutputRowsConcurrently consumes processed output rows concurrently
385
396
func ConsumeOutputRowsConcurrently (outputRowPVCChan <- chan * OutputRowPVC ) []* OutputRowPVC {
386
397
var sliceOfOutputRowPVC []* OutputRowPVC
387
398
for outputRowPVC := range outputRowPVCChan {
@@ -390,7 +401,7 @@ func ConsumeOutputRowsConcurrently(outputRowPVCChan <-chan *OutputRowPVC) []*Out
390
401
return sliceOfOutputRowPVC
391
402
}
392
403
393
- // producer
404
+ // ProduceOutputRowsConcurrently produces output rows concurrently
394
405
func ProduceOutputRowsConcurrently (ctx context.Context , clientset * kubernetes.Clientset , desiredNamespace string , nodeNames []string , outputRowPVCChan chan <- * OutputRowPVC ) error {
395
406
var producerGroup run.Group
396
407
for _ , nodeName := range nodeNames {
@@ -410,6 +421,7 @@ func ProduceOutputRowsConcurrently(ctx context.Context, clientset *kubernetes.Cl
410
421
return nil
411
422
}
412
423
424
+ // GetOutputRowPVCFromNode gets the output row given a nodeName
413
425
func GetOutputRowPVCFromNode (ctx context.Context , clientset * kubernetes.Clientset , desiredNamespace string , nodeName string , outputRowPVCChan chan <- * OutputRowPVC ) error {
414
426
log .Tracef ("connecting to node: %s" , nodeName )
415
427
request := clientset .CoreV1 ().RESTClient ().Get ().Resource ("nodes" ).Name (nodeName ).SubResource ("proxy" ).Suffix ("stats/summary" )
@@ -459,6 +471,7 @@ func GetOutputRowPVCFromNode(ctx context.Context, clientset *kubernetes.Clientse
459
471
return nil
460
472
}
461
473
474
+ // GetWhichNodesToQueryBasedOnNamespace gets a list of nodes to query for all the pods in a namespace
462
475
func GetWhichNodesToQueryBasedOnNamespace (ctx context.Context , clientset * kubernetes.Clientset , desiredNamespace string ) (map [string ][]string , error ) {
463
476
sliceOfPod , err := ListPodsWithPersistentVolumeClaims (ctx , clientset , desiredNamespace )
464
477
if err != nil {
@@ -474,16 +487,16 @@ func GetWhichNodesToQueryBasedOnNamespace(ctx context.Context, clientset *kubern
474
487
return nodeNameToPodNames , nil
475
488
}
476
489
490
+ // GetOutputRowPVCFromPodAndVolume gets an output row for a given pod, volume and optionally namespace
477
491
func GetOutputRowPVCFromPodAndVolume (ctx context.Context , clientset * kubernetes.Clientset , pod * Pod , vol * Volume , desiredNamespace string ) * OutputRowPVC {
478
492
var outputRowPVC * OutputRowPVC
479
493
480
494
if 0 < len (desiredNamespace ) {
481
495
if vol .PvcRef .PvcNamespace != desiredNamespace {
482
496
return nil
483
- } else {
484
- log .Debugf ("restricting findings to namespace: '%s'" , desiredNamespace )
485
497
}
486
498
}
499
+ log .Debugf ("restricting findings to namespace: '%s'" , desiredNamespace )
487
500
488
501
if 0 < len (vol .PvcRef .PvcName ) {
489
502
namespace := pod .PodRef .Namespace
@@ -513,21 +526,25 @@ func GetOutputRowPVCFromPodAndVolume(ctx context.Context, clientset *kubernetes.
513
526
return outputRowPVC
514
527
}
515
528
529
+ // GetKubeConfigFromGenericCliConfigFlags gets the kubeconfig from all the flags
516
530
func GetKubeConfigFromGenericCliConfigFlags (genericCliConfigFlags * genericclioptions.ConfigFlags ) (* rest.Config , error ) {
517
531
config , err := genericCliConfigFlags .ToRESTConfig ()
518
532
return config , errors .Wrap (err , "failed to read kubeconfig" )
519
533
}
520
534
535
+ // ListNodes returns a list of nodes
521
536
func ListNodes (ctx context.Context , clientset * kubernetes.Clientset ) (* corev1.NodeList , error ) {
522
537
log .Tracef ("getting a list of all nodes" )
523
538
return clientset .CoreV1 ().Nodes ().List (ctx , metav1.ListOptions {})
524
539
}
525
540
541
+ // ListPods returns a list of pods
526
542
func ListPods (ctx context.Context , clientset * kubernetes.Clientset , namespace string ) (* corev1.PodList , error ) {
527
543
log .Tracef ("getting a list of all pods in namespace: %s" , namespace )
528
544
return clientset .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {})
529
545
}
530
546
547
+ // ListPodsWithPersistentVolumeClaims returns a list of pods with PVCs
531
548
// kubectl get pods --all-namespaces -o=json | jq -c \
532
549
// '.items[] | {name: .metadata.name, namespace: .metadata.namespace, claimName:.spec.volumes[] | select( has ("persistentVolumeClaim") ).persistentVolumeClaim.claimName }'
533
550
func ListPodsWithPersistentVolumeClaims (ctx context.Context , clientset * kubernetes.Clientset , namespace string ) ([]corev1.Pod , error ) {
@@ -549,6 +566,7 @@ func ListPodsWithPersistentVolumeClaims(ctx context.Context, clientset *kubernet
549
566
return sliceOfPodsWithPVCs , err
550
567
}
551
568
569
+ // GetPVNameFromPVCName returns the name of persistent volume given a namespace and persistent volume claim name
552
570
func GetPVNameFromPVCName (ctx context.Context , clientset * kubernetes.Clientset , namespace string , pvcName string ) (string , error ) {
553
571
var pvName string
554
572
pvc , err := clientset .CoreV1 ().PersistentVolumeClaims (namespace ).Get (ctx , pvcName , metav1.GetOptions {})
@@ -559,6 +577,7 @@ func GetPVNameFromPVCName(ctx context.Context, clientset *kubernetes.Clientset,
559
577
return pvName , err
560
578
}
561
579
580
+ // KubeConfigPath returns the path to kubeconfig file
562
581
func KubeConfigPath () (string , error ) {
563
582
log .Debugf ("getting kubeconfig path based on user's home dir" )
564
583
home , err := os .UserHomeDir ()
@@ -568,11 +587,13 @@ func KubeConfigPath() (string, error) {
568
587
return path .Join (home , ".kube" , "config" ), nil
569
588
}
570
589
590
+ // ListPVCs returns a list of PVCs for a given namespace
571
591
func ListPVCs (ctx context.Context , clientset * kubernetes.Clientset , namespace string ) (* corev1.PersistentVolumeClaimList , error ) {
572
592
log .Tracef ("getting a list of all PVCs in namespace: %s" , namespace )
573
593
return clientset .CoreV1 ().PersistentVolumeClaims (namespace ).List (ctx , metav1.ListOptions {})
574
594
}
575
595
596
+ // ListPVs returns a list of PVs, scoped to corresponding mapped PVCs based on the namespace
576
597
func ListPVs (ctx context.Context , clientset * kubernetes.Clientset , namespace string ) {
577
598
pvList , _ := clientset .CoreV1 ().PersistentVolumes ().List (ctx , metav1.ListOptions {})
578
599
var pvcNames []string
0 commit comments