assetprops.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package opencost
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/opencost/opencost/core/pkg/util/promutil"
  6. )
  7. // AssetProperty is a kind of property belonging to an Asset
  8. type AssetProperty string
  9. const (
  10. // AssetNilProp is the zero-value of AssetProperty
  11. AssetNilProp AssetProperty = ""
  12. // AssetAccountProp describes the account of the Asset
  13. AssetAccountProp AssetProperty = "account"
  14. // AssetCategoryProp describes the category of the Asset
  15. AssetCategoryProp AssetProperty = "category"
  16. // AssetClusterProp describes the cluster of the Asset
  17. AssetClusterProp AssetProperty = "cluster"
  18. // AssetNameProp describes the name of the Asset
  19. AssetNameProp AssetProperty = "name"
  20. // AssetNodeProp describes the node of the Asset
  21. AssetNodeProp AssetProperty = "node"
  22. // AssetProjectProp describes the project of the Asset
  23. AssetProjectProp AssetProperty = "project"
  24. // AssetProviderProp describes the provider of the Asset
  25. AssetProviderProp AssetProperty = "provider"
  26. // AssetProviderIDProp describes the providerID of the Asset
  27. AssetProviderIDProp AssetProperty = "providerID"
  28. // AssetServiceProp describes the service of the Asset
  29. AssetServiceProp AssetProperty = "service"
  30. // AssetTypeProp describes the type of the Asset
  31. AssetTypeProp AssetProperty = "type"
  32. // AssetLabelProp describes a single label within an Asset.
  33. AssetLabelProp AssetProperty = "label"
  34. // AssetDepartmentProp describes the department of the Asset
  35. AssetDepartmentProp AssetProperty = "department"
  36. // AssetEnvironmentProp describes the environment of the Asset
  37. AssetEnvironmentProp AssetProperty = "environment"
  38. // AssetOwnerProp describes the owner of the Asset
  39. AssetOwnerProp AssetProperty = "owner"
  40. // AssetProductProp describes the product of the Asset
  41. AssetProductProp AssetProperty = "product"
  42. // AssetTeamProp describes the team of the Asset
  43. AssetTeamProp AssetProperty = "team"
  44. )
  45. // IsLabel returns true if the allocation property has a label prefix
  46. func (apt *AssetProperty) IsLabel() bool {
  47. return strings.HasPrefix(string(*apt), "label:")
  48. }
  49. // GetLabel returns the label string associated with the label property if it exists.
  50. // Otherwise, empty string is returned.
  51. func (apt *AssetProperty) GetLabel() string {
  52. if apt.IsLabel() {
  53. return strings.TrimSpace(strings.TrimPrefix(string(*apt), "label:"))
  54. }
  55. return ""
  56. }
  57. func ParseAssetProperties(values []string) ([]AssetProperty, error) {
  58. props := []AssetProperty{}
  59. for _, value := range values {
  60. p, err := ParseAssetProperty(value)
  61. if err != nil {
  62. return nil, err
  63. }
  64. props = append(props, p)
  65. }
  66. return props, nil
  67. }
  68. // ParseAssetProperty attempts to parse a string into an AssetProperty
  69. func ParseAssetProperty(text string) (AssetProperty, error) {
  70. switch strings.TrimSpace(strings.ToLower(text)) {
  71. case "account":
  72. return AssetAccountProp, nil
  73. case "category":
  74. return AssetCategoryProp, nil
  75. case "cluster":
  76. return AssetClusterProp, nil
  77. case "name":
  78. return AssetNameProp, nil
  79. case "project":
  80. return AssetProjectProp, nil
  81. case "provider":
  82. return AssetProviderProp, nil
  83. case "providerid":
  84. return AssetProviderIDProp, nil
  85. case "service":
  86. return AssetServiceProp, nil
  87. case "type":
  88. return AssetTypeProp, nil
  89. case "department":
  90. return AssetDepartmentProp, nil
  91. case "environment":
  92. return AssetEnvironmentProp, nil
  93. case "owner":
  94. return AssetOwnerProp, nil
  95. case "product":
  96. return AssetProductProp, nil
  97. case "team":
  98. return AssetTeamProp, nil
  99. }
  100. if strings.HasPrefix(text, "label:") {
  101. label := promutil.SanitizeLabelName(strings.TrimSpace(strings.TrimPrefix(text, "label:")))
  102. return AssetProperty(fmt.Sprintf("label:%s", label)), nil
  103. }
  104. return AssetNilProp, fmt.Errorf("invalid asset property: %s", text)
  105. }
  106. // Category options
  107. // ComputeCategory signifies the Compute Category
  108. const ComputeCategory = "Compute"
  109. // StorageCategory signifies the Storage Category
  110. const StorageCategory = "Storage"
  111. // NetworkCategory signifies the Network Category
  112. const NetworkCategory = "Network"
  113. // ManagementCategory signifies the Management Category
  114. const ManagementCategory = "Management"
  115. // SharedCategory signifies an unassigned Category
  116. const SharedCategory = "Shared"
  117. // OtherCategory signifies an unassigned Category
  118. const OtherCategory = "Other"
  119. // Provider options
  120. // AWSProvider describes the provider AWS
  121. const AWSProvider = "AWS"
  122. // describes how AWS labels nodepool nodes
  123. const EKSNodepoolLabel = "eks.amazonaws.com/nodegroup"
  124. // GCPProvider describes the provider GCP
  125. const GCPProvider = "GCP"
  126. // describes how nodepool nodes are labeled in GKE
  127. const GKENodePoolLabel = "cloud.google.com/gke-nodepool"
  128. // AzureProvider describes the provider Azure
  129. const AzureProvider = "Azure"
  130. // describes how Azure labels nodepool nodes
  131. const AKSNodepoolLabel = "kubernetes.azure.com/agentpool"
  132. // AlibabaProvider describes the provider for Alibaba Cloud
  133. const AlibabaProvider = "Alibaba"
  134. // CSVProvider describes the provider a CSV
  135. const CSVProvider = "CSV"
  136. // CustomProvider describes a custom provider
  137. const CustomProvider = "custom"
  138. // ScalewayProvider describes the provider Scaleway
  139. const ScalewayProvider = "Scaleway"
  140. // OracleProvider describes the provider Oracle
  141. const OracleProvider = "Oracle"
  142. // OTCProvider describes the provider OTC
  143. const OTCProvider = "OTC"
  144. // NilProvider describes unknown provider
  145. const NilProvider = "-"
  146. // Service options
  147. const KubernetesService = "Kubernetes"
  148. // ParseProvider attempts to parse and return a known provider, given a string
  149. func ParseProvider(str string) string {
  150. switch strings.ToLower(strings.TrimSpace(str)) {
  151. case "aws", "eks", "amazon":
  152. return AWSProvider
  153. case "gcp", "gke", "google":
  154. return GCPProvider
  155. case "azure":
  156. return AzureProvider
  157. case "scaleway", "scw", "kapsule":
  158. return ScalewayProvider
  159. case "oci", "oracle":
  160. return OracleProvider
  161. default:
  162. return NilProvider
  163. }
  164. }
  165. // AssetProperties describes all properties assigned to an Asset.
  166. type AssetProperties struct {
  167. Category string `json:"category,omitempty"`
  168. Provider string `json:"provider,omitempty"`
  169. Account string `json:"account,omitempty"`
  170. Project string `json:"project,omitempty"`
  171. Service string `json:"service,omitempty"`
  172. Cluster string `json:"cluster,omitempty"`
  173. Name string `json:"name,omitempty"`
  174. ProviderID string `json:"providerID,omitempty"`
  175. }
  176. // Clone returns a cloned instance of the given AssetProperties
  177. func (ap *AssetProperties) Clone() *AssetProperties {
  178. if ap == nil {
  179. return nil
  180. }
  181. clone := &AssetProperties{}
  182. clone.Category = ap.Category
  183. clone.Provider = ap.Provider
  184. clone.Account = ap.Account
  185. clone.Project = ap.Project
  186. clone.Service = ap.Service
  187. clone.Cluster = ap.Cluster
  188. clone.Name = ap.Name
  189. clone.ProviderID = ap.ProviderID
  190. return clone
  191. }
  192. // Equal returns true only if both AssetProperties are matches
  193. func (ap *AssetProperties) Equal(that *AssetProperties) bool {
  194. if ap == nil && that == nil {
  195. return true
  196. }
  197. if ap == nil || that == nil {
  198. return false
  199. }
  200. if ap.Category != that.Category {
  201. return false
  202. }
  203. if ap.Provider != that.Provider {
  204. return false
  205. }
  206. if ap.Account != that.Account {
  207. return false
  208. }
  209. if ap.Project != that.Project {
  210. return false
  211. }
  212. if ap.Service != that.Service {
  213. return false
  214. }
  215. if ap.Cluster != that.Cluster {
  216. return false
  217. }
  218. if ap.Name != that.Name {
  219. return false
  220. }
  221. if ap.ProviderID != that.ProviderID {
  222. return false
  223. }
  224. return true
  225. }
  226. // Keys returns the list of string values used to key the Asset based on the
  227. // list of properties provided.
  228. func (ap *AssetProperties) Keys(props []AssetProperty) []string {
  229. keys := []string{}
  230. if ap == nil {
  231. return keys
  232. }
  233. if (props == nil || hasProp(props, AssetCategoryProp)) && ap.Category != "" {
  234. keys = append(keys, ap.Category)
  235. }
  236. if (props == nil || hasProp(props, AssetProviderProp)) && ap.Provider != "" {
  237. keys = append(keys, ap.Provider)
  238. }
  239. if (props == nil || hasProp(props, AssetAccountProp)) && ap.Account != "" {
  240. keys = append(keys, ap.Account)
  241. }
  242. if (props == nil || hasProp(props, AssetProjectProp)) && ap.Project != "" {
  243. keys = append(keys, ap.Project)
  244. }
  245. if (props == nil || hasProp(props, AssetServiceProp)) && ap.Service != "" {
  246. keys = append(keys, ap.Service)
  247. }
  248. if (props == nil || hasProp(props, AssetClusterProp)) && ap.Cluster != "" {
  249. keys = append(keys, ap.Cluster)
  250. }
  251. if (props == nil || hasProp(props, AssetNameProp)) && ap.Name != "" {
  252. keys = append(keys, ap.Name)
  253. }
  254. if (props == nil || hasProp(props, AssetProviderIDProp)) && ap.ProviderID != "" {
  255. keys = append(keys, ap.ProviderID)
  256. }
  257. return keys
  258. }
  259. // Merge retains only the properties shared with the given AssetProperties
  260. func (ap *AssetProperties) Merge(that *AssetProperties) *AssetProperties {
  261. if ap == nil || that == nil {
  262. return nil
  263. }
  264. result := &AssetProperties{}
  265. if ap.Category == that.Category {
  266. result.Category = ap.Category
  267. }
  268. if ap.Provider == that.Provider {
  269. result.Provider = ap.Provider
  270. }
  271. if ap.Account == that.Account {
  272. result.Account = ap.Account
  273. }
  274. if ap.Project == that.Project {
  275. result.Project = ap.Project
  276. }
  277. if ap.Service == that.Service {
  278. result.Service = ap.Service
  279. }
  280. if ap.Cluster == that.Cluster {
  281. result.Cluster = ap.Cluster
  282. }
  283. if ap.Name == that.Name {
  284. result.Name = ap.Name
  285. }
  286. if ap.ProviderID == that.ProviderID {
  287. result.ProviderID = ap.ProviderID
  288. }
  289. return result
  290. }
  291. // String represents the properties as a string
  292. func (ap *AssetProperties) String() string {
  293. if ap == nil {
  294. return "<nil>"
  295. }
  296. strs := []string{}
  297. if ap.Category != "" {
  298. strs = append(strs, "Category:"+ap.Category)
  299. }
  300. if ap.Provider != "" {
  301. strs = append(strs, "Provider:"+ap.Provider)
  302. }
  303. if ap.Account != "" {
  304. strs = append(strs, "Account:"+ap.Account)
  305. }
  306. if ap.Project != "" {
  307. strs = append(strs, "Project:"+ap.Project)
  308. }
  309. if ap.Service != "" {
  310. strs = append(strs, "Service:"+ap.Service)
  311. }
  312. if ap.Cluster != "" {
  313. strs = append(strs, "Cluster:"+ap.Cluster)
  314. }
  315. if ap.Name != "" {
  316. strs = append(strs, "Name:"+ap.Name)
  317. }
  318. if ap.ProviderID != "" {
  319. strs = append(strs, "ProviderID:"+ap.ProviderID)
  320. }
  321. return strings.Join(strs, ",")
  322. }
  323. func hasProp(props []AssetProperty, prop AssetProperty) bool {
  324. for _, p := range props {
  325. if p == prop {
  326. return true
  327. }
  328. }
  329. return false
  330. }