azurestorageintegration.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package azure
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. "github.com/opencost/opencost/pkg/kubecost"
  7. "github.com/opencost/opencost/pkg/util/timeutil"
  8. )
  9. type AzureStorageIntegration struct {
  10. AzureStorageBillingParser
  11. ConnectionStatus cloud.ConnectionStatus
  12. }
  13. func (asi *AzureStorageIntegration) GetCloudCost(start, end time.Time) (*kubecost.CloudCostSetRange, error) {
  14. ccsr, err := kubecost.NewCloudCostSetRange(start, end, timeutil.Day, asi.Key())
  15. if err != nil {
  16. return nil, err
  17. }
  18. status, err := asi.ParseBillingData(start, end, func(abv *BillingRowValues) error {
  19. s := abv.Date
  20. e := abv.Date.Add(timeutil.Day)
  21. window := kubecost.NewWindow(&s, &e)
  22. k8sPtc := 0.0
  23. if AzureIsK8s(abv.Tags) {
  24. k8sPtc = 1.0
  25. }
  26. // Create CloudCost
  27. // Using the NetCost as a 'placeholder' for Invoiced and Amortized Net costs now,
  28. // until we can revisit and spend the time to do the calculations correctly
  29. cc := &kubecost.CloudCost{
  30. Properties: &kubecost.CloudCostProperties{
  31. ProviderID: AzureSetProviderID(abv),
  32. Provider: kubecost.AzureProvider,
  33. AccountID: abv.SubscriptionID,
  34. InvoiceEntityID: abv.InvoiceEntityID,
  35. Service: abv.Service,
  36. Category: SelectAzureCategory(abv.MeterCategory),
  37. Labels: abv.Tags,
  38. },
  39. Window: window,
  40. AmortizedNetCost: kubecost.CostMetric{
  41. Cost: abv.NetCost,
  42. KubernetesPercent: k8sPtc,
  43. },
  44. InvoicedCost: kubecost.CostMetric{
  45. Cost: abv.NetCost,
  46. KubernetesPercent: k8sPtc,
  47. },
  48. ListCost: kubecost.CostMetric{
  49. Cost: abv.Cost,
  50. KubernetesPercent: k8sPtc,
  51. },
  52. NetCost: kubecost.CostMetric{
  53. Cost: abv.NetCost,
  54. KubernetesPercent: k8sPtc,
  55. },
  56. // NOTE: on Azure, there is no "AmortizedCost" per se, so we use
  57. // AmortizedNetCost, or NetCost, instead.
  58. AmortizedCost: kubecost.CostMetric{
  59. Cost: abv.NetCost,
  60. KubernetesPercent: k8sPtc,
  61. },
  62. }
  63. // Check if Item
  64. if abv.IsCompute(cc.Properties.Category) {
  65. // TODO: Will need to split VMSS for other features
  66. ccsr.LoadCloudCost(cc)
  67. }
  68. return nil
  69. })
  70. if err != nil {
  71. asi.ConnectionStatus = status
  72. return nil, err
  73. }
  74. return ccsr, nil
  75. }
  76. // Check for the presence of k8s labels
  77. func AzureIsK8s(labels map[string]string) bool {
  78. for key := range labels {
  79. if strings.HasPrefix(key, "aks-managed-") {
  80. return true
  81. }
  82. if strings.HasPrefix(key, "kubernetes.io-created-") {
  83. return true
  84. }
  85. if strings.HasPrefix(key, "k8s-azure-created-") {
  86. return true
  87. }
  88. }
  89. return false
  90. }