/
statsprovider.go
160 lines (120 loc) · 3.48 KB
/
statsprovider.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package provider
import (
"errors"
"log"
"time"
"github.com/finitum/node-cli/stats"
corev1 "k8s.io/api/core/v1"
"github.com/atlarge-research/apate/pkg/scenario/events"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type resources struct {
cpu uint64
memory uint64
ephemeralStorage uint64
}
// Stats is a simple wrapper for statistics fields
type Stats struct {
statsSummary *stats.Summary
}
// NewStats creates a new Stats instance
func NewStats() *Stats {
return &Stats{}
}
func (p *Provider) now() metav1.Time {
return metav1.NewTime(time.Now())
}
// GetStatsSummary should return a node level statistic report
func (p *Provider) GetStatsSummary() (*stats.Summary, error) {
if p.Stats.statsSummary == nil {
return nil, errors.New("statsSummary is nil, please call updateStatsSummary first")
}
return p.Stats.statsSummary, nil
}
func (p *Provider) updateStatsSummary() {
pods := p.getAggregatePodStats()
p.Stats.statsSummary = &stats.Summary{
Node: p.getNodeStats(pods),
Pods: pods,
}
}
// Node statistics
func (p *Provider) getNodeStats(pods []stats.PodStats) stats.NodeStats {
aMem, uMem := p.memoryStats(pods)
aEph, cEph, uEph := p.ephemeralStats(pods)
aSto, cSto, uSto := p.storageStats(pods)
return stats.NodeStats{
Name: p.NodeInfo.Name,
UsageNanoCores: p.cpuStats(pods),
AvailableBytesMemory: aMem,
UsageBytesMemory: uMem,
AvailableBytesEphemeral: aEph,
CapacityBytesEphemeral: cEph,
UsedBytesEphemeral: uEph,
AvailableBytesStorage: aSto,
CapacityBytesStorage: cSto,
UsedBytesStorage: uSto,
Pods: uint64(len(pods)),
}
}
func (p *Provider) cpuStats(pods []stats.PodStats) uint64 {
usage := uint64(0)
for _, pod := range pods {
usage += pod.UsageNanoCores
}
return usage
}
func (p *Provider) memoryStats(pods []stats.PodStats) (uint64, uint64) {
usage := uint64(0)
for _, pod := range pods {
usage += pod.UsageBytesMemory
}
available := uint64(p.Resources.Memory) - usage
return available, usage
}
func (p *Provider) ephemeralStats(pods []stats.PodStats) (uint64, uint64, uint64) {
capacity := uint64(p.Resources.EphemeralStorage)
usage := uint64(0)
for _, pod := range pods {
usage += pod.UsedBytesEphemeral
}
free := capacity - usage
return free, capacity, usage
}
func (p *Provider) storageStats(pods []stats.PodStats) (uint64, uint64, uint64) {
capacity := uint64(p.Resources.Storage)
usage := uint64(0)
for _, pod := range pods {
usage += pod.UsedBytesStorage
}
free := capacity - usage
return free, capacity, usage
}
func (p *Provider) getAggregatePodStats() []stats.PodStats {
var statistics []stats.PodStats
for _, pod := range p.Pods.GetAllPods() {
statistics = append(statistics, *p.getPodStats(pod))
}
return statistics
}
func (p *Provider) getPodStats(pod *corev1.Pod) *stats.PodStats {
unconvertedStats, err := (*p.Store).GetPodFlag(pod, events.PodResources)
if err != nil {
log.Printf("error while retrieving pod flag for resources: %v\n", err)
return addPodSpecificStats(pod, &stats.PodStats{})
}
statistics, ok := unconvertedStats.(*stats.PodStats)
if !ok {
log.Printf("unable to convert '%v' to PodStats\n", unconvertedStats)
return addPodSpecificStats(pod, &stats.PodStats{})
}
return addPodSpecificStats(pod, statistics)
}
func addPodSpecificStats(pod *corev1.Pod, statistics *stats.PodStats) *stats.PodStats {
statistics.PodRef = stats.PodReference{
Name: pod.Name,
Namespace: pod.Namespace,
UID: string(pod.UID),
}
return statistics
}