key.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package costmodel
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/core/pkg/opencost"
  5. "github.com/opencost/opencost/core/pkg/source"
  6. "github.com/opencost/opencost/pkg/env"
  7. )
  8. type containerKey struct {
  9. Cluster string
  10. Namespace string
  11. Pod string
  12. Container string
  13. }
  14. func (k containerKey) String() string {
  15. return fmt.Sprintf("%s/%s/%s/%s", k.Cluster, k.Namespace, k.Pod, k.Container)
  16. }
  17. func newContainerKey(cluster, namespace, pod, container string) containerKey {
  18. return containerKey{
  19. Cluster: cluster,
  20. Namespace: namespace,
  21. Pod: pod,
  22. Container: container,
  23. }
  24. }
  25. // resultContainerKey converts a Prometheus query result to a containerKey by
  26. // looking up values associated with the given label names. For example,
  27. // passing "cluster_id" for clusterLabel will use the value of the label
  28. // "cluster_id" as the containerKey's Cluster field. If a given field does not
  29. // exist on the result, an error is returned. (The only exception to that is
  30. // clusterLabel, which we expect may not exist, but has a default value.)
  31. func resultContainerKey(res *source.QueryResult) (containerKey, error) {
  32. key := containerKey{}
  33. cluster, err := res.GetCluster()
  34. if err != nil {
  35. cluster = env.GetClusterID()
  36. }
  37. key.Cluster = cluster
  38. namespace, err := res.GetNamespace()
  39. if err != nil {
  40. return key, err
  41. }
  42. key.Namespace = namespace
  43. pod, err := res.GetPod()
  44. if err != nil {
  45. return key, err
  46. }
  47. key.Pod = pod
  48. container, err := res.GetContainer()
  49. if err != nil {
  50. return key, err
  51. }
  52. key.Container = container
  53. return key, nil
  54. }
  55. type podKey struct {
  56. namespaceKey
  57. Pod string
  58. }
  59. func (k podKey) String() string {
  60. return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.Pod)
  61. }
  62. func newPodKey(cluster, namespace, pod string) podKey {
  63. return podKey{
  64. namespaceKey: namespaceKey{
  65. Cluster: cluster,
  66. Namespace: namespace,
  67. },
  68. Pod: pod,
  69. }
  70. }
  71. // getUnmountedPodKey while certain Unmounted costs can have a namespace, all unmounted costs for a single cluster will be represented by the same asset
  72. func getUnmountedPodKey(cluster string) podKey {
  73. return newPodKey(cluster, opencost.UnmountedSuffix, opencost.UnmountedSuffix)
  74. }
  75. // resultPodKey converts a Prometheus query result to a podKey by looking
  76. // up values associated with the given label names. For example, passing
  77. // "cluster_id" for clusterLabel will use the value of the label "cluster_id"
  78. // as the podKey's Cluster field. If a given field does not exist on the
  79. // result, an error is returned. (The only exception to that is clusterLabel,
  80. // which we expect may not exist, but has a default value.)
  81. func resultPodKey(res *source.QueryResult) (podKey, error) {
  82. key := podKey{}
  83. cluster, err := res.GetCluster()
  84. if err != nil {
  85. cluster = env.GetClusterID()
  86. }
  87. key.Cluster = cluster
  88. namespace, err := res.GetNamespace()
  89. if err != nil {
  90. return key, err
  91. }
  92. key.Namespace = namespace
  93. pod, err := res.GetPod()
  94. if err != nil {
  95. return key, err
  96. }
  97. key.Pod = pod
  98. return key, nil
  99. }
  100. type namespaceKey struct {
  101. Cluster string
  102. Namespace string
  103. }
  104. func (k namespaceKey) String() string {
  105. return fmt.Sprintf("%s/%s", k.Cluster, k.Namespace)
  106. }
  107. func newNamespaceKey(cluster, namespace string) namespaceKey {
  108. return namespaceKey{
  109. Cluster: cluster,
  110. Namespace: namespace,
  111. }
  112. }
  113. // resultNamespaceKey converts a Prometheus query result to a namespaceKey by
  114. // looking up values associated with the given label names. For example,
  115. // passing "cluster_id" for clusterLabel will use the value of the label
  116. // "cluster_id" as the namespaceKey's Cluster field. If a given field does not
  117. // exist on the result, an error is returned. (The only exception to that is
  118. // clusterLabel, which we expect may not exist, but has a default value.)
  119. func resultNamespaceKey(res *source.QueryResult) (namespaceKey, error) {
  120. key := namespaceKey{}
  121. cluster, err := res.GetCluster()
  122. if err != nil {
  123. cluster = env.GetClusterID()
  124. }
  125. key.Cluster = cluster
  126. namespace, err := res.GetNamespace()
  127. if err != nil {
  128. return key, err
  129. }
  130. key.Namespace = namespace
  131. return key, nil
  132. }
  133. type controllerKey struct {
  134. Cluster string
  135. Namespace string
  136. ControllerKind string
  137. Controller string
  138. }
  139. func (k controllerKey) String() string {
  140. return fmt.Sprintf("%s/%s/%s/%s", k.Cluster, k.Namespace, k.ControllerKind, k.Controller)
  141. }
  142. func newControllerKey(cluster, namespace, controllerKind, controller string) controllerKey {
  143. return controllerKey{
  144. Cluster: cluster,
  145. Namespace: namespace,
  146. ControllerKind: controllerKind,
  147. Controller: controller,
  148. }
  149. }
  150. // resultControllerKey converts a Prometheus query result to a controllerKey by
  151. // looking up values associated with the given label names. For example,
  152. // passing "cluster_id" for clusterLabel will use the value of the label
  153. // "cluster_id" as the controllerKey's Cluster field. If a given field does not
  154. // exist on the result, an error is returned. (The only exception to that is
  155. // clusterLabel, which we expect may not exist, but has a default value.)
  156. func resultControllerKey(controllerKind string, res *source.QueryResult, controllerLabel string) (controllerKey, error) {
  157. key := controllerKey{}
  158. cluster, err := res.GetCluster()
  159. if err != nil {
  160. cluster = env.GetClusterID()
  161. }
  162. key.Cluster = cluster
  163. namespace, err := res.GetNamespace()
  164. if err != nil {
  165. return key, err
  166. }
  167. key.Namespace = namespace
  168. controller, err := res.GetString(controllerLabel)
  169. if err != nil {
  170. return key, err
  171. }
  172. key.Controller = controller
  173. key.ControllerKind = controllerKind
  174. return key, nil
  175. }
  176. // resultDeploymentKey creates a controllerKey for a Deployment.
  177. // (See resultControllerKey for more.)
  178. func resultDeploymentKey(res *source.QueryResult, controllerLabel string) (controllerKey, error) {
  179. return resultControllerKey("deployment", res, controllerLabel)
  180. }
  181. // resultStatefulSetKey creates a controllerKey for a StatefulSet.
  182. // (See resultControllerKey for more.)
  183. func resultStatefulSetKey(res *source.QueryResult, controllerLabel string) (controllerKey, error) {
  184. return resultControllerKey("statefulset", res, controllerLabel)
  185. }
  186. // resultDaemonSetKey creates a controllerKey for a DaemonSet.
  187. // (See resultControllerKey for more.)
  188. func resultDaemonSetKey(res *source.QueryResult, controllerLabel string) (controllerKey, error) {
  189. return resultControllerKey("daemonset", res, controllerLabel)
  190. }
  191. // resultJobKey creates a controllerKey for a Job.
  192. // (See resultControllerKey for more.)
  193. func resultJobKey(res *source.QueryResult, controllerLabel string) (controllerKey, error) {
  194. return resultControllerKey("job", res, controllerLabel)
  195. }
  196. // resultReplicaSetKey creates a controllerKey for a Job.
  197. // (See resultControllerKey for more.)
  198. func resultReplicaSetKey(res *source.QueryResult, controllerLabel string) (controllerKey, error) {
  199. return resultControllerKey("replicaset", res, controllerLabel)
  200. }
  201. // resultReplicaSetRolloutKey creates a controllerKey for a Job.
  202. // (See resultControllerKey for more.)
  203. func resultReplicaSetRolloutKey(res *source.QueryResult, controllerLabel string) (controllerKey, error) {
  204. return resultControllerKey("rollout", res, controllerLabel)
  205. }
  206. type serviceKey struct {
  207. Cluster string
  208. Namespace string
  209. Service string
  210. }
  211. func (k serviceKey) String() string {
  212. return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.Service)
  213. }
  214. func newServiceKey(cluster, namespace, service string) serviceKey {
  215. return serviceKey{
  216. Cluster: cluster,
  217. Namespace: namespace,
  218. Service: service,
  219. }
  220. }
  221. // resultServiceKey converts a Prometheus query result to a serviceKey by
  222. // looking up values associated with the given label names. For example,
  223. // passing "cluster_id" for clusterLabel will use the value of the label
  224. // "cluster_id" as the serviceKey's Cluster field. If a given field does not
  225. // exist on the result, an error is returned. (The only exception to that is
  226. // clusterLabel, which we expect may not exist, but has a default value.)
  227. func resultServiceKey(res *source.QueryResult, serviceLabel string) (serviceKey, error) {
  228. key := serviceKey{}
  229. cluster, err := res.GetCluster()
  230. if err != nil {
  231. cluster = env.GetClusterID()
  232. }
  233. key.Cluster = cluster
  234. namespace, err := res.GetNamespace()
  235. if err != nil {
  236. return key, err
  237. }
  238. key.Namespace = namespace
  239. service, err := res.GetString(serviceLabel)
  240. if err != nil {
  241. return key, err
  242. }
  243. key.Service = service
  244. return key, nil
  245. }
  246. type nodeKey struct {
  247. Cluster string
  248. Node string
  249. }
  250. func (k nodeKey) String() string {
  251. return fmt.Sprintf("%s/%s", k.Cluster, k.Node)
  252. }
  253. func newNodeKey(cluster, node string) nodeKey {
  254. return nodeKey{
  255. Cluster: cluster,
  256. Node: node,
  257. }
  258. }
  259. // resultNodeKey converts a Prometheus query result to a nodeKey by
  260. // looking up values associated with the given label names. For example,
  261. // passing "cluster_id" for clusterLabel will use the value of the label
  262. // "cluster_id" as the nodeKey's Cluster field. If a given field does not
  263. // exist on the result, an error is returned. (The only exception to that is
  264. // clusterLabel, which we expect may not exist, but has a default value.)
  265. func resultNodeKey(res *source.QueryResult) (nodeKey, error) {
  266. key := nodeKey{}
  267. cluster, err := res.GetCluster()
  268. if err != nil {
  269. cluster = env.GetClusterID()
  270. }
  271. key.Cluster = cluster
  272. node, err := res.GetNode()
  273. if err != nil {
  274. return key, err
  275. }
  276. key.Node = node
  277. return key, nil
  278. }
  279. type pvcKey struct {
  280. Cluster string
  281. Namespace string
  282. PersistentVolumeClaim string
  283. }
  284. func (k pvcKey) String() string {
  285. return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.PersistentVolumeClaim)
  286. }
  287. func newPVCKey(cluster, namespace, persistentVolumeClaim string) pvcKey {
  288. return pvcKey{
  289. Cluster: cluster,
  290. Namespace: namespace,
  291. PersistentVolumeClaim: persistentVolumeClaim,
  292. }
  293. }
  294. // resultPVCKey converts a Prometheus query result to a pvcKey by
  295. // looking up values associated with the given label names. For example,
  296. // passing "cluster_id" for clusterLabel will use the value of the label
  297. // "cluster_id" as the pvcKey's Cluster field. If a given field does not
  298. // exist on the result, an error is returned. (The only exception to that is
  299. // clusterLabel, which we expect may not exist, but has a default value.)
  300. func resultPVCKey(res *source.QueryResult, pvcLabel string) (pvcKey, error) {
  301. key := pvcKey{}
  302. cluster, err := res.GetCluster()
  303. if err != nil {
  304. cluster = env.GetClusterID()
  305. }
  306. key.Cluster = cluster
  307. namespace, err := res.GetNamespace()
  308. if err != nil {
  309. return key, err
  310. }
  311. key.Namespace = namespace
  312. pvc, err := res.GetString(pvcLabel)
  313. if err != nil {
  314. return key, err
  315. }
  316. key.PersistentVolumeClaim = pvc
  317. return key, nil
  318. }
  319. type pvKey struct {
  320. Cluster string
  321. PersistentVolume string
  322. }
  323. func (k pvKey) String() string {
  324. return fmt.Sprintf("%s/%s", k.Cluster, k.PersistentVolume)
  325. }
  326. func newPVKey(cluster, persistentVolume string) pvKey {
  327. return pvKey{
  328. Cluster: cluster,
  329. PersistentVolume: persistentVolume,
  330. }
  331. }
  332. // resultPVKey converts a Prometheus query result to a pvKey by
  333. // looking up values associated with the given label names. For example,
  334. // passing "cluster_id" for clusterLabel will use the value of the label
  335. // "cluster_id" as the pvKey's Cluster field. If a given field does not
  336. // exist on the result, an error is returned. (The only exception to that is
  337. // clusterLabel, which we expect may not exist, but has a default value.)
  338. func resultPVKey(res *source.QueryResult, persistentVolumeLabel string) (pvKey, error) {
  339. key := pvKey{}
  340. cluster, err := res.GetCluster()
  341. if err != nil {
  342. cluster = env.GetClusterID()
  343. }
  344. key.Cluster = cluster
  345. persistentVolume, err := res.GetString(persistentVolumeLabel)
  346. if err != nil {
  347. return key, err
  348. }
  349. key.PersistentVolume = persistentVolume
  350. return key, nil
  351. }