boaquerier.go 4.9 KB

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