forked from kubernetes-retired/heapster
-
Notifications
You must be signed in to change notification settings - Fork 3
/
getters.go
514 lines (453 loc) · 15.2 KB
/
getters.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"errors"
"fmt"
"time"
"k8s.io/heapster/store/daystore"
"k8s.io/heapster/store/statstore"
)
// Errors for the Getter methods
var (
errModelEmpty = errors.New("the model is not populated yet")
errNoEntityMetrics = errors.New("the requested entity does not have any metrics yet")
errInvalidNode = errors.New("the requested node is not present in the cluster")
errNoSuchMetric = errors.New("the requested metric is not present in the model")
errNoSuchNamespace = errors.New("the requested namespace is not present in the cluster")
errNoSuchPod = errors.New("the requested pod is not present in the specified namespace")
errNoSuchContainer = errors.New("the requested container is not present in the model")
)
// GetClusterMetric returns a metric of the cluster entity, along with the latest timestamp.
// GetClusterMetric returns a slice of TimePoints for that metric, with times starting AFTER the starting timestamp.
func (rc *realModel) GetClusterMetric(req ClusterMetricRequest) ([]statstore.TimePoint, time.Time, error) {
var zeroTime time.Time
rc.lock.RLock()
defer rc.lock.RUnlock()
if len(rc.Metrics) == 0 {
return nil, zeroTime, errNoEntityMetrics
}
ts, ok := rc.Metrics[req.MetricName]
if !ok {
return nil, zeroTime, errNoSuchMetric
}
res := (*ts).Hour.Get(req.Start, req.End)
return res, rc.timestamp, nil
}
// GetNodeMetric returns a metric of a node entity, along with the latest timestamp.
// GetNodeMetric returns a slice of TimePoints for that metric, with times starting AFTER the starting timestamp.
func (rc *realModel) GetNodeMetric(req NodeMetricRequest) ([]statstore.TimePoint, time.Time, error) {
var zeroTime time.Time
rc.lock.RLock()
defer rc.lock.RUnlock()
if len(rc.Nodes) == 0 {
return nil, zeroTime, errModelEmpty
}
if _, ok := rc.Nodes[req.NodeName]; !ok {
return nil, zeroTime, errInvalidNode
}
if len(rc.Nodes[req.NodeName].Metrics) == 0 {
return nil, zeroTime, errNoEntityMetrics
}
ts, ok := rc.Nodes[req.NodeName].Metrics[req.MetricName]
if !ok {
return nil, zeroTime, errNoSuchMetric
}
res := (*ts).Hour.Get(req.Start, req.End)
return res, rc.timestamp, nil
}
// GetNamespaceMetric returns a metric of a namespace entity, along with the latest timestamp.
// GetNamespaceMetric receives as arguments the namespace, the metric name and a start time.
// GetNamespaceMetric returns a slice of TimePoints for that metric, with times starting AFTER the starting timestamp.
func (rc *realModel) GetNamespaceMetric(req NamespaceMetricRequest) ([]statstore.TimePoint, time.Time, error) {
var zeroTime time.Time
rc.lock.RLock()
defer rc.lock.RUnlock()
if len(rc.Namespaces) == 0 {
return nil, zeroTime, errModelEmpty
}
ns, ok := rc.Namespaces[req.NamespaceName]
if !ok {
return nil, zeroTime, errNoSuchNamespace
}
if len(ns.Metrics) == 0 {
return nil, zeroTime, errNoEntityMetrics
}
ts, ok := ns.Metrics[req.MetricName]
if !ok {
return nil, zeroTime, errNoSuchMetric
}
res := (*ts).Hour.Get(req.Start, req.End)
return res, rc.timestamp, nil
}
// GetPodMetric returns a metric of a Pod entity, along with the latest timestamp.
// GetPodMetric receives as arguments the namespace, the pod name, the metric name and a start time.
// GetPodMetric returns a slice of TimePoints for that metric, with times starting AFTER the starting timestamp.
func (rc *realModel) GetPodMetric(req PodMetricRequest) ([]statstore.TimePoint, time.Time, error) {
var zeroTime time.Time
rc.lock.RLock()
defer rc.lock.RUnlock()
if len(rc.Namespaces) == 0 {
return nil, zeroTime, errModelEmpty
}
ns, ok := rc.Namespaces[req.NamespaceName]
if !ok {
return nil, zeroTime, errNoSuchNamespace
}
pod, ok := ns.Pods[req.PodName]
if !ok {
return nil, zeroTime, errNoSuchPod
}
if len(pod.Metrics) == 0 {
return nil, zeroTime, errNoEntityMetrics
}
ts, ok := pod.Metrics[req.MetricName]
if !ok {
return nil, zeroTime, errNoSuchMetric
}
res := (*ts).Hour.Get(req.Start, req.End)
return res, rc.timestamp, nil
}
// GetBatchPodMetric returns metrics of a batch of Pod entities, along with the latest timestamp.
// GetBatchPodMetric receives as arguments the namespace, the pod names, the metric name and a start time.
// GetBatchPodMetric returns, for ach of the pods, slice of TimePoints for that metric, with times starting AFTER the starting timestamp
// (possibly empty if )
func (rc *realModel) GetBatchPodMetric(req BatchPodRequest) ([][]statstore.TimePoint, time.Time, error) {
var zeroTime time.Time
rc.lock.RLock()
defer rc.lock.RUnlock()
if len(rc.Namespaces) == 0 {
return nil, zeroTime, fmt.Errorf("the model is not populated yet")
}
ns, ok := rc.Namespaces[req.NamespaceName]
if !ok {
return nil, zeroTime, fmt.Errorf("the specified namespace is not present in the cluster")
}
result := make([][]statstore.TimePoint, len(req.PodNames))
for i, podName := range req.PodNames {
pod, ok := ns.Pods[podName]
if !ok {
result[i] = []statstore.TimePoint{}
continue
}
if len(pod.Metrics) == 0 {
result[i] = []statstore.TimePoint{}
continue
}
ts, ok := pod.Metrics[req.MetricName]
if !ok {
result[i] = []statstore.TimePoint{}
continue
}
res := (*ts).Hour.Get(req.Start, req.End)
result[i] = res
}
return result, rc.timestamp, nil
}
// GetPodContainerMetric returns a metric of a container entity that belongs in a Pod, along with the latest timestamp.
// GetPodContainerMetric receives as arguments the namespace, the pod name, the container name, the metric name and a start time.
// GetPodContainerMetric returns a slice of TimePoints for that metric, with times starting AFTER the starting timestamp.
func (rc *realModel) GetPodContainerMetric(req PodContainerMetricRequest) ([]statstore.TimePoint, time.Time, error) {
var zeroTime time.Time
rc.lock.RLock()
defer rc.lock.RUnlock()
if len(rc.Namespaces) == 0 {
return nil, zeroTime, errModelEmpty
}
ns, ok := rc.Namespaces[req.NamespaceName]
if !ok {
return nil, zeroTime, errNoSuchNamespace
}
pod, ok := ns.Pods[req.PodName]
if !ok {
return nil, zeroTime, errNoSuchPod
}
ctr, ok := pod.Containers[req.ContainerName]
if !ok {
return nil, zeroTime, errNoSuchContainer
}
ts, ok := ctr.Metrics[req.MetricName]
if !ok {
return nil, zeroTime, errNoSuchMetric
}
res := (*ts).Hour.Get(req.Start, req.End)
return res, rc.timestamp, nil
}
// GetFreeContainerMetric returns a metric of a free container entity, along with the latest timestamp.
// GetFreeContainerMetric receives as arguments the host name, the container name, the metric name and a start time.
// GetFreeContainerMetric returns a slice of TimePoints for that metric, with times starting AFTER the starting timestamp.
func (rc *realModel) GetFreeContainerMetric(req FreeContainerMetricRequest) ([]statstore.TimePoint, time.Time, error) {
var zeroTime time.Time
rc.lock.RLock()
defer rc.lock.RUnlock()
if len(rc.Nodes) == 0 {
return nil, zeroTime, errModelEmpty
}
node, ok := rc.Nodes[req.NodeName]
if !ok {
return nil, zeroTime, errInvalidNode
}
ctr, ok := node.FreeContainers[req.ContainerName]
if !ok {
return nil, zeroTime, errNoSuchContainer
}
ts, ok := ctr.Metrics[req.MetricName]
if !ok {
return nil, zeroTime, errNoSuchMetric
}
res := (*ts).Hour.Get(req.Start, req.End)
return res, rc.timestamp, nil
}
// makeEntityList creates an EntityListEntry from a map of metrics.
func makeEntityListEntry(name string, entities map[string]*daystore.DayStore) EntityListEntry {
newListEntry := EntityListEntry{}
cpu, ok := entities[cpuUsage]
if !ok {
newListEntry.CPUUsage = uint64(0)
} else {
_, lastHourMaxCPU, err := cpu.Hour.Last()
if err != nil {
newListEntry.CPUUsage = uint64(0)
} else {
newListEntry.CPUUsage = lastHourMaxCPU
}
}
mem, ok := entities[memWorking]
if !ok {
newListEntry.MemUsage = uint64(0)
} else {
_, lastHourMaxMem, err := mem.Hour.Last()
if err != nil {
newListEntry.MemUsage = uint64(0)
} else {
newListEntry.MemUsage = lastHourMaxMem
}
}
newListEntry.Name = name
return newListEntry
}
// GetNodes returns a slice of EntityListEntry for all the nodes that are available on the cluster.
func (rc *realModel) GetNodes() []EntityListEntry {
rc.lock.RLock()
defer rc.lock.RUnlock()
res := make([]EntityListEntry, 0)
for key, val := range rc.Nodes {
newEntity := makeEntityListEntry(key, val.Metrics)
// Ignore entities with no name populated (errors)
if newEntity.Name == "" {
continue
}
res = append(res, newEntity)
}
return res
}
// findPodNamespace finds the namespace name that a given PodInfo belongs to
// assumes cluster lock is taken by the caller.
func (rc *realModel) findPodNamespace(target *PodInfo) (string, error) {
for namespace, nsref := range rc.Namespaces {
for _, pod := range nsref.Pods {
if pod == target {
return namespace, nil
}
}
}
return "", fmt.Errorf("the specified pod does not belong under a namespace")
}
// GetNodePods returns the names and latest usage values of all the pods
// under a specific node.
func (rc *realModel) GetNodePods(hostname string) []EntityListEntry {
rc.lock.RLock()
defer rc.lock.RUnlock()
res := make([]EntityListEntry, 0)
noderef, ok := rc.Nodes[hostname]
if !ok {
return res
}
for podname, val := range noderef.Pods {
// Set the Pod name as <namespace> / <podname>
namespace, err := rc.findPodNamespace(val)
if err != nil {
break
}
newEntity := makeEntityListEntry(namespace+"/"+podname, val.Metrics)
if newEntity.Name == "" {
continue
}
res = append(res, newEntity)
}
return res
}
// GetNamespaces returns the names and latest usage values of all the namespaces
// that are available in the model.
func (rc *realModel) GetNamespaces() []EntityListEntry {
rc.lock.RLock()
defer rc.lock.RUnlock()
res := make([]EntityListEntry, 0)
for key, val := range rc.Namespaces {
newEntity := makeEntityListEntry(key, val.Metrics)
if newEntity.Name == "" {
continue
}
res = append(res, newEntity)
}
return res
}
// GetPods returns the names and latest usage values of all the pods that are
// available in the model under a specific namespace.
func (rc *realModel) GetPods(namespace string) []EntityListEntry {
rc.lock.RLock()
defer rc.lock.RUnlock()
res := make([]EntityListEntry, 0)
ns, ok := rc.Namespaces[namespace]
if !ok {
return res
}
for key, val := range ns.Pods {
newEntity := makeEntityListEntry(key, val.Metrics)
if newEntity.Name == "" {
continue
}
res = append(res, newEntity)
}
return res
}
// GetPodContainers returns the names and latest usage values of all the containers
// that are available in the model under a specific namespace and pod.
func (rc *realModel) GetPodContainers(namespace string, pod string) []EntityListEntry {
rc.lock.RLock()
defer rc.lock.RUnlock()
res := make([]EntityListEntry, 0)
ns, ok := rc.Namespaces[namespace]
if !ok {
return res
}
podref, ok := ns.Pods[pod]
if !ok {
return res
}
for key, val := range podref.Containers {
newEntity := makeEntityListEntry(key, val.Metrics)
if newEntity.Name == "" {
continue
}
res = append(res, newEntity)
}
return res
}
// GetFreeContainers returns the names and latest usage values of all the containers
// that are available in the model under a specific node.
func (rc *realModel) GetFreeContainers(node string) []EntityListEntry {
rc.lock.RLock()
defer rc.lock.RUnlock()
res := make([]EntityListEntry, 0)
noderef, ok := rc.Nodes[node]
if !ok {
return res
}
for key, val := range noderef.FreeContainers {
newEntity := makeEntityListEntry(key, val.Metrics)
if newEntity.Name == "" {
continue
}
res = append(res, newEntity)
}
return res
}
// GetAvailableMetrics returns the names of all metrics that are available on the cluster.
// Due to metric propagation, all entities of the cluster have the same metrics.
func (rc *realModel) GetAvailableMetrics() []string {
rc.lock.RLock()
defer rc.lock.RUnlock()
res := make([]string, 0)
for key := range rc.Metrics {
res = append(res, key)
}
return res
}
// uptime returns the uptime of an entity, given its InfoType
func (rc *realModel) uptime(infotype *InfoType) time.Duration {
return rc.timestamp.Sub(infotype.Creation)
}
// getClusterStats extracts the derived stats and uptime for the Cluster entity.
func (rc *realModel) GetClusterStats() (map[string]StatBundle, time.Duration, error) {
rc.lock.RLock()
defer rc.lock.RUnlock()
return getStats(rc.InfoType), rc.uptime(&rc.InfoType), nil
}
// getNodeStats extracts the derived stats and uptime for a Node entity.
func (rc *realModel) GetNodeStats(req NodeRequest) (map[string]StatBundle, time.Duration, error) {
rc.lock.RLock()
defer rc.lock.RUnlock()
node, ok := rc.Nodes[req.NodeName]
if !ok {
return nil, time.Duration(0), errInvalidNode
}
return getStats(node.InfoType), rc.uptime(&node.InfoType), nil
}
// getNamespaceStats extracts the derived stats and uptime for a Namespace entity.
func (rc *realModel) GetNamespaceStats(req NamespaceRequest) (map[string]StatBundle, time.Duration, error) {
rc.lock.RLock()
defer rc.lock.RUnlock()
ns, ok := rc.Namespaces[req.NamespaceName]
if !ok {
return nil, time.Duration(0), errNoSuchNamespace
}
return getStats(ns.InfoType), rc.uptime(&ns.InfoType), nil
}
// getPodStats extracts the derived stats and uptime for a Pod entity.
func (rc *realModel) GetPodStats(req PodRequest) (map[string]StatBundle, time.Duration, error) {
rc.lock.RLock()
defer rc.lock.RUnlock()
ns, ok := rc.Namespaces[req.NamespaceName]
if !ok {
return nil, time.Duration(0), errNoSuchNamespace
}
pod, ok := ns.Pods[req.PodName]
if !ok {
return nil, time.Duration(0), errNoSuchPod
}
return getStats(pod.InfoType), rc.uptime(&pod.InfoType), nil
}
// getPodContainerStats extracts the derived stats and uptime for a Pod Container entity.
func (rc *realModel) GetPodContainerStats(req PodContainerRequest) (map[string]StatBundle, time.Duration, error) {
rc.lock.RLock()
defer rc.lock.RUnlock()
ns, ok := rc.Namespaces[req.NamespaceName]
if !ok {
return nil, time.Duration(0), errNoSuchNamespace
}
pod, ok := ns.Pods[req.PodName]
if !ok {
return nil, time.Duration(0), errNoSuchPod
}
ctr, ok := pod.Containers[req.ContainerName]
if !ok {
return nil, time.Duration(0), errNoSuchContainer
}
return getStats(ctr.InfoType), rc.uptime(&ctr.InfoType), nil
}
// getFreeContainerStats extracts the derived stats and uptime for a Pod Container entity.
func (rc *realModel) GetFreeContainerStats(req FreeContainerRequest) (map[string]StatBundle, time.Duration, error) {
rc.lock.RLock()
defer rc.lock.RUnlock()
node, ok := rc.Nodes[req.NodeName]
if !ok {
return nil, time.Duration(0), errInvalidNode
}
ctr, ok := node.FreeContainers[req.ContainerName]
if !ok {
return nil, time.Duration(0), errNoSuchContainer
}
return getStats(ctr.InfoType), rc.uptime(&ctr.InfoType), nil
}