key.go 11 KB

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