key.go 12 KB

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