cloudcostprops.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package opencost
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/opencost/opencost/core/pkg/log"
  6. )
  7. type CloudCostProperty string
  8. // IsLabel returns true if the allocation property has a label prefix
  9. func (apt *CloudCostProperty) IsLabel() bool {
  10. return strings.HasPrefix(string(*apt), "label:")
  11. }
  12. // GetLabel returns the label string associated with the label property if it exists.
  13. // Otherwise, empty string is returned.
  14. func (apt *CloudCostProperty) GetLabel() string {
  15. if apt.IsLabel() {
  16. return strings.TrimSpace(strings.TrimPrefix(string(*apt), "label:"))
  17. }
  18. return ""
  19. }
  20. const (
  21. CloudCostInvoiceEntityIDProp string = "invoiceEntityID"
  22. CloudCostAccountIDProp string = "accountID"
  23. CloudCostProviderProp string = "provider"
  24. CloudCostProviderIDProp string = "providerID"
  25. CloudCostCategoryProp string = "category"
  26. CloudCostServiceProp string = "service"
  27. CloudCostLabelProp string = "label"
  28. )
  29. func ParseCloudProperties(props []string) ([]CloudCostProperty, error) {
  30. properties := []CloudCostProperty{}
  31. added := make(map[CloudCostProperty]struct{})
  32. for _, prop := range props {
  33. property, err := ParseCloudCostProperty(prop)
  34. if err != nil {
  35. return nil, fmt.Errorf("Failed to parse property: %w", err)
  36. }
  37. if _, ok := added[property]; !ok {
  38. added[property] = struct{}{}
  39. properties = append(properties, property)
  40. }
  41. }
  42. return properties, nil
  43. }
  44. func ParseCloudCostProperty(text string) (CloudCostProperty, error) {
  45. switch strings.TrimSpace(strings.ToLower(text)) {
  46. case "invoiceentityid":
  47. return CloudCostProperty(CloudCostInvoiceEntityIDProp), nil
  48. case "accountid":
  49. return CloudCostProperty(CloudCostAccountIDProp), nil
  50. case "provider":
  51. return CloudCostProperty(CloudCostProviderProp), nil
  52. case "providerid":
  53. return CloudCostProperty(CloudCostProviderIDProp), nil
  54. case "category":
  55. return CloudCostProperty(CloudCostCategoryProp), nil
  56. case "service":
  57. return CloudCostProperty(CloudCostServiceProp), nil
  58. }
  59. if strings.HasPrefix(text, "label:") {
  60. label := strings.TrimSpace(strings.TrimPrefix(text, "label:"))
  61. return CloudCostProperty(fmt.Sprintf("label:%s", label)), nil
  62. }
  63. return "", fmt.Errorf("invalid cloud cost property: %s", text)
  64. }
  65. const (
  66. // CloudCostClusterManagementCategory describes CloudCost representing Hosted Kubernetes Fees
  67. CloudCostClusterManagementCategory string = "Cluster Management"
  68. // CloudCostDiskCategory describes CloudCost representing Disk usage
  69. CloudCostDiskCategory string = "Disk"
  70. // CloudCostLoadBalancerCategory describes CloudCost representing Load Balancer usage
  71. CloudCostLoadBalancerCategory string = "Load Balancer"
  72. // CloudCostNetworkCategory describes CloudCost representing Network usage
  73. CloudCostNetworkCategory string = "Network"
  74. // CloudCostVirtualMachineCategory describes CloudCost representing VM usage
  75. CloudCostVirtualMachineCategory string = "Virtual Machine"
  76. // CloudCostOtherCategory describes CloudCost that do not belong to a defined category
  77. CloudCostOtherCategory string = "Other"
  78. )
  79. type CloudCostLabels map[string]string
  80. func (ccl CloudCostLabels) Clone() CloudCostLabels {
  81. result := make(map[string]string, len(ccl))
  82. for k, v := range ccl {
  83. result[k] = v
  84. }
  85. return result
  86. }
  87. func (ccl CloudCostLabels) Equal(that CloudCostLabels) bool {
  88. if len(ccl) != len(that) {
  89. return false
  90. }
  91. // Maps are of equal length, so if all keys are in both maps, we don't
  92. // have to check the keys of the other map.
  93. for k, val := range ccl {
  94. if thatVal, ok := that[k]; !ok || val != thatVal {
  95. return false
  96. }
  97. }
  98. return true
  99. }
  100. // Intersection returns the set of labels that have the same key and value in the receiver and arg
  101. func (ccl CloudCostLabels) Intersection(that CloudCostLabels) CloudCostLabels {
  102. intersection := make(map[string]string)
  103. if len(ccl) == 0 || len(that) == 0 {
  104. return intersection
  105. }
  106. // Pick the smaller of the two label sets
  107. smallerLabels := ccl
  108. largerLabels := that
  109. if len(ccl) > len(that) {
  110. smallerLabels = that
  111. largerLabels = ccl
  112. }
  113. // Loop through the smaller label set
  114. for k, sVal := range smallerLabels {
  115. if lVal, ok := largerLabels[k]; ok && sVal == lVal {
  116. intersection[k] = sVal
  117. }
  118. }
  119. return intersection
  120. }
  121. type CloudCostProperties struct {
  122. ProviderID string `json:"providerID,omitempty"`
  123. Provider string `json:"provider,omitempty"`
  124. AccountID string `json:"accountID,omitempty"`
  125. InvoiceEntityID string `json:"invoiceEntityID,omitempty"`
  126. Service string `json:"service,omitempty"`
  127. Category string `json:"category,omitempty"`
  128. Labels CloudCostLabels `json:"labels,omitempty"`
  129. }
  130. func (ccp *CloudCostProperties) Equal(that *CloudCostProperties) bool {
  131. return ccp.ProviderID == that.ProviderID &&
  132. ccp.Provider == that.Provider &&
  133. ccp.AccountID == that.AccountID &&
  134. ccp.InvoiceEntityID == that.InvoiceEntityID &&
  135. ccp.Service == that.Service &&
  136. ccp.Category == that.Category &&
  137. ccp.Labels.Equal(that.Labels)
  138. }
  139. func (ccp *CloudCostProperties) Clone() *CloudCostProperties {
  140. return &CloudCostProperties{
  141. ProviderID: ccp.ProviderID,
  142. Provider: ccp.Provider,
  143. AccountID: ccp.AccountID,
  144. InvoiceEntityID: ccp.InvoiceEntityID,
  145. Service: ccp.Service,
  146. Category: ccp.Category,
  147. Labels: ccp.Labels.Clone(),
  148. }
  149. }
  150. // Intersection ensure the values of two CloudCostAggregateProperties are maintain only if they are equal
  151. func (ccp *CloudCostProperties) Intersection(that *CloudCostProperties) *CloudCostProperties {
  152. if ccp == nil || that == nil {
  153. return nil
  154. }
  155. if ccp.Equal(that) {
  156. return ccp
  157. }
  158. intersectionCCP := &CloudCostProperties{}
  159. if ccp.Equal(intersectionCCP) || that.Equal(intersectionCCP) {
  160. return intersectionCCP
  161. }
  162. if ccp.Provider == that.Provider {
  163. intersectionCCP.Provider = ccp.Provider
  164. }
  165. if ccp.ProviderID == that.ProviderID {
  166. intersectionCCP.ProviderID = ccp.ProviderID
  167. }
  168. if ccp.AccountID == that.AccountID {
  169. intersectionCCP.AccountID = ccp.AccountID
  170. }
  171. if ccp.InvoiceEntityID == that.InvoiceEntityID {
  172. intersectionCCP.InvoiceEntityID = ccp.InvoiceEntityID
  173. }
  174. if ccp.Service == that.Service {
  175. intersectionCCP.Service = ccp.Service
  176. }
  177. if ccp.Category == that.Category {
  178. intersectionCCP.Category = ccp.Category
  179. }
  180. intersectionCCP.Labels = ccp.Labels.Intersection(that.Labels)
  181. return intersectionCCP
  182. }
  183. var cloudCostDefaultKeyProperties = []string{
  184. CloudCostProviderProp,
  185. CloudCostInvoiceEntityIDProp,
  186. CloudCostAccountIDProp,
  187. CloudCostCategoryProp,
  188. CloudCostServiceProp,
  189. CloudCostProviderIDProp,
  190. }
  191. // GenerateKey takes a list of properties and creates a "/" seperated key based on the values of the requested properties.
  192. // Invalid values are ignored with a warning. A nil input returns the default key, while an empty slice returns the empty string
  193. func (ccp *CloudCostProperties) GenerateKey(props []string) string {
  194. // nil props replaced with default property list
  195. if props == nil {
  196. props = cloudCostDefaultKeyProperties
  197. }
  198. values := make([]string, len(props))
  199. for i, prop := range props {
  200. propVal := UnallocatedSuffix
  201. switch true {
  202. case prop == CloudCostProviderProp:
  203. if ccp.Provider != "" {
  204. propVal = ccp.Provider
  205. }
  206. case prop == CloudCostProviderIDProp:
  207. if ccp.ProviderID != "" {
  208. propVal = ccp.ProviderID
  209. }
  210. case prop == CloudCostCategoryProp:
  211. if ccp.Category != "" {
  212. propVal = ccp.Category
  213. }
  214. case prop == CloudCostInvoiceEntityIDProp:
  215. if ccp.InvoiceEntityID != "" {
  216. propVal = ccp.InvoiceEntityID
  217. }
  218. case prop == CloudCostAccountIDProp:
  219. if ccp.AccountID != "" {
  220. propVal = ccp.AccountID
  221. }
  222. case prop == CloudCostServiceProp:
  223. if ccp.Service != "" {
  224. propVal = ccp.Service
  225. }
  226. case strings.HasPrefix(prop, "label:"):
  227. labels := ccp.Labels
  228. if labels != nil {
  229. labelName := strings.TrimPrefix(prop, "label:")
  230. if labelValue, ok := labels[labelName]; ok && labelValue != "" {
  231. propVal = labelValue
  232. }
  233. }
  234. default:
  235. // This case should never be reached, as input up until this point
  236. // should be checked and rejected if invalid. But if we do get a
  237. // value we don't recognize, log a warning.
  238. log.Warnf("CloudCost: GenerateKey: illegal aggregation parameter: %s", prop)
  239. }
  240. values[i] = propVal
  241. }
  242. return strings.Join(values, "/")
  243. }