allocation.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. package costmodel
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/kubecost/cost-model/pkg/env"
  6. "github.com/kubecost/cost-model/pkg/kubecost"
  7. "github.com/kubecost/cost-model/pkg/log"
  8. "github.com/kubecost/cost-model/pkg/prom"
  9. "github.com/kubecost/cost-model/pkg/thanos"
  10. )
  11. // ComputeAllocation uses the CostModel instance to compute an AllocationSet
  12. // for the window defined by the given start and end times. The Allocations
  13. // returned are unaggregated (i.e. down to the container level).
  14. func (cm *CostModel) ComputeAllocation(start, end time.Time) (*kubecost.AllocationSet, error) {
  15. // Create a window spanning the requested query
  16. s, e := start, end
  17. window := kubecost.NewWindow(&s, &e)
  18. // Create an empty AllocationSet. For safety, in the case of an error, we
  19. // should prefer to return this empty set with the error. (In the case of
  20. // no error, of course we populate the set and return it.)
  21. allocSet := kubecost.NewAllocationSet(start, end)
  22. // Convert window (start, end) to (duration, offset) for querying Prometheus
  23. timesToDurations := func(s, e time.Time) (dur, off time.Duration) {
  24. now := time.Now()
  25. off = now.Sub(e)
  26. dur = e.Sub(s)
  27. return dur, off
  28. }
  29. duration, offset := timesToDurations(start, end)
  30. // If using Thanos, increase offset to 3 hours, reducing the duration by
  31. // equal measure to maintain the same starting point.
  32. thanosDur := thanos.OffsetDuration()
  33. // TODO niko/cdmr confirm that this flag works interchangeably with ThanosClient != nil
  34. if offset < thanosDur && env.IsThanosEnabled() {
  35. diff := thanosDur - offset
  36. offset += diff
  37. duration -= diff
  38. }
  39. // If duration < 0, return an empty set
  40. if duration < 0 {
  41. return allocSet, nil
  42. }
  43. // Negative offset means that the end time is in the future. Prometheus
  44. // fails for non-positive offset values, so shrink the duration and
  45. // remove the offset altogether.
  46. if offset < 0 {
  47. duration = duration + offset
  48. offset = 0
  49. }
  50. durStr := fmt.Sprintf("%dm", int64(duration.Minutes()))
  51. offStr := fmt.Sprintf("%dm", int64(offset.Minutes()))
  52. if offset < time.Minute {
  53. offStr = ""
  54. }
  55. // TODO niko/cdmr dynamic resolution? add to ComputeAllocation() in allocation.Source?
  56. resStr := "1m"
  57. // resPerHr := 60
  58. ctx := prom.NewContext(cm.PrometheusClient)
  59. // TODO niko/cdmr retries? (That should probably go into the Store.)
  60. // TODO niko/cmdr check: will multiple Prometheus jobs multiply the totals?
  61. // TODO niko/cdmr should we try doing this without resolution? Could yield
  62. // more accurate results, but might also be more challenging in some
  63. // respects; e.g. "correcting" the start point by what amount?
  64. queryMinutes := fmt.Sprintf(`
  65. avg(kube_pod_container_status_running{}) by (container, pod, namespace, kubernetes_node, cluster_id)[%s:%s]%s
  66. `, durStr, resStr, offStr)
  67. resChMinutes := ctx.Query(queryMinutes)
  68. queryRAMBytesAllocated := fmt.Sprintf(`
  69. avg(
  70. avg_over_time(container_memory_allocation_bytes{container!="", container!="POD", node!=""}[%s]%s)
  71. ) by (container, pod, namespace, node, cluster_id)
  72. `, durStr, offStr)
  73. resChRAMBytesAllocated := ctx.Query(queryRAMBytesAllocated)
  74. // TODO niko/cdmr
  75. // queryRAMRequests := fmt.Sprintf()
  76. // resChRAMRequests := ctx.Query(queryRAMRequests)
  77. // TODO niko/cdmr
  78. // queryRAMUsage := fmt.Sprintf()
  79. // resChRAMUsage := ctx.Query(queryRAMUsage)
  80. queryCPUCoresAllocated := fmt.Sprintf(`
  81. avg(
  82. avg_over_time(container_cpu_allocation{container!="", container!="POD", node!=""}[%s]%s)
  83. ) by (container, pod, namespace, node, cluster_id)
  84. `, durStr, offStr)
  85. resChCPUCoresAllocated := ctx.Query(queryCPUCoresAllocated)
  86. // TODO niko/cdmr
  87. // queryCPURequests := fmt.Sprintf()
  88. // resChCPURequests := ctx.Query(queryCPURequests)
  89. // TODO niko/cdmr
  90. // queryCPUUsage := fmt.Sprintf()
  91. // resChCPUUsage := ctx.Query(queryCPUUsage)
  92. // TODO niko/cdmr find an env with GPUs to test this (generate one?)
  93. queryGPUsRequested := fmt.Sprintf(`
  94. avg(
  95. avg_over_time(kube_pod_container_resource_requests{resource="nvidia_com_gpu", container!="",container!="POD", node!=""}[%s]%s)
  96. ) by (container, pod, namespace, node, cluster_id)
  97. `, durStr, offStr)
  98. resChGPUsRequested := ctx.Query(queryGPUsRequested)
  99. queryPVCAllocation := fmt.Sprintf(`
  100. avg(
  101. avg_over_time(pod_pvc_allocation[%s]%s)
  102. ) by (persistentvolume, persistentvolumeclaim, pod, namespace, cluster_id)
  103. `, durStr, offStr)
  104. resChPVCAllocation := ctx.Query(queryPVCAllocation)
  105. queryPVCBytesRequested := fmt.Sprintf(`
  106. avg(
  107. avg_over_time(kube_persistentvolumeclaim_resource_requests_storage_bytes{}[%s]%s)
  108. ) by (persistentvolumeclaim, namespace, cluster_id)
  109. `, durStr, offStr)
  110. resChPVCBytesRequested := ctx.Query(queryPVCBytesRequested)
  111. queryPVCostPerGiBHour := fmt.Sprintf(`
  112. avg(
  113. avg_over_time(pv_hourly_cost[%s]%s)
  114. ) by (volumename, cluster_id)
  115. `, durStr, offStr)
  116. resChPVCostPerGiBHour := ctx.Query(queryPVCostPerGiBHour)
  117. queryPVCInfo := fmt.Sprintf(`
  118. avg(
  119. avg_over_time(kube_persistentvolumeclaim_info{volumename != ""}[%s]%s)
  120. ) by (persistentvolumeclaim, storageclass, volumename, namespace, cluster_id)
  121. `, durStr, offStr)
  122. resChPVCInfo := ctx.Query(queryPVCInfo)
  123. // TODO niko/cdmr
  124. // queryNetZoneRequests := fmt.Sprintf()
  125. // resChNetZoneRequests := ctx.Query(queryNetZoneRequests)
  126. // TODO niko/cdmr
  127. // queryNetRegionRequests := fmt.Sprintf()
  128. // resChNetRegionRequests := ctx.Query(queryNetRegionRequests)
  129. // TODO niko/cdmr
  130. // queryNetInternetRequests := fmt.Sprintf()
  131. // resChNetInternetRequests := ctx.Query(queryNetInternetRequests)
  132. // TODO niko/cdmr
  133. // queryNamespaceLabels := fmt.Sprintf()
  134. // resChNamespaceLabels := ctx.Query(queryNamespaceLabels)
  135. // TODO niko/cdmr
  136. // queryPodLabels := fmt.Sprintf()
  137. // resChPodLabels := ctx.Query(queryPodLabels)
  138. // TODO niko/cdmr
  139. // queryNamespaceAnnotations := fmt.Sprintf()
  140. // resChNamespaceAnnotations := ctx.Query(queryNamespaceAnnotations)
  141. // TODO niko/cdmr
  142. // queryPodAnnotations := fmt.Sprintf()
  143. // resChPodAnnotations := ctx.Query(queryPodAnnotations)
  144. // TODO niko/cdmr
  145. // queryServiceLabels := fmt.Sprintf()
  146. // resChServiceLabels := ctx.Query(queryServiceLabels)
  147. // TODO niko/cdmr
  148. // queryDeploymentLabels := fmt.Sprintf()
  149. // resChDeploymentLabels := ctx.Query(queryDeploymentLabels)
  150. // TODO niko/cdmr
  151. // queryStatefulSetLabels := fmt.Sprintf()
  152. // resChStatefulSetLabels := ctx.Query(queryStatefulSetLabels)
  153. // TODO niko/cdmr
  154. // queryJobLabels := fmt.Sprintf()
  155. // resChJobLabels := ctx.Query(queryJobLabels)
  156. // TODO niko/cdmr
  157. // queryDaemonSetLabels := fmt.Sprintf()
  158. // resChDaemonSetLabels := ctx.Query(queryDaemonSetLabels)
  159. resMinutes, _ := resChMinutes.Await()
  160. resCPUCoresAllocated, _ := resChCPUCoresAllocated.Await()
  161. resRAMBytesAllocated, _ := resChRAMBytesAllocated.Await()
  162. resGPUsRequested, _ := resChGPUsRequested.Await()
  163. resPVCAllocation, _ := resChPVCAllocation.Await()
  164. resPVCBytesRequested, _ := resChPVCBytesRequested.Await()
  165. resPVCostPerGiBHour, _ := resChPVCostPerGiBHour.Await()
  166. resPVCInfo, _ := resChPVCInfo.Await()
  167. // Build out a map of allocations, starting with (start, end) so that we
  168. // begin with minutes, from which we compute resource allocation and cost
  169. // totals from measured rate data.
  170. // TODO niko/cdmr can we start with a reasonable guess at map size?
  171. allocMap := map[containerKey]*kubecost.Allocation{}
  172. // TODO niko/cdmr comment
  173. podCount := map[podKey]int{}
  174. // clusterStarts and clusterEnds record the earliest start and latest end
  175. // times, respectively, on a cluster-basis. These are used for unmounted
  176. // PVs and other "virtual" Allocations so that minutes are maximally
  177. // accurate during start-up or spin-down of a cluster
  178. clusterStart := map[string]time.Time{}
  179. clusterEnd := map[string]time.Time{}
  180. for _, res := range resMinutes {
  181. if len(res.Values) == 0 {
  182. log.Warningf("CostModel.ComputeAllocation: empty minutes result")
  183. continue
  184. }
  185. cluster, err := res.GetString("cluster_id")
  186. if err != nil {
  187. cluster = env.GetClusterID()
  188. }
  189. labels, err := res.GetStrings("kubernetes_node", "namespace", "pod", "container")
  190. if err != nil {
  191. log.Warningf("CostModel.ComputeAllocation: minutes query result missing field: %s", err)
  192. continue
  193. }
  194. node := labels["kubernetes_node"]
  195. namespace := labels["namespace"]
  196. pod := labels["pod"]
  197. container := labels["container"]
  198. containerKey := newContainerKey(cluster, namespace, pod, container)
  199. podKey := newPodKey(cluster, namespace, pod)
  200. // allocStart is the timestamp of the first minute. We subtract 1m because
  201. // this point will actually represent the end of the first minute. We
  202. // don't subtract from end (timestamp of the last minute) because it's
  203. // already the end of the last minute, which is what we want.
  204. allocStart := time.Unix(int64(res.Values[0].Timestamp), 0).Add(-1 * time.Minute)
  205. if allocStart.Before(start) {
  206. log.Warningf("CostModel.ComputeAllocation: allocation %s measured start before window start: %s < %s", containerKey, allocStart, start)
  207. allocStart = start
  208. }
  209. allocEnd := time.Unix(int64(res.Values[len(res.Values)-1].Timestamp), 0)
  210. if allocEnd.After(end) {
  211. log.Warningf("CostModel.ComputeAllocation: allocation %s measured end before window end: %s < %s", containerKey, allocEnd, end)
  212. allocEnd = end
  213. }
  214. // TODO niko/cdmr "snap-to" start and end if within some epsilon of window start, end
  215. // Set start if unset or this datum's start time is earlier than the
  216. // current earliest time.
  217. if _, ok := clusterStart[cluster]; !ok || allocStart.Before(clusterStart[cluster]) {
  218. clusterStart[cluster] = allocStart
  219. }
  220. // Set end if unset or this datum's end time is later than the
  221. // current latest time.
  222. if _, ok := clusterEnd[cluster]; !ok || allocEnd.After(clusterEnd[cluster]) {
  223. clusterEnd[cluster] = allocEnd
  224. }
  225. name := fmt.Sprintf("%s/%s/%s/%s", cluster, namespace, pod, container)
  226. alloc := &kubecost.Allocation{
  227. Name: name,
  228. Start: allocStart,
  229. End: allocEnd,
  230. Window: window.Clone(),
  231. }
  232. props := kubecost.Properties{}
  233. props.SetContainer(container)
  234. props.SetPod(pod)
  235. props.SetNamespace(namespace)
  236. props.SetNode(node)
  237. props.SetCluster(cluster)
  238. allocMap[containerKey] = alloc
  239. podCount[podKey]++
  240. }
  241. for _, res := range resCPUCoresAllocated {
  242. // TODO niko/cdmr do we need node here?
  243. key, err := resultContainerKey(res, "cluster_id", "namespace", "pod", "container")
  244. if err != nil {
  245. log.Warningf("CostModel.ComputeAllocation: CPU allocation query result missing field: %s", err)
  246. continue
  247. }
  248. _, ok := allocMap[key]
  249. if !ok {
  250. log.Warningf("CostModel.ComputeAllocation: unidentified CPU allocation query result: %s", key)
  251. continue
  252. }
  253. cpuCores := res.Values[0].Value
  254. hours := allocMap[key].Minutes() / 60.0
  255. allocMap[key].CPUCoreHours = cpuCores * hours
  256. }
  257. for _, res := range resRAMBytesAllocated {
  258. // TODO niko/cdmr do we need node here?
  259. key, err := resultContainerKey(res, "cluster_id", "namespace", "pod", "container")
  260. if err != nil {
  261. log.Warningf("CostModel.ComputeAllocation: CPU allocation query result missing field: %s", err)
  262. continue
  263. }
  264. _, ok := allocMap[key]
  265. if !ok {
  266. log.Warningf("CostModel.ComputeAllocation: unidentified RAM allocation query result: %s", key)
  267. continue
  268. }
  269. ramBytes := res.Values[0].Value
  270. hours := allocMap[key].Minutes() / 60.0
  271. allocMap[key].RAMByteHours = ramBytes * hours
  272. }
  273. for _, res := range resGPUsRequested {
  274. // TODO niko/cdmr do we need node here?
  275. key, err := resultContainerKey(res, "cluster_id", "namespace", "pod", "container")
  276. if err != nil {
  277. log.Warningf("CostModel.ComputeAllocation: CPU allocation query result missing field: %s", err)
  278. continue
  279. }
  280. _, ok := allocMap[key]
  281. if !ok {
  282. log.Warningf("CostModel.ComputeAllocation: unidentified RAM allocation query result: %s", key)
  283. continue
  284. }
  285. // TODO niko/cdmr complete
  286. log.Infof("CostModel.ComputeAllocation: GPU results: %s=%f", key, res.Values[0].Value)
  287. }
  288. // TODO niko/cdmr comment
  289. pvMap := map[pvKey]*PV{}
  290. for _, res := range resPVCostPerGiBHour {
  291. // TODO niko/cdmr double-check "persistentvolume" vs "volumename"
  292. key, err := resultPVKey(res, "cluster_id", "persistentvolume")
  293. if err != nil {
  294. log.Warningf("CostModel.ComputeAllocation: PV cost per byte*hr query result missing field: %s", err)
  295. continue
  296. }
  297. if _, ok := pvMap[key]; !ok {
  298. log.Warningf("CostModel.ComputeAllocation: PV cost per byte*hr for unidentified PV: %s", key)
  299. continue
  300. }
  301. pvMap[key].CostPerGiBHour = res.Values[0].Value
  302. }
  303. // TODO niko/cdmr comment
  304. // pvcMap := map[pvcKey]*PVC{}
  305. for _, res := range resPVCBytesRequested {
  306. key, err := resultPVCKey(res, "cluster_id", "persistentvolumeclaim")
  307. if err != nil {
  308. log.Warningf("CostModel.ComputeAllocation: PV bytes requested query result missing field: %s", err)
  309. continue
  310. }
  311. // TODO niko/cdmr double-check "persistentvolume" vs "volumename"
  312. // pvc, err := res.GetString("persistentvolumeclaim")
  313. // if err != nil {
  314. // log.Warningf("CostModel.ComputeAllocation: PV bytes requested query result missing field: %s", err)
  315. // continue
  316. // }
  317. log.Infof("CostModel.ComputeAllocation: PVC: %s %fGiB", key, res.Values[0].Value/1024/1024/1024)
  318. // TODO niko/cdmr
  319. // pvcMap[key] = &PVC{
  320. // Bytes: res.Values[0].Value,
  321. // Name: pvc,
  322. // }
  323. }
  324. // TODO niko/cdmr comment
  325. podPVCMap := map[podKey][]*PVC{}
  326. for _, res := range resPVCAllocation {
  327. values, err := res.GetStrings("persistentvolume", "persistentvolumeclaim", "pod", "namespace", "cluster_id")
  328. if err != nil {
  329. log.Warningf("CostModel.ComputeAllocation: PVC allocation query result missing field: %s", err)
  330. continue
  331. }
  332. cluster := values["cluster_id"]
  333. namespace := values["namespace"]
  334. pod := values["pod"]
  335. name := values["persistentvolumeclaim"]
  336. volume := values["persistentvolume"]
  337. podKey := newPodKey(cluster, namespace, pod)
  338. pvKey := newPVKey(cluster, volume)
  339. if _, ok := pvMap[pvKey]; !ok {
  340. log.Warningf("CostModel.ComputeAllocation: PV missing for PVC allocation query result: %s", pvKey)
  341. continue
  342. }
  343. if _, ok := podPVCMap[podKey]; !ok {
  344. podPVCMap[podKey] = []*PVC{}
  345. }
  346. podPVCMap[podKey] = append(podPVCMap[podKey], &PVC{
  347. Bytes: res.Values[0].Value,
  348. Count: podCount[podKey],
  349. Name: name,
  350. Volume: pvMap[pvKey],
  351. })
  352. }
  353. for _, res := range resPVCInfo {
  354. values, err := res.GetStrings("persistentvolumeclaim", "storageclass", "volumename", "namespace", "cluster_id")
  355. if err != nil {
  356. log.Warningf("CostModel.ComputeAllocation: PVC allocation query result missing field: %s", err)
  357. continue
  358. }
  359. cluster := values["cluster_id"]
  360. // TODO niko/cdmr ?
  361. // namespace := values["namespace"]
  362. // name := values["persistentvolumeclaim"]
  363. volume := values["volumename"]
  364. storageClass := values["storageclass"]
  365. pvKey := newPVKey(cluster, volume)
  366. if _, ok := pvMap[pvKey]; !ok {
  367. log.Warningf("CostModel.ComputeAllocation: PV missing for PVC allocation query result: %s", pvKey)
  368. continue
  369. }
  370. pvMap[pvKey].StorageClass = storageClass
  371. }
  372. for _, pv := range pvMap {
  373. log.Infof("CostModel.ComputeAllocation: PV: %v", pv)
  374. }
  375. for pod, pvcs := range podPVCMap {
  376. for _, pvc := range pvcs {
  377. log.Infof("CostModel.ComputeAllocation: Pod %s: PVC: %v", pod, pvc)
  378. }
  379. }
  380. for _, alloc := range allocMap {
  381. // TODO niko/cdmr compute costs from resources and prices?
  382. cluster, _ := alloc.Properties.GetCluster()
  383. namespace, _ := alloc.Properties.GetNamespace()
  384. pod, _ := alloc.Properties.GetPod()
  385. podKey := newPodKey(cluster, namespace, pod)
  386. if pvcs, ok := podPVCMap[podKey]; ok {
  387. for _, pvc := range pvcs {
  388. // TODO niko/cdmr this isn't quite right... use PVC info query?
  389. hrs := alloc.Minutes() / 60.0
  390. gib := pvc.Bytes / 1024 / 1024 / 1024
  391. alloc.PVByteHours += pvc.Bytes * hrs
  392. alloc.PVCost += pvc.Volume.CostPerGiBHour * gib * hrs
  393. }
  394. }
  395. log.Infof("CostModel.ComputeAllocation: %s: %v", alloc.Name, alloc)
  396. allocSet.Set(alloc)
  397. }
  398. return allocSet, nil
  399. }
  400. // TODO niko/cdmr move to pkg/kubecost
  401. // TODO niko/cdmr add PersistenVolumeClaims to type Allocation?
  402. type PVC struct {
  403. Bytes float64 `json:"bytes"`
  404. Count int `json:"count"`
  405. Name string `json:"name"`
  406. Volume *PV `json:"persistentVolume"`
  407. }
  408. // TODO niko/cdmr move to pkg/kubecost
  409. type PV struct {
  410. Bytes float64 `json:"bytes"`
  411. CostPerGiBHour float64 `json:"costPerGiBHour"` // TODO niko/cdmr GiB or GB?
  412. Name string `json:"name"`
  413. StorageClass string `json:"storageClass"`
  414. }
  415. type containerKey struct {
  416. Cluster string
  417. Namespace string
  418. Pod string
  419. Container string
  420. }
  421. func (k containerKey) String() string {
  422. return fmt.Sprintf("%s/%s/%s/%s", k.Cluster, k.Namespace, k.Pod, k.Container)
  423. }
  424. func newContainerKey(cluster, namespace, pod, container string) containerKey {
  425. return containerKey{
  426. Cluster: cluster,
  427. Namespace: namespace,
  428. Pod: pod,
  429. Container: container,
  430. }
  431. }
  432. func resultContainerKey(res *prom.QueryResult, clusterLabel, namespaceLabel, podLabel, containerLabel string) (containerKey, error) {
  433. key := containerKey{}
  434. cluster, err := res.GetString(clusterLabel)
  435. if err != nil {
  436. cluster = env.GetClusterID()
  437. }
  438. key.Cluster = cluster
  439. namespace, err := res.GetString(namespaceLabel)
  440. if err != nil {
  441. return key, err
  442. }
  443. key.Namespace = namespace
  444. pod, err := res.GetString(podLabel)
  445. if err != nil {
  446. return key, err
  447. }
  448. key.Pod = pod
  449. container, err := res.GetString(containerLabel)
  450. if err != nil {
  451. return key, err
  452. }
  453. key.Container = container
  454. return key, nil
  455. }
  456. type podKey struct {
  457. Cluster string
  458. Namespace string
  459. Pod string
  460. }
  461. func (k podKey) String() string {
  462. return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.Pod)
  463. }
  464. func newPodKey(cluster, namespace, pod string) podKey {
  465. return podKey{
  466. Cluster: cluster,
  467. Namespace: namespace,
  468. Pod: pod,
  469. }
  470. }
  471. func resultPodKey(res *prom.QueryResult, clusterLabel, namespaceLabel, podLabel string) (podKey, error) {
  472. key := podKey{}
  473. cluster, err := res.GetString(clusterLabel)
  474. if err != nil {
  475. cluster = env.GetClusterID()
  476. }
  477. key.Cluster = cluster
  478. namespace, err := res.GetString(namespaceLabel)
  479. if err != nil {
  480. return key, err
  481. }
  482. key.Namespace = namespace
  483. pod, err := res.GetString(podLabel)
  484. if err != nil {
  485. return key, err
  486. }
  487. key.Pod = pod
  488. return key, nil
  489. }
  490. type pvcKey struct {
  491. Cluster string
  492. PersistentVolumeClaim string
  493. }
  494. func (k pvcKey) String() string {
  495. return fmt.Sprintf("%s/%s", k.Cluster, k.PersistentVolumeClaim)
  496. }
  497. func newPVCKey(cluster, persistentVolumeClaim string) pvcKey {
  498. return pvcKey{
  499. Cluster: cluster,
  500. PersistentVolumeClaim: persistentVolumeClaim,
  501. }
  502. }
  503. func resultPVCKey(res *prom.QueryResult, clusterLabel, pvcLabel string) (pvcKey, error) {
  504. key := pvcKey{}
  505. cluster, err := res.GetString(clusterLabel)
  506. if err != nil {
  507. cluster = env.GetClusterID()
  508. }
  509. key.Cluster = cluster
  510. pvc, err := res.GetString(pvcLabel)
  511. if err != nil {
  512. return key, err
  513. }
  514. key.PersistentVolumeClaim = pvc
  515. return key, nil
  516. }
  517. type pvKey struct {
  518. Cluster string
  519. PersistentVolume string
  520. }
  521. func (k pvKey) String() string {
  522. return fmt.Sprintf("%s/%s", k.Cluster, k.PersistentVolume)
  523. }
  524. func newPVKey(cluster, persistentVolume string) pvKey {
  525. return pvKey{
  526. Cluster: cluster,
  527. PersistentVolume: persistentVolume,
  528. }
  529. }
  530. func resultPVKey(res *prom.QueryResult, clusterLabel, persistentVolumeLabel string) (pvKey, error) {
  531. key := pvKey{}
  532. cluster, err := res.GetString(clusterLabel)
  533. if err != nil {
  534. cluster = env.GetClusterID()
  535. }
  536. key.Cluster = cluster
  537. persistentVolume, err := res.GetString(persistentVolumeLabel)
  538. if err != nil {
  539. return key, err
  540. }
  541. key.PersistentVolume = persistentVolume
  542. return key, nil
  543. }