Skip to content

Commit c942720

Browse files
committed
improve logging; add example pod and pvc yaml
1 parent 36e9b7c commit c942720

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

examples/pod.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: volume-test
5+
namespace: default
6+
spec:
7+
containers:
8+
- name: volume-test
9+
image: nginx:stable-alpine
10+
imagePullPolicy: IfNotPresent
11+
volumeMounts:
12+
- name: volv
13+
mountPath: /data
14+
ports:
15+
- containerPort: 80
16+
volumes:
17+
- name: volv
18+
persistentVolumeClaim:
19+
claimName: local-path-pvc

examples/pvc.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
name: local-path-pvc
5+
namespace: default
6+
spec:
7+
accessModes:
8+
- ReadWriteOnce
9+
# storageClassName: local-path
10+
resources:
11+
requests:
12+
storage: 2Gi

pkg/df-pv/root.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
// "github.com/gookit/color"
1212
// . "github.com/logrusorgru/aurora"
1313
// "k8s.io/cli-runtime/pkg/printers"
14+
// "github.com/olekukonko/tablewriter"
1415
"github.com/jedib0t/go-pretty/table"
1516
"github.com/jedib0t/go-pretty/text"
1617
"github.com/pkg/errors"
@@ -71,15 +72,24 @@ func runRootCommand(flags *flagpole) error {
7172
return errors.Cause(err)
7273
}
7374

74-
PrintUsingGoPretty(sliceOfOutputRowPVC)
75+
if nil == sliceOfOutputRowPVC || 0 > len(sliceOfOutputRowPVC) {
76+
ns := flags.namespace
77+
if 0 == len(ns) {
78+
ns = "all"
79+
}
80+
log.Infof("Either no volumes found in namespace/s: '%s' or the storage provisioner used for the volumes does not publish metrics to kubelet", ns)
81+
} else {
82+
PrintUsingGoPretty(sliceOfOutputRowPVC)
83+
}
84+
7585
return nil
7686
}
7787

7888
func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC) {
89+
// https://github.com/jedib0t/go-pretty/tree/master/table#table
7990
t := table.NewWriter()
80-
t.SetOutputMirror(os.Stdout)
81-
t.AppendHeader(table.Row{"PVC", "Namespace", "Pod", "Size", "Used", "Available", "%Used", "iused", "ifree", "%iused"})
8291

92+
t.AppendHeader(table.Row{"PVC", "Namespace", "Pod", "Size", "Used", "Available", "%Used", "iused", "ifree", "%iused"})
8393
for _, pvcRow := range sliceOfOutputRowPVC {
8494
percentageUsedColor := GetColorFromPercentageUsed(pvcRow.PercentageUsed)
8595
percentageIUsedColor := GetColorFromPercentageUsed(pvcRow.PercentageIUsed)
@@ -97,12 +107,14 @@ func PrintUsingGoPretty(sliceOfOutputRowPVC []*OutputRowPVC) {
97107
})
98108
}
99109

100-
// t.SetAutoIndex(true)
110+
// https://github.com/jedib0t/go-pretty/blob/master/table/style.go
101111
styleBold := table.StyleBold
102112
styleBold.Options = table.OptionsNoBordersAndSeparators
103113
t.SetStyle(styleBold)
104114
// t.Style().Options.SeparateRows = true
105-
t.Render()
115+
// t.SetAutoIndex(true)
116+
// t.SetOutputMirror(os.Stdout)
117+
fmt.Printf("\n%s\n\n", t.Render())
106118
}
107119

108120
func GetColorFromPercentageUsed(percentageUsed float64) text.Color {
@@ -457,8 +469,9 @@ func GetSliceOfOutputRowPVC(flags *flagpole) ([]*OutputRowPVC, error) {
457469

458470
func GetOutputRowPVCFromNodeChan(ctx context.Context, clientset *kubernetes.Clientset, nodeChan <-chan corev1.Node, desiredNamespace string, outputRowPVCChan chan<- *OutputRowPVC) error {
459471
for node := range nodeChan {
472+
log.Tracef("connecting to node: %s", node.Name)
460473
request := clientset.CoreV1().RESTClient().Get().Resource("nodes").Name(node.Name).SubResource("proxy").Suffix("stats/summary")
461-
responseRawArrayOfBytes, err := request.DoRaw(context.Background())
474+
responseRawArrayOfBytes, err := request.DoRaw(ctx)
462475
if err != nil {
463476
return errors.Wrapf(err, "failed to get stats from node")
464477
}
@@ -473,6 +486,7 @@ func GetOutputRowPVCFromNodeChan(ctx context.Context, clientset *kubernetes.Clie
473486
for _, vol := range pod.ListOfVolumes {
474487
outputRowPVC := GetOutputRowPVCFromPodAndVolume(pod, vol, desiredNamespace)
475488
if nil == outputRowPVC {
489+
log.Tracef("no pvc found for pod: '%s', vol: '%s', desiredNamespace: '%s'; continuing...", pod.PodRef.Name, vol.PvcRef.PvcName, desiredNamespace)
476490
continue
477491
}
478492
select {
@@ -493,6 +507,8 @@ func GetOutputRowPVCFromPodAndVolume(pod *Pod, vol *Volume, desiredNamespace str
493507
if 0 < len(desiredNamespace) {
494508
if vol.PvcRef.PvcNamespace != desiredNamespace {
495509
return nil
510+
} else {
511+
log.Debugf("restricting findings to namespace: '%s'", desiredNamespace)
496512
}
497513
}
498514

@@ -524,10 +540,12 @@ func GetOutputRowPVCFromPodAndVolume(pod *Pod, vol *Volume, desiredNamespace str
524540
// }
525541

526542
func ListNodes(ctx context.Context, clientset *kubernetes.Clientset) (*corev1.NodeList, error) {
543+
log.Tracef("getting a list of all nodes")
527544
return clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
528545
}
529546

530547
func KubeConfigPath() (string, error) {
548+
log.Debugf("getting kubeconfig path based on user's home dir")
531549
home, err := os.UserHomeDir()
532550
if err != nil {
533551
return "", errors.Wrapf(err, "unable to get home dir")

0 commit comments

Comments
 (0)