persistentvolumeclaim.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package kubemodel
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/log"
  5. "github.com/opencost/opencost/core/pkg/model/kubemodel"
  6. "github.com/opencost/opencost/core/pkg/source"
  7. )
  8. func (km *KubeModel) computePersistentVolumeClaims(kms *kubemodel.KubeModelSet, start, end time.Time) error {
  9. grp := source.NewQueryGroup()
  10. metrics := km.ds.Metrics()
  11. pvcInfoResultFuture := source.WithGroup(grp, metrics.QueryKMPVCInfo(start, end))
  12. pvcUptimeResultFuture := source.WithGroup(grp, metrics.QueryPVCUptime(start, end))
  13. pvcBytesRequestedResultFuture := source.WithGroup(grp, metrics.QueryPVCBytesRequested(start, end))
  14. pvcBytesUsedAvgResultFuture := source.WithGroup(grp, metrics.QueryPVCBytesUsedAverage(start, end))
  15. pvcBytesUsedMaxResultFuture := source.WithGroup(grp, metrics.QueryPVCBytesUsedMax(start, end))
  16. pvcMap := make(map[string]*kubemodel.PersistentVolumeClaim)
  17. pvcInfoResult, _ := pvcInfoResultFuture.Await()
  18. for _, res := range pvcInfoResult {
  19. pvcMap[res.UID] = &kubemodel.PersistentVolumeClaim{
  20. UID: res.UID,
  21. Name: res.PersistentVolumeClaim,
  22. NamespaceUID: res.NamespaceUID,
  23. PersistentVolumeUID: res.PVUID,
  24. StorageClass: res.StorageClass,
  25. }
  26. }
  27. pvcUptimeResult, _ := pvcUptimeResultFuture.Await()
  28. for _, res := range pvcUptimeResult {
  29. pvc, ok := pvcMap[res.UID]
  30. if !ok {
  31. log.Warnf("persistent volume claim with UID '%s' has not been initialized to add uptime", res.UID)
  32. continue
  33. }
  34. s, e := res.GetStartEnd(start, end, km.ds.Resolution())
  35. pvc.Start = s
  36. pvc.End = e
  37. }
  38. pvcBytesRequestedResult, _ := pvcBytesRequestedResultFuture.Await()
  39. for _, res := range pvcBytesRequestedResult {
  40. pvc, ok := pvcMap[res.UID]
  41. if !ok {
  42. log.Warnf("persistent volume claim with UID '%s' has not been initialized to add requested bytes", res.UID)
  43. continue
  44. }
  45. if len(res.Data) > 0 {
  46. pvc.RequestedBytes = res.Data[0].Value
  47. }
  48. }
  49. pvcBytesUsedAvgResult, _ := pvcBytesUsedAvgResultFuture.Await()
  50. for _, res := range pvcBytesUsedAvgResult {
  51. pvc, ok := pvcMap[res.UID]
  52. if !ok {
  53. log.Warnf("persistent volume claim with UID '%s' has not been initialized to add bytes used average", res.UID)
  54. continue
  55. }
  56. pvc.UsageBytesAvg = res.Value
  57. }
  58. pvcBytesUsedMaxResult, _ := pvcBytesUsedMaxResultFuture.Await()
  59. for _, res := range pvcBytesUsedMaxResult {
  60. pvc, ok := pvcMap[res.UID]
  61. if !ok {
  62. log.Warnf("persistent volume claim with UID '%s' has not been initialized to add bytes used max", res.UID)
  63. continue
  64. }
  65. pvc.UsageBytesMax = res.Value
  66. }
  67. for _, pvc := range pvcMap {
  68. err := kms.RegisterPVC(pvc)
  69. if err != nil {
  70. log.Warnf("Failed to register persistent volume claim: %s", err.Error())
  71. }
  72. }
  73. return nil
  74. }