allocation.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. queryPVBytesRequested := 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. resChPVBytesRequested := ctx.Query(queryPVBytesRequested)
  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. resPVBytesRequested, _ := resChPVBytesRequested.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. values, err := res.GetStrings("cluster_id", "kubernetes_node", "namespace", "pod", "container")
  186. if err != nil {
  187. log.Warningf("CostModel.ComputeAllocation: minutes query result missing field: %s", err)
  188. continue
  189. }
  190. cluster := values["cluster_id"]
  191. node := values["kubernetes_node"]
  192. namespace := values["namespace"]
  193. pod := values["pod"]
  194. container := values["container"]
  195. containerKey := newContainerKey(cluster, namespace, pod, container)
  196. podKey := newPodKey(cluster, namespace, pod)
  197. // allocStart is the timestamp of the first minute. We subtract 1m because
  198. // this point will actually represent the end of the first minute. We
  199. // don't subtract from end (timestamp of the last minute) because it's
  200. // already the end of the last minute, which is what we want.
  201. allocStart := time.Unix(int64(res.Values[0].Timestamp), 0).Add(-1 * time.Minute)
  202. if allocStart.Before(start) {
  203. log.Warningf("CostModel.ComputeAllocation: allocation %s measured start before window start: %s < %s", containerKey, allocStart, start)
  204. allocStart = start
  205. }
  206. allocEnd := time.Unix(int64(res.Values[len(res.Values)-1].Timestamp), 0)
  207. if allocEnd.After(end) {
  208. log.Warningf("CostModel.ComputeAllocation: allocation %s measured end before window end: %s < %s", containerKey, allocEnd, end)
  209. allocEnd = end
  210. }
  211. // TODO niko/cdmr "snap-to" start and end if within some epsilon of window start, end
  212. // Set start if unset or this datum's start time is earlier than the
  213. // current earliest time.
  214. if _, ok := clusterStart[cluster]; !ok || allocStart.Before(clusterStart[cluster]) {
  215. clusterStart[cluster] = allocStart
  216. }
  217. // Set end if unset or this datum's end time is later than the
  218. // current latest time.
  219. if _, ok := clusterEnd[cluster]; !ok || allocEnd.After(clusterEnd[cluster]) {
  220. clusterEnd[cluster] = allocEnd
  221. }
  222. name := fmt.Sprintf("%s/%s/%s/%s", cluster, namespace, pod, container)
  223. alloc := &kubecost.Allocation{
  224. Name: name,
  225. Start: allocStart,
  226. End: allocEnd,
  227. Window: window.Clone(),
  228. }
  229. props := kubecost.Properties{}
  230. props.SetContainer(container)
  231. props.SetPod(pod)
  232. props.SetNamespace(namespace)
  233. props.SetNode(node)
  234. props.SetCluster(cluster)
  235. allocMap[containerKey] = alloc
  236. podCount[podKey]++
  237. }
  238. for _, res := range resCPUCoresAllocated {
  239. // TODO niko/cdmr do we need node here?
  240. key, err := resultContainerKey(res, "cluster", "namespace", "pod", "container")
  241. if err != nil {
  242. log.Warningf("CostModel.ComputeAllocation: CPU allocation query result missing field: %s", err)
  243. continue
  244. }
  245. _, ok := allocMap[key]
  246. if !ok {
  247. log.Warningf("CostModel.ComputeAllocation: unidentified CPU allocation query result: %s", key)
  248. continue
  249. }
  250. cpuCores := res.Values[0].Value
  251. hours := allocMap[key].Minutes() / 60.0
  252. allocMap[key].CPUCoreHours = cpuCores * hours
  253. }
  254. for _, res := range resRAMBytesAllocated {
  255. // TODO niko/cdmr do we need node here?
  256. key, err := resultContainerKey(res, "cluster", "namespace", "pod", "container")
  257. if err != nil {
  258. log.Warningf("CostModel.ComputeAllocation: CPU allocation query result missing field: %s", err)
  259. continue
  260. }
  261. _, ok := allocMap[key]
  262. if !ok {
  263. log.Warningf("CostModel.ComputeAllocation: unidentified RAM allocation query result: %s", key)
  264. continue
  265. }
  266. ramBytes := res.Values[0].Value
  267. hours := allocMap[key].Minutes() / 60.0
  268. allocMap[key].RAMByteHours = ramBytes * hours
  269. }
  270. for _, res := range resGPUsRequested {
  271. // TODO niko/cdmr do we need node here?
  272. key, err := resultContainerKey(res, "cluster", "namespace", "pod", "container")
  273. if err != nil {
  274. log.Warningf("CostModel.ComputeAllocation: CPU allocation query result missing field: %s", err)
  275. continue
  276. }
  277. _, ok := allocMap[key]
  278. if !ok {
  279. log.Warningf("CostModel.ComputeAllocation: unidentified RAM allocation query result: %s", key)
  280. continue
  281. }
  282. // TODO niko/cdmr complete
  283. log.Infof("CostModel.ComputeAllocation: GPU results: %s=%f", key, res.Values[0].Value)
  284. }
  285. // TODO niko/cdmr comment
  286. pvMap := map[pvKey]*PV{}
  287. for _, res := range resPVBytesRequested {
  288. // TODO niko/cdmr double-check "persistentvolume" vs "volumename"
  289. key, err := resultPVKey(res, "cluster_id", "persistentvolume")
  290. if err != nil {
  291. log.Warningf("CostModel.ComputeAllocation: PV bytes requested query result missing field: %s", err)
  292. continue
  293. }
  294. // TODO niko/cdmr double-check "persistentvolume" vs "volumename"
  295. name, err := res.GetString("persistentvolume")
  296. if err != nil {
  297. log.Warningf("CostModel.ComputeAllocation: PV bytes requested query result missing field: %s", err)
  298. continue
  299. }
  300. pvMap[key] = &PV{
  301. Bytes: res.Values[0].Value,
  302. Name: name,
  303. }
  304. }
  305. for _, res := range resPVCostPerGiBHour {
  306. // TODO niko/cdmr double-check "persistentvolume" vs "volumename"
  307. key, err := resultPVKey(res, "cluster_id", "persistentvolume")
  308. if err != nil {
  309. log.Warningf("CostModel.ComputeAllocation: PV cost per byte*hr query result missing field: %s", err)
  310. continue
  311. }
  312. if _, ok := pvMap[key]; !ok {
  313. log.Warningf("CostModel.ComputeAllocation: PV cost per byte*hr for unidentified PV: %s", key)
  314. continue
  315. }
  316. pvMap[key].CostPerGiBHour = res.Values[0].Value
  317. }
  318. // TODO niko/cdmr comment
  319. pvcMap := map[podKey][]*PVC{}
  320. for _, res := range resPVCAllocation {
  321. values, err := res.GetStrings("persistentvolume", "persistentvolumeclaim", "pod", "namespace", "cluster_id")
  322. if err != nil {
  323. log.Warningf("CostModel.ComputeAllocation: PVC allocation query result missing field: %s", err)
  324. continue
  325. }
  326. cluster := values["cluster_id"]
  327. namespace := values["namespace"]
  328. pod := values["pod"]
  329. name := values["persistentvolumeclaim"]
  330. volume := values["persistentvolume"]
  331. podKey := newPodKey(cluster, namespace, pod)
  332. pvKey := newPVKey(cluster, volume)
  333. if _, ok := pvMap[pvKey]; !ok {
  334. log.Warningf("CostModel.ComputeAllocation: PV missing for PVC allocation query result: %s", pvKey)
  335. continue
  336. }
  337. if _, ok := pvcMap[podKey]; !ok {
  338. pvcMap[podKey] = []*PVC{}
  339. }
  340. pvcMap[podKey] = append(pvcMap[podKey], &PVC{
  341. Bytes: res.Values[0].Value,
  342. Count: podCount[podKey],
  343. Name: name,
  344. Volume: pvMap[pvKey],
  345. })
  346. }
  347. for _, res := range resPVCInfo {
  348. values, err := res.GetStrings("persistentvolumeclaim", "storageclass", "volumename", "namespace", "cluster_id")
  349. if err != nil {
  350. log.Warningf("CostModel.ComputeAllocation: PVC allocation query result missing field: %s", err)
  351. continue
  352. }
  353. cluster := values["cluster_id"]
  354. // TODO niko/cdmr ?
  355. // namespace := values["namespace"]
  356. // name := values["persistentvolumeclaim"]
  357. volume := values["volumename"]
  358. storageClass := values["storageclass"]
  359. pvKey := newPVKey(cluster, volume)
  360. if _, ok := pvMap[pvKey]; !ok {
  361. log.Warningf("CostModel.ComputeAllocation: PV missing for PVC allocation query result: %s", pvKey)
  362. continue
  363. }
  364. pvMap[pvKey].StorageClass = storageClass
  365. }
  366. for _, pv := range pvMap {
  367. log.Infof("CostModel.ComputeAllocation: PV: %v", pv)
  368. }
  369. for _, pvc := range pvcMap {
  370. log.Infof("CostModel.ComputeAllocation: PVC: %v", pvc)
  371. }
  372. for _, alloc := range allocMap {
  373. // TODO niko/cdmr compute costs from resources and prices?
  374. cluster, _ := alloc.Properties.GetCluster()
  375. namespace, _ := alloc.Properties.GetNamespace()
  376. pod, _ := alloc.Properties.GetPod()
  377. podKey := newPodKey(cluster, namespace, pod)
  378. if pvcs, ok := pvcMap[podKey]; ok {
  379. for _, pvc := range pvcs {
  380. // TODO niko/cdmr this isn't quite right... use PVC info query?
  381. hrs := alloc.Minutes() / 60.0
  382. gib := pvc.Bytes / 1024 / 1024 / 1024
  383. alloc.PVByteHours += pvc.Bytes * hrs
  384. alloc.PVCost += pvc.Volume.CostPerGiBHour * gib * hrs
  385. }
  386. }
  387. log.Infof("CostModel.ComputeAllocation: %s: %v", alloc.Name, alloc)
  388. allocSet.Set(alloc)
  389. }
  390. return allocSet, nil
  391. }
  392. // TODO niko/cdmr move to pkg/kubecost
  393. // TODO niko/cdmr add PersistenVolumeClaims to type Allocation?
  394. type PVC struct {
  395. Bytes float64 `json:"bytes"`
  396. Count int `json:"count"`
  397. Name string `json:"name"`
  398. Volume *PV `json:"persistentVolume"`
  399. }
  400. // TODO niko/cdmr move to pkg/kubecost
  401. type PV struct {
  402. Bytes float64 `json:"bytes"`
  403. CostPerGiBHour float64 `json:"costPerGiBHour"` // TODO niko/cdmr GiB or GB?
  404. Name string `json:"name"`
  405. StorageClass string `json:"storageClass"`
  406. }
  407. type containerKey struct {
  408. Cluster string
  409. Namespace string
  410. Pod string
  411. Container string
  412. }
  413. func (k containerKey) String() string {
  414. return fmt.Sprintf("%s/%s/%s/%s", k.Cluster, k.Namespace, k.Pod, k.Container)
  415. }
  416. func newContainerKey(cluster, namespace, pod, container string) containerKey {
  417. return containerKey{
  418. Cluster: cluster,
  419. Namespace: namespace,
  420. Pod: pod,
  421. Container: container,
  422. }
  423. }
  424. func resultContainerKey(res *prom.QueryResult, clusterLabel, namespaceLabel, podLabel, containerLabel string) (containerKey, error) {
  425. key := containerKey{}
  426. cluster, err := res.GetString(clusterLabel)
  427. if err != nil {
  428. cluster = env.GetClusterID()
  429. }
  430. key.Cluster = cluster
  431. namespace, err := res.GetString(namespaceLabel)
  432. if err != nil {
  433. return key, err
  434. }
  435. key.Namespace = namespace
  436. pod, err := res.GetString(podLabel)
  437. if err != nil {
  438. return key, err
  439. }
  440. key.Pod = pod
  441. container, err := res.GetString(containerLabel)
  442. if err != nil {
  443. return key, err
  444. }
  445. key.Container = container
  446. return key, nil
  447. }
  448. type podKey struct {
  449. Cluster string
  450. Namespace string
  451. Pod string
  452. }
  453. func (k podKey) String() string {
  454. return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.Pod)
  455. }
  456. func newPodKey(cluster, namespace, pod string) podKey {
  457. return podKey{
  458. Cluster: cluster,
  459. Namespace: namespace,
  460. Pod: pod,
  461. }
  462. }
  463. func resultPodKey(res *prom.QueryResult, clusterLabel, namespaceLabel, podLabel string) (podKey, error) {
  464. key := podKey{}
  465. cluster, err := res.GetString(clusterLabel)
  466. if err != nil {
  467. cluster = env.GetClusterID()
  468. }
  469. key.Cluster = cluster
  470. namespace, err := res.GetString(namespaceLabel)
  471. if err != nil {
  472. return key, err
  473. }
  474. key.Namespace = namespace
  475. pod, err := res.GetString(podLabel)
  476. if err != nil {
  477. return key, err
  478. }
  479. key.Pod = pod
  480. return key, nil
  481. }
  482. type pvKey struct {
  483. Cluster string
  484. Name string
  485. }
  486. func (k pvKey) String() string {
  487. return fmt.Sprintf("%s/%s", k.Cluster, k.Name)
  488. }
  489. func newPVKey(cluster, name string) pvKey {
  490. return pvKey{
  491. Cluster: cluster,
  492. Name: name,
  493. }
  494. }
  495. func resultPVKey(res *prom.QueryResult, clusterLabel, nameLabel string) (pvKey, error) {
  496. key := pvKey{}
  497. cluster, err := res.GetString(clusterLabel)
  498. if err != nil {
  499. cluster = env.GetClusterID()
  500. }
  501. key.Cluster = cluster
  502. name, err := res.GetString(nameLabel)
  503. if err != nil {
  504. return key, err
  505. }
  506. key.Name = name
  507. return key, nil
  508. }