azurestorageintegration.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. providerID, _ := AzureSetProviderID(abv)
  27. // Create CloudCost
  28. // Using the NetCost as a 'placeholder' for Invoiced and Amortized Net costs now,
  29. // until we can revisit and spend the time to do the calculations correctly
  30. cc := &kubecost.CloudCost{
  31. Properties: &kubecost.CloudCostProperties{
  32. ProviderID: providerID,
  33. Provider: kubecost.AzureProvider,
  34. AccountID: abv.SubscriptionID,
  35. InvoiceEntityID: abv.InvoiceEntityID,
  36. Service: abv.Service,
  37. Category: SelectAzureCategory(abv.MeterCategory),
  38. Labels: abv.Tags,
  39. },
  40. Window: window,
  41. AmortizedNetCost: kubecost.CostMetric{
  42. Cost: abv.NetCost,
  43. KubernetesPercent: k8sPtc,
  44. },
  45. InvoicedCost: kubecost.CostMetric{
  46. Cost: abv.NetCost,
  47. KubernetesPercent: k8sPtc,
  48. },
  49. ListCost: kubecost.CostMetric{
  50. Cost: abv.Cost,
  51. KubernetesPercent: k8sPtc,
  52. },
  53. NetCost: kubecost.CostMetric{
  54. Cost: abv.NetCost,
  55. KubernetesPercent: k8sPtc,
  56. },
  57. // NOTE: on Azure, there is no "AmortizedCost" per se, so we use
  58. // AmortizedNetCost, or NetCost, instead.
  59. AmortizedCost: kubecost.CostMetric{
  60. Cost: abv.NetCost,
  61. KubernetesPercent: k8sPtc,
  62. },
  63. }
  64. // Check if Item
  65. if abv.IsCompute(cc.Properties.Category) {
  66. // TODO: Will need to split VMSS for other features
  67. ccsr.LoadCloudCost(cc)
  68. }
  69. return nil
  70. })
  71. if err != nil {
  72. asi.ConnectionStatus = status
  73. return nil, err
  74. }
  75. return ccsr, nil
  76. }
  77. // Check for the presence of k8s labels
  78. func AzureIsK8s(labels map[string]string) bool {
  79. for key := range labels {
  80. if strings.HasPrefix(key, "aks-managed-") {
  81. return true
  82. }
  83. if strings.HasPrefix(key, "kubernetes.io-created-") {
  84. return true
  85. }
  86. if strings.HasPrefix(key, "k8s-azure-created-") {
  87. return true
  88. }
  89. }
  90. return false
  91. }