-
Notifications
You must be signed in to change notification settings - Fork 75
/
getUsage.go
80 lines (65 loc) · 1.63 KB
/
getUsage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package pvcmonitor
import (
"encoding/json"
"github.com/abahmed/kwatch/util"
"github.com/sirupsen/logrus"
)
type SummaryResponse struct {
Pods []*Pod `json:"pods"`
}
type Pod struct {
PodRef *Ref `json:"podRef"`
Volume []*Volume `json:"volume"`
}
type Volume struct {
UsedBytes int64 `json:"usedBytes"`
CapacityBytes int64 `json:"capacityBytes"`
Name string `json:"name"`
PvcRef *Ref `json:"pvcRef"`
}
type Ref struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}
// getNodeUsage gets list of pvc usage for specific node
func (p *PvcMonitor) getNodeUsage(nodeName string) ([]*PvcUsage, error) {
result := make([]*PvcUsage, 0)
summaryResponse, err := util.GetNodeSummary(p.client, nodeName)
if err != nil {
return result, err
}
var summaryObj SummaryResponse
err = json.Unmarshal(summaryResponse, &summaryObj)
if err != nil {
return result, err
}
for _, pod := range summaryObj.Pods {
for _, vol := range pod.Volume {
if vol.PvcRef == nil || len(vol.PvcRef.Name) == 0 {
continue
}
pvName, err :=
util.GetPVNameFromPVC(
p.client,
pod.PodRef.Namespace,
vol.PvcRef.Name)
if err != nil {
logrus.Errorf(
"failed to get pv name for pvc %s: %s",
vol.PvcRef.Name,
err.Error())
continue
}
percentage :=
(float64(vol.UsedBytes) / float64(vol.CapacityBytes)) * 100.0
result = append(result, &PvcUsage{
Name: vol.PvcRef.Name,
PVName: pvName,
Namespace: pod.PodRef.Namespace,
PodName: pod.PodRef.Name,
UsagePercentage: percentage,
})
}
}
return result, nil
}