boaquerier.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package alibaba
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. cloudconfig "github.com/opencost/opencost/pkg/cloud/config"
  7. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  8. "github.com/aliyun/alibaba-cloud-sdk-go/services/bssopenapi"
  9. "github.com/opencost/opencost/pkg/kubecost"
  10. "github.com/opencost/opencost/pkg/log"
  11. )
  12. const (
  13. boaIsNode = "i-" // isNode if prefix of instance_id is i-
  14. boaIsDisk = "d-" // isDisk if prefix is disk is d-
  15. boaIsNetwork = "piece" //usage unit of network resource in Alibaba is Piece
  16. )
  17. type BoaQuerier struct {
  18. BOAConfiguration
  19. ConnectionStatus cloud.ConnectionStatus
  20. }
  21. func (bq *BoaQuerier) GetStatus() cloud.ConnectionStatus {
  22. return bq.ConnectionStatus
  23. }
  24. func (bq *BoaQuerier) Equals(config cloudconfig.Config) bool {
  25. thatConfig, ok := config.(*BoaQuerier)
  26. if !ok {
  27. return false
  28. }
  29. return bq.BOAConfiguration.Equals(&thatConfig.BOAConfiguration)
  30. }
  31. // QueryInstanceBill performs the request to the BSS client and get the response for the current page number
  32. func (bq *BoaQuerier) QueryInstanceBill(client *bssopenapi.Client, isBillingItem bool, invocationScheme, granularity, billingCycle, billingDate string, pageNum int) (*bssopenapi.QueryInstanceBillResponse, error) {
  33. log.Debugf("QueryInstanceBill: query for BSS Open API for billing date: %s with pageNum: %d ", billingDate, pageNum)
  34. request := bssopenapi.CreateQueryInstanceBillRequest()
  35. request.Scheme = invocationScheme
  36. request.BillingCycle = billingCycle
  37. request.IsBillingItem = requests.NewBoolean(true)
  38. request.Granularity = granularity
  39. request.BillingDate = billingDate
  40. request.PageNum = requests.NewInteger(pageNum)
  41. response, err := client.QueryInstanceBill(request)
  42. if err != nil {
  43. return nil, fmt.Errorf("QueryInstanceBill: Failed to hit the BSS Open API with error for page num %d: %v", pageNum, err)
  44. }
  45. log.Debugf("QueryInstanceBill: Total Number of total items for billing Date: %s pageNum: %d is %d", billingDate, pageNum, response.Data.TotalCount)
  46. return response, nil
  47. }
  48. // QueryBoaPaginated Calls the API in a paginated fashion. There's no paramter in API that can distinguish if it hasMorePages
  49. // hence the logic of processedItem <= TotalItem.
  50. func (bq *BoaQuerier) QueryBoaPaginated(client *bssopenapi.Client, isBillingItem bool, invocationScheme, granularity, billingCycle, billingDate string, fn func(*bssopenapi.QueryInstanceBillResponse) bool) error {
  51. pageNum := 1
  52. processedItem := 0 // setting default here to hit the API for the first time
  53. totalItem := 1
  54. for processedItem < totalItem {
  55. log.Debugf("QueryBoaPaginated: query for BSS Open API for billing date: %s with pageNum: %d", billingDate, pageNum)
  56. response, err := bq.QueryInstanceBill(client, isBillingItem, invocationScheme, granularity, billingCycle, billingDate, pageNum)
  57. if err != nil {
  58. return fmt.Errorf("QueryBoaPaginated for billing cycle : %s, billing date: %s, page num %d: %v", billingCycle, billingDate, pageNum, err)
  59. }
  60. fn(response)
  61. totalItem = response.Data.TotalCount
  62. processedItem += response.Data.PageSize
  63. pageNum += 1
  64. }
  65. return nil
  66. }
  67. // GetBoaQueryInstanceBillFunc gives the item to the handler function in boaIntegration.go to process
  68. // computeItem, topNItem and aggregatedItem
  69. func GetBoaQueryInstanceBillFunc(fn func(bssopenapi.Item) error, billingDate string) func(output *bssopenapi.QueryInstanceBillResponse) bool {
  70. processBOAItems := func(output *bssopenapi.QueryInstanceBillResponse) bool {
  71. // This could be connection error were unable to fetch response output from Client
  72. if output == nil {
  73. log.Errorf("BoaQuerier: No Response from the ALibaba BSS Open API client for billing Date: %s", billingDate)
  74. return false
  75. }
  76. // These infer that the rest call was successful but the Cloud Usage resource for those days were 0
  77. if output.Data.TotalCount == 0 {
  78. log.Warnf("BoaQuerier: Total Item Count is 0 for billing Date: %s ", billingDate)
  79. return false
  80. }
  81. for _, item := range output.Data.Items.Item {
  82. fn(item)
  83. }
  84. return true
  85. }
  86. return processBOAItems
  87. }
  88. // SelectAlibabaCategory processes the Alibaba service to associated Kubecost category
  89. func SelectAlibabaCategory(item bssopenapi.Item) string {
  90. if (item != bssopenapi.Item{}) {
  91. // Provider ID has prefix "i-" for node in Alibaba
  92. if strings.HasPrefix(item.InstanceID, boaIsNode) {
  93. return kubecost.ComputeCategory
  94. }
  95. // Provider ID for disk start with "d-" for storage type in Alibaba
  96. if strings.HasPrefix(item.InstanceID, boaIsDisk) {
  97. return kubecost.StorageCategory
  98. }
  99. // Network has the highest priority and is based on the usage type of "piece" in Alibaba
  100. if item.UsageUnit == boaIsNetwork {
  101. return kubecost.NetworkCategory
  102. }
  103. }
  104. // Alibaba CUR integration report has service lower case mostly unlike AWS
  105. // TO-DO: Can investigate further product codes but bare minimal differentiation for start
  106. switch strings.ToLower(item.ProductCode) {
  107. case "slb", "eip", "nis", "gtm":
  108. return kubecost.NetworkCategory
  109. case "ecs", "eds", "sas":
  110. return kubecost.ComputeCategory
  111. case "ack":
  112. return kubecost.ManagementCategory
  113. case "ebs", "oss", "scu":
  114. return kubecost.StorageCategory
  115. default:
  116. return kubecost.OtherCategory
  117. }
  118. }