assetprops.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. // NilProvider describes unknown provider
  143. const NilProvider = "-"
  144. // Service options
  145. const KubernetesService = "Kubernetes"
  146. // ParseProvider attempts to parse and return a known provider, given a string
  147. func ParseProvider(str string) string {
  148. switch strings.ToLower(strings.TrimSpace(str)) {
  149. case "aws", "eks", "amazon":
  150. return AWSProvider
  151. case "gcp", "gke", "google":
  152. return GCPProvider
  153. case "azure":
  154. return AzureProvider
  155. case "scaleway", "scw", "kapsule":
  156. return ScalewayProvider
  157. case "oci", "oracle":
  158. return OracleProvider
  159. default:
  160. return NilProvider
  161. }
  162. }
  163. // AssetProperties describes all properties assigned to an Asset.
  164. type AssetProperties struct {
  165. Category string `json:"category,omitempty"`
  166. Provider string `json:"provider,omitempty"`
  167. Account string `json:"account,omitempty"`
  168. Project string `json:"project,omitempty"`
  169. Service string `json:"service,omitempty"`
  170. Cluster string `json:"cluster,omitempty"`
  171. Name string `json:"name,omitempty"`
  172. ProviderID string `json:"providerID,omitempty"`
  173. }
  174. // Clone returns a cloned instance of the given AssetProperties
  175. func (ap *AssetProperties) Clone() *AssetProperties {
  176. if ap == nil {
  177. return nil
  178. }
  179. clone := &AssetProperties{}
  180. clone.Category = ap.Category
  181. clone.Provider = ap.Provider
  182. clone.Account = ap.Account
  183. clone.Project = ap.Project
  184. clone.Service = ap.Service
  185. clone.Cluster = ap.Cluster
  186. clone.Name = ap.Name
  187. clone.ProviderID = ap.ProviderID
  188. return clone
  189. }
  190. // Equal returns true only if both AssetProperties are matches
  191. func (ap *AssetProperties) Equal(that *AssetProperties) bool {
  192. if ap == nil && that == nil {
  193. return true
  194. }
  195. if ap == nil || that == nil {
  196. return false
  197. }
  198. if ap.Category != that.Category {
  199. return false
  200. }
  201. if ap.Provider != that.Provider {
  202. return false
  203. }
  204. if ap.Account != that.Account {
  205. return false
  206. }
  207. if ap.Project != that.Project {
  208. return false
  209. }
  210. if ap.Service != that.Service {
  211. return false
  212. }
  213. if ap.Cluster != that.Cluster {
  214. return false
  215. }
  216. if ap.Name != that.Name {
  217. return false
  218. }
  219. if ap.ProviderID != that.ProviderID {
  220. return false
  221. }
  222. return true
  223. }
  224. // Keys returns the list of string values used to key the Asset based on the
  225. // list of properties provided.
  226. func (ap *AssetProperties) Keys(props []AssetProperty) []string {
  227. keys := []string{}
  228. if ap == nil {
  229. return keys
  230. }
  231. if (props == nil || hasProp(props, AssetCategoryProp)) && ap.Category != "" {
  232. keys = append(keys, ap.Category)
  233. }
  234. if (props == nil || hasProp(props, AssetProviderProp)) && ap.Provider != "" {
  235. keys = append(keys, ap.Provider)
  236. }
  237. if (props == nil || hasProp(props, AssetAccountProp)) && ap.Account != "" {
  238. keys = append(keys, ap.Account)
  239. }
  240. if (props == nil || hasProp(props, AssetProjectProp)) && ap.Project != "" {
  241. keys = append(keys, ap.Project)
  242. }
  243. if (props == nil || hasProp(props, AssetServiceProp)) && ap.Service != "" {
  244. keys = append(keys, ap.Service)
  245. }
  246. if (props == nil || hasProp(props, AssetClusterProp)) && ap.Cluster != "" {
  247. keys = append(keys, ap.Cluster)
  248. }
  249. if (props == nil || hasProp(props, AssetNameProp)) && ap.Name != "" {
  250. keys = append(keys, ap.Name)
  251. }
  252. if (props == nil || hasProp(props, AssetProviderIDProp)) && ap.ProviderID != "" {
  253. keys = append(keys, ap.ProviderID)
  254. }
  255. return keys
  256. }
  257. // Merge retains only the properties shared with the given AssetProperties
  258. func (ap *AssetProperties) Merge(that *AssetProperties) *AssetProperties {
  259. if ap == nil || that == nil {
  260. return nil
  261. }
  262. result := &AssetProperties{}
  263. if ap.Category == that.Category {
  264. result.Category = ap.Category
  265. }
  266. if ap.Provider == that.Provider {
  267. result.Provider = ap.Provider
  268. }
  269. if ap.Account == that.Account {
  270. result.Account = ap.Account
  271. }
  272. if ap.Project == that.Project {
  273. result.Project = ap.Project
  274. }
  275. if ap.Service == that.Service {
  276. result.Service = ap.Service
  277. }
  278. if ap.Cluster == that.Cluster {
  279. result.Cluster = ap.Cluster
  280. }
  281. if ap.Name == that.Name {
  282. result.Name = ap.Name
  283. }
  284. if ap.ProviderID == that.ProviderID {
  285. result.ProviderID = ap.ProviderID
  286. }
  287. return result
  288. }
  289. // String represents the properties as a string
  290. func (ap *AssetProperties) String() string {
  291. if ap == nil {
  292. return "<nil>"
  293. }
  294. strs := []string{}
  295. if ap.Category != "" {
  296. strs = append(strs, "Category:"+ap.Category)
  297. }
  298. if ap.Provider != "" {
  299. strs = append(strs, "Provider:"+ap.Provider)
  300. }
  301. if ap.Account != "" {
  302. strs = append(strs, "Account:"+ap.Account)
  303. }
  304. if ap.Project != "" {
  305. strs = append(strs, "Project:"+ap.Project)
  306. }
  307. if ap.Service != "" {
  308. strs = append(strs, "Service:"+ap.Service)
  309. }
  310. if ap.Cluster != "" {
  311. strs = append(strs, "Cluster:"+ap.Cluster)
  312. }
  313. if ap.Name != "" {
  314. strs = append(strs, "Name:"+ap.Name)
  315. }
  316. if ap.ProviderID != "" {
  317. strs = append(strs, "ProviderID:"+ap.ProviderID)
  318. }
  319. return strings.Join(strs, ",")
  320. }
  321. func hasProp(props []AssetProperty, prop AssetProperty) bool {
  322. for _, p := range props {
  323. if p == prop {
  324. return true
  325. }
  326. }
  327. return false
  328. }