key.go 12 KB

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