key.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. type serviceKey struct {
  205. Cluster string
  206. Namespace string
  207. Service string
  208. }
  209. func (k serviceKey) String() string {
  210. return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.Service)
  211. }
  212. func newServiceKey(cluster, namespace, service string) serviceKey {
  213. return serviceKey{
  214. Cluster: cluster,
  215. Namespace: namespace,
  216. Service: service,
  217. }
  218. }
  219. // resultServiceKey converts a Prometheus query result to a serviceKey by
  220. // looking up values associated with the given label names. For example,
  221. // passing "cluster_id" for clusterLabel will use the value of the label
  222. // "cluster_id" as the serviceKey's Cluster field. If a given field does not
  223. // exist on the result, an error is returned. (The only exception to that is
  224. // clusterLabel, which we expect may not exist, but has a default value.)
  225. func resultServiceKey(res *prom.QueryResult, clusterLabel, namespaceLabel, serviceLabel string) (serviceKey, error) {
  226. key := serviceKey{}
  227. cluster, err := res.GetString(clusterLabel)
  228. if err != nil {
  229. cluster = env.GetClusterID()
  230. }
  231. key.Cluster = cluster
  232. namespace, err := res.GetString(namespaceLabel)
  233. if err != nil {
  234. return key, err
  235. }
  236. key.Namespace = namespace
  237. service, err := res.GetString(serviceLabel)
  238. if err != nil {
  239. return key, err
  240. }
  241. key.Service = service
  242. return key, nil
  243. }
  244. type nodeKey struct {
  245. Cluster string
  246. Node string
  247. }
  248. func (k nodeKey) String() string {
  249. return fmt.Sprintf("%s/%s", k.Cluster, k.Node)
  250. }
  251. func newNodeKey(cluster, node string) nodeKey {
  252. return nodeKey{
  253. Cluster: cluster,
  254. Node: node,
  255. }
  256. }
  257. // resultNodeKey converts a Prometheus query result to a nodeKey by
  258. // looking up values associated with the given label names. For example,
  259. // passing "cluster_id" for clusterLabel will use the value of the label
  260. // "cluster_id" as the nodeKey's Cluster field. If a given field does not
  261. // exist on the result, an error is returned. (The only exception to that is
  262. // clusterLabel, which we expect may not exist, but has a default value.)
  263. func resultNodeKey(res *prom.QueryResult, clusterLabel, nodeLabel string) (nodeKey, error) {
  264. key := nodeKey{}
  265. cluster, err := res.GetString(clusterLabel)
  266. if err != nil {
  267. cluster = env.GetClusterID()
  268. }
  269. key.Cluster = cluster
  270. node, err := res.GetString(nodeLabel)
  271. if err != nil {
  272. return key, err
  273. }
  274. key.Node = node
  275. return key, nil
  276. }
  277. type pvcKey struct {
  278. Cluster string
  279. Namespace string
  280. PersistentVolumeClaim string
  281. }
  282. func (k pvcKey) String() string {
  283. return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.PersistentVolumeClaim)
  284. }
  285. func newPVCKey(cluster, namespace, persistentVolumeClaim string) pvcKey {
  286. return pvcKey{
  287. Cluster: cluster,
  288. Namespace: namespace,
  289. PersistentVolumeClaim: persistentVolumeClaim,
  290. }
  291. }
  292. // resultPVCKey converts a Prometheus query result to a pvcKey by
  293. // looking up values associated with the given label names. For example,
  294. // passing "cluster_id" for clusterLabel will use the value of the label
  295. // "cluster_id" as the pvcKey's Cluster field. If a given field does not
  296. // exist on the result, an error is returned. (The only exception to that is
  297. // clusterLabel, which we expect may not exist, but has a default value.)
  298. func resultPVCKey(res *prom.QueryResult, clusterLabel, namespaceLabel, pvcLabel string) (pvcKey, error) {
  299. key := pvcKey{}
  300. cluster, err := res.GetString(clusterLabel)
  301. if err != nil {
  302. cluster = env.GetClusterID()
  303. }
  304. key.Cluster = cluster
  305. namespace, err := res.GetString(namespaceLabel)
  306. if err != nil {
  307. return key, err
  308. }
  309. key.Namespace = namespace
  310. pvc, err := res.GetString(pvcLabel)
  311. if err != nil {
  312. return key, err
  313. }
  314. key.PersistentVolumeClaim = pvc
  315. return key, nil
  316. }
  317. type pvKey struct {
  318. Cluster string
  319. PersistentVolume string
  320. }
  321. func (k pvKey) String() string {
  322. return fmt.Sprintf("%s/%s", k.Cluster, k.PersistentVolume)
  323. }
  324. func newPVKey(cluster, persistentVolume string) pvKey {
  325. return pvKey{
  326. Cluster: cluster,
  327. PersistentVolume: persistentVolume,
  328. }
  329. }
  330. // resultPVKey converts a Prometheus query result to a pvKey by
  331. // looking up values associated with the given label names. For example,
  332. // passing "cluster_id" for clusterLabel will use the value of the label
  333. // "cluster_id" as the pvKey's Cluster field. If a given field does not
  334. // exist on the result, an error is returned. (The only exception to that is
  335. // clusterLabel, which we expect may not exist, but has a default value.)
  336. func resultPVKey(res *prom.QueryResult, clusterLabel, persistentVolumeLabel string) (pvKey, error) {
  337. key := pvKey{}
  338. cluster, err := res.GetString(clusterLabel)
  339. if err != nil {
  340. cluster = env.GetClusterID()
  341. }
  342. key.Cluster = cluster
  343. persistentVolume, err := res.GetString(persistentVolumeLabel)
  344. if err != nil {
  345. return key, err
  346. }
  347. key.PersistentVolume = persistentVolume
  348. return key, nil
  349. }