key.go 11 KB

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