key.go 11 KB

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