azurestorageintegration.go 2.4 KB

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