/
cluster.go
1577 lines (1347 loc) · 57.3 KB
/
cluster.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package costmodel
import (
"fmt"
"net"
"strconv"
"time"
"github.com/opencost/opencost/pkg/cloud/provider"
prometheus "github.com/prometheus/client_golang/api"
"golang.org/x/exp/slices"
"github.com/opencost/opencost/core/pkg/log"
"github.com/opencost/opencost/core/pkg/opencost"
"github.com/opencost/opencost/core/pkg/util/timeutil"
"github.com/opencost/opencost/pkg/cloud/models"
"github.com/opencost/opencost/pkg/env"
"github.com/opencost/opencost/pkg/prom"
)
const (
queryClusterCores = `sum(
avg(avg_over_time(kube_node_status_capacity_cpu_cores{%s}[%s] %s)) by (node, %s) * avg(avg_over_time(node_cpu_hourly_cost{%s}[%s] %s)) by (node, %s) * 730 +
avg(avg_over_time(node_gpu_hourly_cost{%s}[%s] %s)) by (node, %s) * 730
) by (%s)`
queryClusterRAM = `sum(
avg(avg_over_time(kube_node_status_capacity_memory_bytes{%s}[%s] %s)) by (node, %s) / 1024 / 1024 / 1024 * avg(avg_over_time(node_ram_hourly_cost{%s}[%s] %s)) by (node, %s) * 730
) by (%s)`
queryStorage = `sum(
avg(avg_over_time(pv_hourly_cost{%s}[%s] %s)) by (persistentvolume, %s) * 730
* avg(avg_over_time(kube_persistentvolume_capacity_bytes{%s}[%s] %s)) by (persistentvolume, %s) / 1024 / 1024 / 1024
) by (%s) %s`
queryTotal = `sum(avg(node_total_hourly_cost{%s}) by (node, %s)) * 730 +
sum(
avg(avg_over_time(pv_hourly_cost{%s}[1h])) by (persistentvolume, %s) * 730
* avg(avg_over_time(kube_persistentvolume_capacity_bytes{%s}[1h])) by (persistentvolume, %s) / 1024 / 1024 / 1024
) by (%s) %s`
queryNodes = `sum(avg(node_total_hourly_cost{%s}) by (node, %s)) * 730 %s`
)
const maxLocalDiskSize = 200 // AWS limits root disks to 100 Gi, and occasional metric errors in filesystem size should not contribute to large costs.
// Costs represents cumulative and monthly cluster costs over a given duration. Costs
// are broken down by cores, memory, and storage.
type ClusterCosts struct {
Start *time.Time `json:"startTime"`
End *time.Time `json:"endTime"`
CPUCumulative float64 `json:"cpuCumulativeCost"`
CPUMonthly float64 `json:"cpuMonthlyCost"`
CPUBreakdown *ClusterCostsBreakdown `json:"cpuBreakdown"`
GPUCumulative float64 `json:"gpuCumulativeCost"`
GPUMonthly float64 `json:"gpuMonthlyCost"`
RAMCumulative float64 `json:"ramCumulativeCost"`
RAMMonthly float64 `json:"ramMonthlyCost"`
RAMBreakdown *ClusterCostsBreakdown `json:"ramBreakdown"`
StorageCumulative float64 `json:"storageCumulativeCost"`
StorageMonthly float64 `json:"storageMonthlyCost"`
StorageBreakdown *ClusterCostsBreakdown `json:"storageBreakdown"`
TotalCumulative float64 `json:"totalCumulativeCost"`
TotalMonthly float64 `json:"totalMonthlyCost"`
DataMinutes float64
}
// ClusterCostsBreakdown provides percentage-based breakdown of a resource by
// categories: user for user-space (i.e. non-system) usage, system, and idle.
type ClusterCostsBreakdown struct {
Idle float64 `json:"idle"`
Other float64 `json:"other"`
System float64 `json:"system"`
User float64 `json:"user"`
}
// NewClusterCostsFromCumulative takes cumulative cost data over a given time range, computes
// the associated monthly rate data, and returns the Costs.
func NewClusterCostsFromCumulative(cpu, gpu, ram, storage float64, window, offset time.Duration, dataHours float64) (*ClusterCosts, error) {
start, end := timeutil.ParseTimeRange(window, offset)
// If the number of hours is not given (i.e. is zero) compute one from the window and offset
if dataHours == 0 {
dataHours = end.Sub(start).Hours()
}
// Do not allow zero-length windows to prevent divide-by-zero issues
if dataHours == 0 {
return nil, fmt.Errorf("illegal time range: window %s, offset %s", window, offset)
}
cc := &ClusterCosts{
Start: &start,
End: &end,
CPUCumulative: cpu,
GPUCumulative: gpu,
RAMCumulative: ram,
StorageCumulative: storage,
TotalCumulative: cpu + gpu + ram + storage,
CPUMonthly: cpu / dataHours * (timeutil.HoursPerMonth),
GPUMonthly: gpu / dataHours * (timeutil.HoursPerMonth),
RAMMonthly: ram / dataHours * (timeutil.HoursPerMonth),
StorageMonthly: storage / dataHours * (timeutil.HoursPerMonth),
}
cc.TotalMonthly = cc.CPUMonthly + cc.GPUMonthly + cc.RAMMonthly + cc.StorageMonthly
return cc, nil
}
type Disk struct {
Cluster string
Name string
ProviderID string
StorageClass string
VolumeName string
ClaimName string
ClaimNamespace string
Cost float64
Bytes float64
// These two fields may not be available at all times because they rely on
// a new set of metrics that may or may not be available. Thus, they must
// be nilable to represent the complete absence of the data.
//
// In other words, nilability here lets us distinguish between
// "metric is not available" and "metric is available but is 0".
//
// They end in "Ptr" to distinguish from an earlier version in order to
// ensure that all usages are checked for nil.
BytesUsedAvgPtr *float64
BytesUsedMaxPtr *float64
Local bool
Start time.Time
End time.Time
Minutes float64
Breakdown *ClusterCostsBreakdown
}
type DiskIdentifier struct {
Cluster string
Name string
}
func ClusterDisks(client prometheus.Client, provider models.Provider, start, end time.Time) (map[DiskIdentifier]*Disk, error) {
// Start from the time "end", querying backwards
t := end
// minsPerResolution determines accuracy and resource use for the following
// queries. Smaller values (higher resolution) result in better accuracy,
// but more expensive queries, and vice-a-versa.
resolution := env.GetETLResolution()
//Ensuring if ETL_RESOLUTION_SECONDS is less than 60s default it to 1m
var minsPerResolution int
if minsPerResolution = int(resolution.Minutes()); int(resolution.Minutes()) == 0 {
minsPerResolution = 1
log.DedupedWarningf(3, "ClusterDisks(): Configured ETL resolution (%d seconds) is below the 60 seconds threshold. Overriding with 1 minute.", int(resolution.Seconds()))
}
durStr := timeutil.DurationString(end.Sub(start))
if durStr == "" {
return nil, fmt.Errorf("illegal duration value for %s", opencost.NewClosedWindow(start, end))
}
// hourlyToCumulative is a scaling factor that, when multiplied by an hourly
// value, converts it to a cumulative value; i.e.
// [$/hr] * [min/res]*[hr/min] = [$/res]
hourlyToCumulative := float64(minsPerResolution) * (1.0 / 60.0)
// TODO niko/assets how do we not hard-code this price?
costPerGBHr := 0.04 / 730.0
ctx := prom.NewNamedContext(client, prom.ClusterContextName)
queryPVCost := fmt.Sprintf(`avg(avg_over_time(pv_hourly_cost{%s}[%s])) by (%s, persistentvolume,provider_id)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryPVSize := fmt.Sprintf(`avg(avg_over_time(kube_persistentvolume_capacity_bytes{%s}[%s])) by (%s, persistentvolume)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryActiveMins := fmt.Sprintf(`avg(kube_persistentvolume_capacity_bytes{%s}) by (%s, persistentvolume)[%s:%dm]`, env.GetPromClusterFilter(), env.GetPromClusterLabel(), durStr, minsPerResolution)
queryPVStorageClass := fmt.Sprintf(`avg(avg_over_time(kubecost_pv_info{%s}[%s])) by (%s, persistentvolume, storageclass)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryPVUsedAvg := fmt.Sprintf(`avg(avg_over_time(kubelet_volume_stats_used_bytes{%s}[%s])) by (%s, persistentvolumeclaim, namespace)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryPVUsedMax := fmt.Sprintf(`max(max_over_time(kubelet_volume_stats_used_bytes{%s}[%s])) by (%s, persistentvolumeclaim, namespace)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryPVCInfo := fmt.Sprintf(`avg(avg_over_time(kube_persistentvolumeclaim_info{%s}[%s])) by (%s, volumename, persistentvolumeclaim, namespace)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryLocalStorageCost := fmt.Sprintf(`sum_over_time(sum(container_fs_limit_bytes{device!="tmpfs", id="/", %s}) by (instance, %s)[%s:%dm]) / 1024 / 1024 / 1024 * %f * %f`, env.GetPromClusterFilter(), env.GetPromClusterLabel(), durStr, minsPerResolution, hourlyToCumulative, costPerGBHr)
queryLocalStorageUsedCost := fmt.Sprintf(`sum_over_time(sum(container_fs_usage_bytes{device!="tmpfs", id="/", %s}) by (instance, %s)[%s:%dm]) / 1024 / 1024 / 1024 * %f * %f`, env.GetPromClusterFilter(), env.GetPromClusterLabel(), durStr, minsPerResolution, hourlyToCumulative, costPerGBHr)
queryLocalStorageUsedAvg := fmt.Sprintf(`avg(sum(avg_over_time(container_fs_usage_bytes{device!="tmpfs", id="/", %s}[%s])) by (instance, %s, job)) by (instance, %s)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel(), env.GetPromClusterLabel())
queryLocalStorageUsedMax := fmt.Sprintf(`max(sum(max_over_time(container_fs_usage_bytes{device!="tmpfs", id="/", %s}[%s])) by (instance, %s, job)) by (instance, %s)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel(), env.GetPromClusterLabel())
queryLocalStorageBytes := fmt.Sprintf(`avg_over_time(sum(container_fs_limit_bytes{device!="tmpfs", id="/", %s}) by (instance, %s)[%s:%dm])`, env.GetPromClusterFilter(), env.GetPromClusterLabel(), durStr, minsPerResolution)
queryLocalActiveMins := fmt.Sprintf(`count(node_total_hourly_cost{%s}) by (%s, node)[%s:%dm]`, env.GetPromClusterFilter(), env.GetPromClusterLabel(), durStr, minsPerResolution)
resChPVCost := ctx.QueryAtTime(queryPVCost, t)
resChPVSize := ctx.QueryAtTime(queryPVSize, t)
resChActiveMins := ctx.QueryAtTime(queryActiveMins, t)
resChPVStorageClass := ctx.QueryAtTime(queryPVStorageClass, t)
resChPVUsedAvg := ctx.QueryAtTime(queryPVUsedAvg, t)
resChPVUsedMax := ctx.QueryAtTime(queryPVUsedMax, t)
resChPVCInfo := ctx.QueryAtTime(queryPVCInfo, t)
resChLocalStorageCost := ctx.QueryAtTime(queryLocalStorageCost, t)
resChLocalStorageUsedCost := ctx.QueryAtTime(queryLocalStorageUsedCost, t)
resChLocalStoreageUsedAvg := ctx.QueryAtTime(queryLocalStorageUsedAvg, t)
resChLocalStoreageUsedMax := ctx.QueryAtTime(queryLocalStorageUsedMax, t)
resChLocalStorageBytes := ctx.QueryAtTime(queryLocalStorageBytes, t)
resChLocalActiveMins := ctx.QueryAtTime(queryLocalActiveMins, t)
resPVCost, _ := resChPVCost.Await()
resPVSize, _ := resChPVSize.Await()
resActiveMins, _ := resChActiveMins.Await()
resPVStorageClass, _ := resChPVStorageClass.Await()
resPVUsedAvg, _ := resChPVUsedAvg.Await()
resPVUsedMax, _ := resChPVUsedMax.Await()
resPVCInfo, _ := resChPVCInfo.Await()
resLocalStorageCost, _ := resChLocalStorageCost.Await()
resLocalStorageUsedCost, _ := resChLocalStorageUsedCost.Await()
resLocalStorageUsedAvg, _ := resChLocalStoreageUsedAvg.Await()
resLocalStorageUsedMax, _ := resChLocalStoreageUsedMax.Await()
resLocalStorageBytes, _ := resChLocalStorageBytes.Await()
resLocalActiveMins, _ := resChLocalActiveMins.Await()
if ctx.HasErrors() {
return nil, ctx.ErrorCollection()
}
diskMap := map[DiskIdentifier]*Disk{}
for _, result := range resPVCInfo {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
volumeName, err := result.GetString("volumename")
if err != nil {
log.Debugf("ClusterDisks: pv claim data missing volumename")
continue
}
claimName, err := result.GetString("persistentvolumeclaim")
if err != nil {
log.Debugf("ClusterDisks: pv claim data missing persistentvolumeclaim")
continue
}
claimNamespace, err := result.GetString("namespace")
if err != nil {
log.Debugf("ClusterDisks: pv claim data missing namespace")
continue
}
key := DiskIdentifier{cluster, volumeName}
if _, ok := diskMap[key]; !ok {
diskMap[key] = &Disk{
Cluster: cluster,
Name: volumeName,
Breakdown: &ClusterCostsBreakdown{},
}
}
diskMap[key].VolumeName = volumeName
diskMap[key].ClaimName = claimName
diskMap[key].ClaimNamespace = claimNamespace
}
pvCosts(diskMap, resolution, resActiveMins, resPVSize, resPVCost, resPVUsedAvg, resPVUsedMax, resPVCInfo, provider, opencost.NewClosedWindow(start, end))
for _, result := range resLocalStorageCost {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
name, err := result.GetString("instance")
if err != nil {
log.Warnf("ClusterDisks: local storage data missing instance")
continue
}
cost := result.Values[0].Value
key := DiskIdentifier{cluster, name}
if _, ok := diskMap[key]; !ok {
diskMap[key] = &Disk{
Cluster: cluster,
Name: name,
Breakdown: &ClusterCostsBreakdown{},
Local: true,
}
}
diskMap[key].Cost += cost
//Assigning explicitly the storage class of local storage to local
diskMap[key].StorageClass = opencost.LocalStorageClass
}
for _, result := range resLocalStorageUsedCost {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
name, err := result.GetString("instance")
if err != nil {
log.Warnf("ClusterDisks: local storage usage data missing instance")
continue
}
cost := result.Values[0].Value
key := DiskIdentifier{cluster, name}
if _, ok := diskMap[key]; !ok {
diskMap[key] = &Disk{
Cluster: cluster,
Name: name,
Breakdown: &ClusterCostsBreakdown{},
Local: true,
}
}
diskMap[key].Breakdown.System = cost / diskMap[key].Cost
}
for _, result := range resLocalStorageUsedAvg {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
name, err := result.GetString("instance")
if err != nil {
log.Warnf("ClusterDisks: local storage data missing instance")
continue
}
bytesAvg := result.Values[0].Value
key := DiskIdentifier{cluster, name}
if _, ok := diskMap[key]; !ok {
diskMap[key] = &Disk{
Cluster: cluster,
Name: name,
Breakdown: &ClusterCostsBreakdown{},
Local: true,
}
}
diskMap[key].BytesUsedAvgPtr = &bytesAvg
}
for _, result := range resLocalStorageUsedMax {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
name, err := result.GetString("instance")
if err != nil {
log.Warnf("ClusterDisks: local storage data missing instance")
continue
}
bytesMax := result.Values[0].Value
key := DiskIdentifier{cluster, name}
if _, ok := diskMap[key]; !ok {
diskMap[key] = &Disk{
Cluster: cluster,
Name: name,
Breakdown: &ClusterCostsBreakdown{},
Local: true,
}
}
diskMap[key].BytesUsedMaxPtr = &bytesMax
}
for _, result := range resLocalStorageBytes {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
name, err := result.GetString("instance")
if err != nil {
log.Warnf("ClusterDisks: local storage data missing instance")
continue
}
bytes := result.Values[0].Value
key := DiskIdentifier{cluster, name}
if _, ok := diskMap[key]; !ok {
diskMap[key] = &Disk{
Cluster: cluster,
Name: name,
Breakdown: &ClusterCostsBreakdown{},
Local: true,
}
}
diskMap[key].Bytes = bytes
if bytes/1024/1024/1024 > maxLocalDiskSize {
log.DedupedWarningf(5, "Deleting large root disk/localstorage disk from analysis")
delete(diskMap, key)
}
}
for _, result := range resLocalActiveMins {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
name, err := result.GetString("node")
if err != nil {
log.DedupedWarningf(5, "ClusterDisks: local active mins data missing instance")
continue
}
key := DiskIdentifier{cluster, name}
if _, ok := diskMap[key]; !ok {
log.DedupedWarningf(5, "ClusterDisks: local active mins for unidentified disk or disk deleted from analysis")
continue
}
if len(result.Values) == 0 {
continue
}
s := time.Unix(int64(result.Values[0].Timestamp), 0)
e := time.Unix(int64(result.Values[len(result.Values)-1].Timestamp), 0)
mins := e.Sub(s).Minutes()
// TODO niko/assets if mins >= threshold, interpolate for missing data?
diskMap[key].End = e
diskMap[key].Start = s
diskMap[key].Minutes = mins
}
var unTracedDiskLogData []DiskIdentifier
//Iterating through Persistent Volume given by custom metrics kubecost_pv_info and assign the storage class if known and __unknown__ if not populated.
for _, result := range resPVStorageClass {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
name, _ := result.GetString("persistentvolume")
key := DiskIdentifier{cluster, name}
if _, ok := diskMap[key]; !ok {
if !slices.Contains(unTracedDiskLogData, key) {
unTracedDiskLogData = append(unTracedDiskLogData, key)
}
continue
}
if len(result.Values) == 0 {
continue
}
storageClass, err := result.GetString("storageclass")
if err != nil {
diskMap[key].StorageClass = opencost.UnknownStorageClass
} else {
diskMap[key].StorageClass = storageClass
}
}
// Logging the unidentified disk information outside the loop
for _, unIdentifiedDisk := range unTracedDiskLogData {
log.Warnf("ClusterDisks: Cluster %s has Storage Class information for unidentified disk %s or disk deleted from analysis", unIdentifiedDisk.Cluster, unIdentifiedDisk.Name)
}
for _, disk := range diskMap {
// Apply all remaining RAM to Idle
disk.Breakdown.Idle = 1.0 - (disk.Breakdown.System + disk.Breakdown.Other + disk.Breakdown.User)
// Set provider Id to the name for reconciliation
if disk.ProviderID == "" {
disk.ProviderID = disk.Name
}
}
return diskMap, nil
}
type NodeOverhead struct {
CpuOverheadFraction float64
RamOverheadFraction float64
}
type Node struct {
Cluster string
Name string
ProviderID string
NodeType string
CPUCost float64
CPUCores float64
GPUCost float64
GPUCount float64
RAMCost float64
RAMBytes float64
Discount float64
Preemptible bool
CPUBreakdown *ClusterCostsBreakdown
RAMBreakdown *ClusterCostsBreakdown
Start time.Time
End time.Time
Minutes float64
Labels map[string]string
CostPerCPUHr float64
CostPerRAMGiBHr float64
CostPerGPUHr float64
Overhead *NodeOverhead
}
// GKE lies about the number of cores e2 nodes have. This table
// contains a mapping from node type -> actual CPU cores
// for those cases.
var partialCPUMap = map[string]float64{
"e2-micro": 0.25,
"e2-small": 0.5,
"e2-medium": 1.0,
}
type NodeIdentifier struct {
Cluster string
Name string
ProviderID string
}
type nodeIdentifierNoProviderID struct {
Cluster string
Name string
}
func costTimesMinuteAndCount(activeDataMap map[NodeIdentifier]activeData, costMap map[NodeIdentifier]float64, resourceCountMap map[nodeIdentifierNoProviderID]float64) {
for k, v := range activeDataMap {
keyNon := nodeIdentifierNoProviderID{
Cluster: k.Cluster,
Name: k.Name,
}
if cost, ok := costMap[k]; ok {
minutes := v.minutes
count := 1.0
if c, ok := resourceCountMap[keyNon]; ok {
count = c
}
costMap[k] = cost * (minutes / 60) * count
}
}
}
func costTimesMinute(activeDataMap map[NodeIdentifier]activeData, costMap map[NodeIdentifier]float64) {
for k, v := range activeDataMap {
if cost, ok := costMap[k]; ok {
minutes := v.minutes
costMap[k] = cost * (minutes / 60)
}
}
}
func ClusterNodes(cp models.Provider, client prometheus.Client, start, end time.Time) (map[NodeIdentifier]*Node, error) {
// Start from the time "end", querying backwards
t := end
// minsPerResolution determines accuracy and resource use for the following
// queries. Smaller values (higher resolution) result in better accuracy,
// but more expensive queries, and vice-a-versa.
resolution := env.GetETLResolution()
//Ensuring if ETL_RESOLUTION_SECONDS is less than 60s default it to 1m
var minsPerResolution int
if minsPerResolution = int(resolution.Minutes()); int(resolution.Minutes()) == 0 {
minsPerResolution = 1
log.DedupedWarningf(3, "ClusterNodes(): Configured ETL resolution (%d seconds) is below the 60 seconds threshold. Overriding with 1 minute.", int(resolution.Seconds()))
}
durStr := timeutil.DurationString(end.Sub(start))
if durStr == "" {
return nil, fmt.Errorf("illegal duration value for %s", opencost.NewClosedWindow(start, end))
}
requiredCtx := prom.NewNamedContext(client, prom.ClusterContextName)
optionalCtx := prom.NewNamedContext(client, prom.ClusterOptionalContextName)
queryNodeCPUHourlyCost := fmt.Sprintf(`avg(avg_over_time(node_cpu_hourly_cost{%s}[%s])) by (%s, node, instance_type, provider_id)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryNodeCPUCoresCapacity := fmt.Sprintf(`avg(avg_over_time(kube_node_status_capacity_cpu_cores{%s}[%s])) by (%s, node)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryNodeCPUCoresAllocatable := fmt.Sprintf(`avg(avg_over_time(kube_node_status_allocatable_cpu_cores{%s}[%s])) by (%s, node)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryNodeRAMHourlyCost := fmt.Sprintf(`avg(avg_over_time(node_ram_hourly_cost{%s}[%s])) by (%s, node, instance_type, provider_id) / 1024 / 1024 / 1024`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryNodeRAMBytesCapacity := fmt.Sprintf(`avg(avg_over_time(kube_node_status_capacity_memory_bytes{%s}[%s])) by (%s, node)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryNodeRAMBytesAllocatable := fmt.Sprintf(`avg(avg_over_time(kube_node_status_allocatable_memory_bytes{%s}[%s])) by (%s, node)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryNodeGPUCount := fmt.Sprintf(`avg(avg_over_time(node_gpu_count{%s}[%s])) by (%s, node, provider_id)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryNodeGPUHourlyCost := fmt.Sprintf(`avg(avg_over_time(node_gpu_hourly_cost{%s}[%s])) by (%s, node, instance_type, provider_id)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryNodeCPUModeTotal := fmt.Sprintf(`sum(rate(node_cpu_seconds_total{%s}[%s:%dm])) by (kubernetes_node, %s, mode)`, env.GetPromClusterFilter(), durStr, minsPerResolution, env.GetPromClusterLabel())
queryNodeRAMSystemPct := fmt.Sprintf(`sum(sum_over_time(container_memory_working_set_bytes{container_name!="POD",container_name!="",namespace="kube-system", %s}[%s:%dm])) by (instance, %s) / avg(label_replace(sum(sum_over_time(kube_node_status_capacity_memory_bytes{%s}[%s:%dm])) by (node, %s), "instance", "$1", "node", "(.*)")) by (instance, %s)`, env.GetPromClusterFilter(), durStr, minsPerResolution, env.GetPromClusterLabel(), env.GetPromClusterFilter(), durStr, minsPerResolution, env.GetPromClusterLabel(), env.GetPromClusterLabel())
queryNodeRAMUserPct := fmt.Sprintf(`sum(sum_over_time(container_memory_working_set_bytes{container_name!="POD",container_name!="",namespace!="kube-system", %s}[%s:%dm])) by (instance, %s) / avg(label_replace(sum(sum_over_time(kube_node_status_capacity_memory_bytes{%s}[%s:%dm])) by (node, %s), "instance", "$1", "node", "(.*)")) by (instance, %s)`, env.GetPromClusterFilter(), durStr, minsPerResolution, env.GetPromClusterLabel(), env.GetPromClusterFilter(), durStr, minsPerResolution, env.GetPromClusterLabel(), env.GetPromClusterLabel())
queryActiveMins := fmt.Sprintf(`avg(node_total_hourly_cost{%s}) by (node, %s, provider_id)[%s:%dm]`, env.GetPromClusterFilter(), env.GetPromClusterLabel(), durStr, minsPerResolution)
queryIsSpot := fmt.Sprintf(`avg_over_time(kubecost_node_is_spot{%s}[%s:%dm])`, env.GetPromClusterFilter(), durStr, minsPerResolution)
queryLabels := fmt.Sprintf(`count_over_time(kube_node_labels{%s}[%s:%dm])`, env.GetPromClusterFilter(), durStr, minsPerResolution)
// Return errors if these fail
resChNodeCPUHourlyCost := requiredCtx.QueryAtTime(queryNodeCPUHourlyCost, t)
resChNodeCPUCoresCapacity := requiredCtx.QueryAtTime(queryNodeCPUCoresCapacity, t)
resChNodeCPUCoresAllocatable := requiredCtx.QueryAtTime(queryNodeCPUCoresAllocatable, t)
resChNodeRAMHourlyCost := requiredCtx.QueryAtTime(queryNodeRAMHourlyCost, t)
resChNodeRAMBytesCapacity := requiredCtx.QueryAtTime(queryNodeRAMBytesCapacity, t)
resChNodeRAMBytesAllocatable := requiredCtx.QueryAtTime(queryNodeRAMBytesAllocatable, t)
resChNodeGPUCount := requiredCtx.QueryAtTime(queryNodeGPUCount, t)
resChNodeGPUHourlyCost := requiredCtx.QueryAtTime(queryNodeGPUHourlyCost, t)
resChActiveMins := requiredCtx.QueryAtTime(queryActiveMins, t)
resChIsSpot := requiredCtx.QueryAtTime(queryIsSpot, t)
// Do not return errors if these fail, but log warnings
resChNodeCPUModeTotal := optionalCtx.QueryAtTime(queryNodeCPUModeTotal, t)
resChNodeRAMSystemPct := optionalCtx.QueryAtTime(queryNodeRAMSystemPct, t)
resChNodeRAMUserPct := optionalCtx.QueryAtTime(queryNodeRAMUserPct, t)
resChLabels := optionalCtx.QueryAtTime(queryLabels, t)
resNodeCPUHourlyCost, _ := resChNodeCPUHourlyCost.Await()
resNodeCPUCoresCapacity, _ := resChNodeCPUCoresCapacity.Await()
resNodeCPUCoresAllocatable, _ := resChNodeCPUCoresAllocatable.Await()
resNodeGPUCount, _ := resChNodeGPUCount.Await()
resNodeGPUHourlyCost, _ := resChNodeGPUHourlyCost.Await()
resNodeRAMHourlyCost, _ := resChNodeRAMHourlyCost.Await()
resNodeRAMBytesCapacity, _ := resChNodeRAMBytesCapacity.Await()
resNodeRAMBytesAllocatable, _ := resChNodeRAMBytesAllocatable.Await()
resIsSpot, _ := resChIsSpot.Await()
resNodeCPUModeTotal, _ := resChNodeCPUModeTotal.Await()
resNodeRAMSystemPct, _ := resChNodeRAMSystemPct.Await()
resNodeRAMUserPct, _ := resChNodeRAMUserPct.Await()
resActiveMins, _ := resChActiveMins.Await()
resLabels, _ := resChLabels.Await()
if optionalCtx.HasErrors() {
for _, err := range optionalCtx.Errors() {
log.Warnf("ClusterNodes: %s", err)
}
}
if requiredCtx.HasErrors() {
for _, err := range requiredCtx.Errors() {
log.Errorf("ClusterNodes: %s", err)
}
return nil, requiredCtx.ErrorCollection()
}
activeDataMap := buildActiveDataMap(resActiveMins, resolution, opencost.NewClosedWindow(start, end))
gpuCountMap := buildGPUCountMap(resNodeGPUCount)
preemptibleMap := buildPreemptibleMap(resIsSpot)
cpuCostMap, clusterAndNameToType1 := buildCPUCostMap(resNodeCPUHourlyCost, cp, preemptibleMap)
ramCostMap, clusterAndNameToType2 := buildRAMCostMap(resNodeRAMHourlyCost, cp, preemptibleMap)
gpuCostMap, clusterAndNameToType3 := buildGPUCostMap(resNodeGPUHourlyCost, gpuCountMap, cp, preemptibleMap)
clusterAndNameToTypeIntermediate := mergeTypeMaps(clusterAndNameToType1, clusterAndNameToType2)
clusterAndNameToType := mergeTypeMaps(clusterAndNameToTypeIntermediate, clusterAndNameToType3)
cpuCoresCapacityMap := buildCPUCoresMap(resNodeCPUCoresCapacity)
ramBytesCapacityMap := buildRAMBytesMap(resNodeRAMBytesCapacity)
cpuCoresAllocatableMap := buildCPUCoresMap(resNodeCPUCoresAllocatable)
ramBytesAllocatableMap := buildRAMBytesMap(resNodeRAMBytesAllocatable)
overheadMap := buildOverheadMap(ramBytesCapacityMap, ramBytesAllocatableMap, cpuCoresCapacityMap, cpuCoresAllocatableMap)
ramUserPctMap := buildRAMUserPctMap(resNodeRAMUserPct)
ramSystemPctMap := buildRAMSystemPctMap(resNodeRAMSystemPct)
cpuBreakdownMap := buildCPUBreakdownMap(resNodeCPUModeTotal)
labelsMap := buildLabelsMap(resLabels)
costTimesMinuteAndCount(activeDataMap, cpuCostMap, cpuCoresCapacityMap)
costTimesMinuteAndCount(activeDataMap, ramCostMap, ramBytesCapacityMap)
costTimesMinute(activeDataMap, gpuCostMap) // there's no need to do a weird "nodeIdentifierNoProviderID" type match since gpuCounts have a providerID
nodeMap := buildNodeMap(
cpuCostMap, ramCostMap, gpuCostMap, gpuCountMap,
cpuCoresCapacityMap, ramBytesCapacityMap, ramUserPctMap,
ramSystemPctMap,
cpuBreakdownMap,
activeDataMap,
preemptibleMap,
labelsMap,
clusterAndNameToType,
resolution,
overheadMap,
)
c, err := cp.GetConfig()
if err != nil {
return nil, err
}
discount, err := ParsePercentString(c.Discount)
if err != nil {
return nil, err
}
negotiatedDiscount, err := ParsePercentString(c.NegotiatedDiscount)
if err != nil {
return nil, err
}
for _, node := range nodeMap {
// TODO take GKE Reserved Instances into account
node.Discount = cp.CombinedDiscountForNode(node.NodeType, node.Preemptible, discount, negotiatedDiscount)
// Apply all remaining resources to Idle
node.CPUBreakdown.Idle = 1.0 - (node.CPUBreakdown.System + node.CPUBreakdown.Other + node.CPUBreakdown.User)
node.RAMBreakdown.Idle = 1.0 - (node.RAMBreakdown.System + node.RAMBreakdown.Other + node.RAMBreakdown.User)
}
return nodeMap, nil
}
type LoadBalancerIdentifier struct {
Cluster string
Namespace string
Name string
}
type LoadBalancer struct {
Cluster string
Namespace string
Name string
ProviderID string
Cost float64
Start time.Time
End time.Time
Minutes float64
Private bool
Ip string
}
func ClusterLoadBalancers(client prometheus.Client, start, end time.Time) (map[LoadBalancerIdentifier]*LoadBalancer, error) {
// Start from the time "end", querying backwards
t := end
// minsPerResolution determines accuracy and resource use for the following
// queries. Smaller values (higher resolution) result in better accuracy,
// but more expensive queries, and vice-a-versa.
resolution := env.GetETLResolution()
//Ensuring if ETL_RESOLUTION_SECONDS is less than 60s default it to 1m
var minsPerResolution int
if minsPerResolution = int(resolution.Minutes()); int(resolution.Minutes()) == 0 {
minsPerResolution = 1
log.DedupedWarningf(3, "ClusterLoadBalancers(): Configured ETL resolution (%d seconds) is below the 60 seconds threshold. Overriding with 1 minute.", int(resolution.Seconds()))
}
// Query for the duration between start and end
durStr := timeutil.DurationString(end.Sub(start))
if durStr == "" {
return nil, fmt.Errorf("illegal duration value for %s", opencost.NewClosedWindow(start, end))
}
ctx := prom.NewNamedContext(client, prom.ClusterContextName)
queryLBCost := fmt.Sprintf(`avg(avg_over_time(kubecost_load_balancer_cost{%s}[%s])) by (namespace, service_name, %s, ingress_ip)`, env.GetPromClusterFilter(), durStr, env.GetPromClusterLabel())
queryActiveMins := fmt.Sprintf(`avg(kubecost_load_balancer_cost{%s}) by (namespace, service_name, %s, ingress_ip)[%s:%dm]`, env.GetPromClusterFilter(), env.GetPromClusterLabel(), durStr, minsPerResolution)
resChLBCost := ctx.QueryAtTime(queryLBCost, t)
resChActiveMins := ctx.QueryAtTime(queryActiveMins, t)
resLBCost, _ := resChLBCost.Await()
resActiveMins, _ := resChActiveMins.Await()
if ctx.HasErrors() {
return nil, ctx.ErrorCollection()
}
loadBalancerMap := make(map[LoadBalancerIdentifier]*LoadBalancer, len(resActiveMins))
for _, result := range resActiveMins {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
namespace, err := result.GetString("namespace")
if err != nil {
log.Warnf("ClusterLoadBalancers: LB cost data missing namespace")
continue
}
name, err := result.GetString("service_name")
if err != nil {
log.Warnf("ClusterLoadBalancers: LB cost data missing service_name")
continue
}
providerID, err := result.GetString("ingress_ip")
if err != nil {
log.DedupedWarningf(5, "ClusterLoadBalancers: LB cost data missing ingress_ip")
providerID = ""
}
key := LoadBalancerIdentifier{
Cluster: cluster,
Namespace: namespace,
Name: name,
}
// Skip if there are no data
if len(result.Values) == 0 {
continue
}
// Add load balancer to the set of load balancers
if _, ok := loadBalancerMap[key]; !ok {
loadBalancerMap[key] = &LoadBalancer{
Cluster: cluster,
Namespace: namespace,
Name: fmt.Sprintf("%s/%s", namespace, name), // TODO:ETL this is kept for backwards-compatibility, but not good
ProviderID: provider.ParseLBID(providerID),
}
}
// Append start, end, and minutes. This should come before all other data.
s := time.Unix(int64(result.Values[0].Timestamp), 0)
e := time.Unix(int64(result.Values[len(result.Values)-1].Timestamp), 0)
loadBalancerMap[key].Start = s
loadBalancerMap[key].End = e
loadBalancerMap[key].Minutes = e.Sub(s).Minutes()
// Fill in Provider ID if it is available and missing in the loadBalancerMap
// Prevents there from being a duplicate LoadBalancers on the same day
if providerID != "" && loadBalancerMap[key].ProviderID == "" {
loadBalancerMap[key].ProviderID = providerID
}
}
for _, result := range resLBCost {
cluster, err := result.GetString(env.GetPromClusterLabel())
if err != nil {
cluster = env.GetClusterID()
}
namespace, err := result.GetString("namespace")
if err != nil {
log.Warnf("ClusterLoadBalancers: LB cost data missing namespace")
continue
}
name, err := result.GetString("service_name")
if err != nil {
log.Warnf("ClusterLoadBalancers: LB cost data missing service_name")
continue
}
providerID, err := result.GetString("ingress_ip")
if err != nil {
log.DedupedWarningf(5, "ClusterLoadBalancers: LB cost data missing ingress_ip")
// only update asset cost when an actual IP was returned
continue
}
key := LoadBalancerIdentifier{
Cluster: cluster,
Namespace: namespace,
Name: name,
}
// Apply cost as price-per-hour * hours
if lb, ok := loadBalancerMap[key]; ok {
lbPricePerHr := result.Values[0].Value
// interpolate any missing data
resultMins := lb.Minutes
if resultMins > 0 {
scaleFactor := (resultMins + resolution.Minutes()) / resultMins
hrs := (lb.Minutes * scaleFactor) / 60.0
lb.Cost += lbPricePerHr * hrs
} else {
log.DedupedWarningf(20, "ClusterLoadBalancers: found zero minutes for key: %v", key)
}
if lb.Ip != "" && lb.Ip != providerID {
log.DedupedWarningf(5, "ClusterLoadBalancers: multiple IPs per load balancer not supported, using most recent IP")
}
lb.Ip = providerID
lb.Private = privateIPCheck(providerID)
} else {
log.DedupedWarningf(20, "ClusterLoadBalancers: found minutes for key that does not exist: %v", key)
}
}
return loadBalancerMap, nil
}
// Check if an ip is private.
func privateIPCheck(ip string) bool {
ipAddress := net.ParseIP(ip)
return ipAddress.IsPrivate()
}
// ComputeClusterCosts gives the cumulative and monthly-rate cluster costs over a window of time for all clusters.
func (a *Accesses) ComputeClusterCosts(client prometheus.Client, provider models.Provider, window, offset time.Duration, withBreakdown bool) (map[string]*ClusterCosts, error) {
if window < 10*time.Minute {
return nil, fmt.Errorf("minimum window of 10m required; got %s", window)
}
// Compute number of minutes in the full interval, for use interpolating missed scrapes or scaling missing data
start, end := timeutil.ParseTimeRange(window, offset)
mins := end.Sub(start).Minutes()
// minsPerResolution determines accuracy and resource use for the following
// queries. Smaller values (higher resolution) result in better accuracy,
// but more expensive queries, and vice-a-versa.
resolution := env.GetETLResolution()
//Ensuring if ETL_RESOLUTION_SECONDS is less than 60s default it to 1m
var minsPerResolution int
if minsPerResolution = int(resolution.Minutes()); int(resolution.Minutes()) < 1 {
minsPerResolution = 1
log.DedupedWarningf(3, "ComputeClusterCosts(): Configured ETL resolution (%d seconds) is below the 60 seconds threshold. Overriding with 1 minute.", int(resolution.Seconds()))
}
windowStr := timeutil.DurationString(window)
// hourlyToCumulative is a scaling factor that, when multiplied by an hourly
// value, converts it to a cumulative value; i.e.
// [$/hr] * [min/res]*[hr/min] = [$/res]
hourlyToCumulative := float64(minsPerResolution) * (1.0 / 60.0)
const fmtQueryDataCount = `
count_over_time(sum(kube_node_status_capacity_cpu_cores{%s}) by (%s)[%s:%dm]%s) * %d
`
const fmtQueryTotalGPU = `
sum(
sum_over_time(node_gpu_hourly_cost{%s}[%s:%dm]%s) * %f
) by (%s)
`
const fmtQueryTotalCPU = `
sum(
sum_over_time(avg(kube_node_status_capacity_cpu_cores{%s}) by (node, %s)[%s:%dm]%s) *
avg(avg_over_time(node_cpu_hourly_cost{%s}[%s:%dm]%s)) by (node, %s) * %f
) by (%s)
`
const fmtQueryTotalRAM = `
sum(
sum_over_time(avg(kube_node_status_capacity_memory_bytes{%s}) by (node, %s)[%s:%dm]%s) / 1024 / 1024 / 1024 *
avg(avg_over_time(node_ram_hourly_cost{%s}[%s:%dm]%s)) by (node, %s) * %f
) by (%s)
`
const fmtQueryTotalStorage = `
sum(
sum_over_time(avg(kube_persistentvolume_capacity_bytes{%s}) by (persistentvolume, %s)[%s:%dm]%s) / 1024 / 1024 / 1024 *
avg(avg_over_time(pv_hourly_cost{%s}[%s:%dm]%s)) by (persistentvolume, %s) * %f
) by (%s)
`
const fmtQueryCPUModePct = `
sum(rate(node_cpu_seconds_total{%s}[%s]%s)) by (%s, mode) / ignoring(mode)
group_left sum(rate(node_cpu_seconds_total{%s}[%s]%s)) by (%s)
`
const fmtQueryRAMSystemPct = `
sum(sum_over_time(container_memory_usage_bytes{container_name!="",namespace="kube-system", %s}[%s:%dm]%s)) by (%s)
/ sum(sum_over_time(kube_node_status_capacity_memory_bytes{%s}[%s:%dm]%s)) by (%s)
`
const fmtQueryRAMUserPct = `
sum(sum_over_time(kubecost_cluster_memory_working_set_bytes{%s}[%s:%dm]%s)) by (%s)
/ sum(sum_over_time(kube_node_status_capacity_memory_bytes{%s}[%s:%dm]%s)) by (%s)
`
// TODO niko/clustercost metric "kubelet_volume_stats_used_bytes" was deprecated in 1.12, then seems to have come back in 1.17
// const fmtQueryPVStorageUsePct = `(sum(kube_persistentvolumeclaim_info) by (persistentvolumeclaim, storageclass,namespace) + on (persistentvolumeclaim,namespace)
// group_right(storageclass) sum(kubelet_volume_stats_used_bytes) by (persistentvolumeclaim,namespace))`
queryUsedLocalStorage := provider.GetLocalStorageQuery(window, offset, false, true)
queryTotalLocalStorage := provider.GetLocalStorageQuery(window, offset, false, false)
if queryTotalLocalStorage != "" {
queryTotalLocalStorage = fmt.Sprintf(" + %s", queryTotalLocalStorage)
}
fmtOffset := timeutil.DurationToPromOffsetString(offset)
queryDataCount := fmt.Sprintf(fmtQueryDataCount, env.GetPromClusterFilter(), env.GetPromClusterLabel(), windowStr, minsPerResolution, fmtOffset, minsPerResolution)
queryTotalGPU := fmt.Sprintf(fmtQueryTotalGPU, env.GetPromClusterFilter(), windowStr, minsPerResolution, fmtOffset, hourlyToCumulative, env.GetPromClusterLabel())
queryTotalCPU := fmt.Sprintf(fmtQueryTotalCPU, env.GetPromClusterFilter(), env.GetPromClusterLabel(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterFilter(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterLabel(), hourlyToCumulative, env.GetPromClusterLabel())
queryTotalRAM := fmt.Sprintf(fmtQueryTotalRAM, env.GetPromClusterFilter(), env.GetPromClusterLabel(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterFilter(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterLabel(), hourlyToCumulative, env.GetPromClusterLabel())
queryTotalStorage := fmt.Sprintf(fmtQueryTotalStorage, env.GetPromClusterFilter(), env.GetPromClusterLabel(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterFilter(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterLabel(), hourlyToCumulative, env.GetPromClusterLabel())
ctx := prom.NewNamedContext(client, prom.ClusterContextName)
resChs := ctx.QueryAll(
queryDataCount,
queryTotalGPU,
queryTotalCPU,
queryTotalRAM,
queryTotalStorage,
)
// Only submit the local storage query if it is valid. Otherwise Prometheus
// will return errors. Always append something to resChs, regardless, to
// maintain indexing.
if queryTotalLocalStorage != "" {
resChs = append(resChs, ctx.Query(queryTotalLocalStorage))
} else {
resChs = append(resChs, nil)
}
if withBreakdown {
queryCPUModePct := fmt.Sprintf(fmtQueryCPUModePct, env.GetPromClusterFilter(), windowStr, fmtOffset, env.GetPromClusterLabel(), env.GetPromClusterFilter(), windowStr, fmtOffset, env.GetPromClusterLabel())
queryRAMSystemPct := fmt.Sprintf(fmtQueryRAMSystemPct, env.GetPromClusterFilter(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterLabel(), env.GetPromClusterFilter(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterLabel())
queryRAMUserPct := fmt.Sprintf(fmtQueryRAMUserPct, env.GetPromClusterFilter(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterLabel(), env.GetPromClusterFilter(), windowStr, minsPerResolution, fmtOffset, env.GetPromClusterLabel())
bdResChs := ctx.QueryAll(
queryCPUModePct,
queryRAMSystemPct,