Skip to content

Commit 54d2ecd

Browse files
committed
add comments
1 parent 7b2791c commit 54d2ecd

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

pkg/df-pv/root.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path"
9+
910
// "github.com/fatih/color"
1011
// "github.com/gookit/color"
1112
// . "github.com/logrusorgru/aurora"
@@ -25,6 +26,7 @@ import (
2526
"k8s.io/client-go/rest"
2627
)
2728

29+
// InitAndExecute sets up and executes the cobra root command
2830
func InitAndExecute() {
2931
rootCmd := setupRootCommand()
3032
if err := errors.Wrapf(rootCmd.Execute(), "run df-pv root command"); err != nil {
@@ -90,6 +92,7 @@ func runRootCommand(flags *flagpole) error {
9092
return nil
9193
}
9294

95+
// PrintUsingGoPretty prints a slice of output rows
9396
func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC, disableColor bool) {
9497
if disableColor {
9598
text.DisableColors()
@@ -132,6 +135,7 @@ func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC, disableColor bool)
132135
fmt.Printf("\n%s\n\n", t.Render())
133136
}
134137

138+
// GetColorFromPercentageUsed gives a color based on percentage
135139
func GetColorFromPercentageUsed(percentageUsed float64) text.Color {
136140
if percentageUsed > 75 {
137141
return text.FgRed
@@ -142,6 +146,7 @@ func GetColorFromPercentageUsed(percentageUsed float64) text.Color {
142146
}
143147
}
144148

149+
// ConvertQuantityValueToHumanReadableIECString converts value to human readable IEC format
145150
// https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
146151
func ConvertQuantityValueToHumanReadableIECString(quantity *resource.Quantity) string {
147152
var val = quantity.Value()
@@ -177,6 +182,7 @@ func ConvertQuantityValueToHumanReadableIECString(quantity *resource.Quantity) s
177182
}
178183
}
179184

185+
// ConvertQuantityValueToHumanReadableDecimalString converts value to human readable decimal format
180186
func ConvertQuantityValueToHumanReadableDecimalString(quantity *resource.Quantity) string {
181187
var val = quantity.Value()
182188
var suffix string
@@ -203,6 +209,7 @@ func ConvertQuantityValueToHumanReadableDecimalString(quantity *resource.Quantit
203209
}
204210
}
205211

212+
// OutputRowPVC represents the output row
206213
type OutputRowPVC struct {
207214
PVName string `json:"pvName"`
208215
PVCName string `json:"pvcName"`
@@ -220,10 +227,12 @@ type OutputRowPVC struct {
220227
PercentageIUsed float64 `json:"percentageIUsed"`
221228
}
222229

230+
// ServerResponseStruct represents the response at the node endpoint
223231
type ServerResponseStruct struct {
224232
Pods []*Pod `json:"pods"`
225233
}
226234

235+
// Pod represents pod spec in the server response
227236
type Pod struct {
228237
/*
229238
EXAMPLE:
@@ -247,6 +256,7 @@ type Pod struct {
247256
ListOfVolumes []*Volume `json:"volume"`
248257
}
249258

259+
// Volume represents the volume struct
250260
/*
251261
EXAMPLE:
252262
{
@@ -303,6 +313,7 @@ type Volume struct {
303313
} `json:"pvcRef"`
304314
}
305315

316+
// GetSliceOfOutputRowPVC gets the output row
306317
func GetSliceOfOutputRowPVC(flags *flagpole) ([]*OutputRowPVC, error) {
307318

308319
ctx := context.Background()
@@ -346,7 +357,7 @@ func GetSliceOfOutputRowPVC(flags *flagpole) ([]*OutputRowPVC, error) {
346357
if err != nil {
347358
return nil, err
348359
}
349-
for nodeName, _ := range nodeNameToPodNames {
360+
for nodeName := range nodeNameToPodNames {
350361
sliceOfNodeName = append(sliceOfNodeName, nodeName)
351362
}
352363
}
@@ -381,7 +392,7 @@ func GetSliceOfOutputRowPVC(flags *flagpole) ([]*OutputRowPVC, error) {
381392
return sliceOfOutputRowPVC, mainGroup.Run()
382393
}
383394

384-
// consumer
395+
// ConsumeOutputRowsConcurrently consumes processed output rows concurrently
385396
func ConsumeOutputRowsConcurrently(outputRowPVCChan <-chan *OutputRowPVC) []*OutputRowPVC {
386397
var sliceOfOutputRowPVC []*OutputRowPVC
387398
for outputRowPVC := range outputRowPVCChan {
@@ -390,7 +401,7 @@ func ConsumeOutputRowsConcurrently(outputRowPVCChan <-chan *OutputRowPVC) []*Out
390401
return sliceOfOutputRowPVC
391402
}
392403

393-
// producer
404+
// ProduceOutputRowsConcurrently produces output rows concurrently
394405
func ProduceOutputRowsConcurrently(ctx context.Context, clientset *kubernetes.Clientset, desiredNamespace string, nodeNames []string, outputRowPVCChan chan<- *OutputRowPVC) error {
395406
var producerGroup run.Group
396407
for _, nodeName := range nodeNames {
@@ -410,6 +421,7 @@ func ProduceOutputRowsConcurrently(ctx context.Context, clientset *kubernetes.Cl
410421
return nil
411422
}
412423

424+
// GetOutputRowPVCFromNode gets the output row given a nodeName
413425
func GetOutputRowPVCFromNode(ctx context.Context, clientset *kubernetes.Clientset, desiredNamespace string, nodeName string, outputRowPVCChan chan<- *OutputRowPVC) error {
414426
log.Tracef("connecting to node: %s", nodeName)
415427
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
459471
return nil
460472
}
461473

474+
// GetWhichNodesToQueryBasedOnNamespace gets a list of nodes to query for all the pods in a namespace
462475
func GetWhichNodesToQueryBasedOnNamespace(ctx context.Context, clientset *kubernetes.Clientset, desiredNamespace string) (map[string][]string, error) {
463476
sliceOfPod, err := ListPodsWithPersistentVolumeClaims(ctx, clientset, desiredNamespace)
464477
if err != nil {
@@ -474,16 +487,16 @@ func GetWhichNodesToQueryBasedOnNamespace(ctx context.Context, clientset *kubern
474487
return nodeNameToPodNames, nil
475488
}
476489

490+
// GetOutputRowPVCFromPodAndVolume gets an output row for a given pod, volume and optionally namespace
477491
func GetOutputRowPVCFromPodAndVolume(ctx context.Context, clientset *kubernetes.Clientset, pod *Pod, vol *Volume, desiredNamespace string) *OutputRowPVC {
478492
var outputRowPVC *OutputRowPVC
479493

480494
if 0 < len(desiredNamespace) {
481495
if vol.PvcRef.PvcNamespace != desiredNamespace {
482496
return nil
483-
} else {
484-
log.Debugf("restricting findings to namespace: '%s'", desiredNamespace)
485497
}
486498
}
499+
log.Debugf("restricting findings to namespace: '%s'", desiredNamespace)
487500

488501
if 0 < len(vol.PvcRef.PvcName) {
489502
namespace := pod.PodRef.Namespace
@@ -513,21 +526,25 @@ func GetOutputRowPVCFromPodAndVolume(ctx context.Context, clientset *kubernetes.
513526
return outputRowPVC
514527
}
515528

529+
// GetKubeConfigFromGenericCliConfigFlags gets the kubeconfig from all the flags
516530
func GetKubeConfigFromGenericCliConfigFlags(genericCliConfigFlags *genericclioptions.ConfigFlags) (*rest.Config, error) {
517531
config, err := genericCliConfigFlags.ToRESTConfig()
518532
return config, errors.Wrap(err, "failed to read kubeconfig")
519533
}
520534

535+
// ListNodes returns a list of nodes
521536
func ListNodes(ctx context.Context, clientset *kubernetes.Clientset) (*corev1.NodeList, error) {
522537
log.Tracef("getting a list of all nodes")
523538
return clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
524539
}
525540

541+
// ListPods returns a list of pods
526542
func ListPods(ctx context.Context, clientset *kubernetes.Clientset, namespace string) (*corev1.PodList, error) {
527543
log.Tracef("getting a list of all pods in namespace: %s", namespace)
528544
return clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
529545
}
530546

547+
// ListPodsWithPersistentVolumeClaims returns a list of pods with PVCs
531548
// kubectl get pods --all-namespaces -o=json | jq -c \
532549
// '.items[] | {name: .metadata.name, namespace: .metadata.namespace, claimName:.spec.volumes[] | select( has ("persistentVolumeClaim") ).persistentVolumeClaim.claimName }'
533550
func ListPodsWithPersistentVolumeClaims(ctx context.Context, clientset *kubernetes.Clientset, namespace string) ([]corev1.Pod, error) {
@@ -549,6 +566,7 @@ func ListPodsWithPersistentVolumeClaims(ctx context.Context, clientset *kubernet
549566
return sliceOfPodsWithPVCs, err
550567
}
551568

569+
// GetPVNameFromPVCName returns the name of persistent volume given a namespace and persistent volume claim name
552570
func GetPVNameFromPVCName(ctx context.Context, clientset *kubernetes.Clientset, namespace string, pvcName string) (string, error) {
553571
var pvName string
554572
pvc, err := clientset.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, pvcName, metav1.GetOptions{})
@@ -559,6 +577,7 @@ func GetPVNameFromPVCName(ctx context.Context, clientset *kubernetes.Clientset,
559577
return pvName, err
560578
}
561579

580+
// KubeConfigPath returns the path to kubeconfig file
562581
func KubeConfigPath() (string, error) {
563582
log.Debugf("getting kubeconfig path based on user's home dir")
564583
home, err := os.UserHomeDir()
@@ -568,11 +587,13 @@ func KubeConfigPath() (string, error) {
568587
return path.Join(home, ".kube", "config"), nil
569588
}
570589

590+
// ListPVCs returns a list of PVCs for a given namespace
571591
func ListPVCs(ctx context.Context, clientset *kubernetes.Clientset, namespace string) (*corev1.PersistentVolumeClaimList, error) {
572592
log.Tracef("getting a list of all PVCs in namespace: %s", namespace)
573593
return clientset.CoreV1().PersistentVolumeClaims(namespace).List(ctx, metav1.ListOptions{})
574594
}
575595

596+
// ListPVs returns a list of PVs, scoped to corresponding mapped PVCs based on the namespace
576597
func ListPVs(ctx context.Context, clientset *kubernetes.Clientset, namespace string) {
577598
pvList, _ := clientset.CoreV1().PersistentVolumes().List(ctx, metav1.ListOptions{})
578599
var pvcNames []string

0 commit comments

Comments
 (0)