main.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "net"
  7. "net/http"
  8. "os"
  9. "reflect"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "k8s.io/klog"
  14. "github.com/julienschmidt/httprouter"
  15. costAnalyzerCloud "github.com/kubecost/cost-model/cloud"
  16. costModel "github.com/kubecost/cost-model/costmodel"
  17. prometheusClient "github.com/prometheus/client_golang/api"
  18. prometheusAPI "github.com/prometheus/client_golang/api/prometheus/v1"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "github.com/prometheus/client_golang/prometheus"
  22. "github.com/prometheus/client_golang/prometheus/promhttp"
  23. "k8s.io/client-go/kubernetes"
  24. "k8s.io/client-go/rest"
  25. )
  26. const (
  27. prometheusServerEndpointEnvVar = "PROMETHEUS_SERVER_ENDPOINT"
  28. prometheusTroubleshootingEp = "http://docs.kubecost.com/custom-prom#troubleshoot"
  29. )
  30. var (
  31. // gitCommit is set by the build system
  32. gitCommit string
  33. )
  34. type Accesses struct {
  35. PrometheusClient prometheusClient.Client
  36. KubeClientSet kubernetes.Interface
  37. Cloud costAnalyzerCloud.Provider
  38. CPUPriceRecorder *prometheus.GaugeVec
  39. RAMPriceRecorder *prometheus.GaugeVec
  40. PersistentVolumePriceRecorder *prometheus.GaugeVec
  41. GPUPriceRecorder *prometheus.GaugeVec
  42. NodeTotalPriceRecorder *prometheus.GaugeVec
  43. RAMAllocationRecorder *prometheus.GaugeVec
  44. CPUAllocationRecorder *prometheus.GaugeVec
  45. GPUAllocationRecorder *prometheus.GaugeVec
  46. ContainerUptimeRecorder *prometheus.GaugeVec
  47. }
  48. type DataEnvelope struct {
  49. Code int `json:"code"`
  50. Status string `json:"status"`
  51. Data interface{} `json:"data"`
  52. Message string `json:"message,omitempty"`
  53. }
  54. func wrapData(data interface{}, err error) []byte {
  55. var resp []byte
  56. if err != nil {
  57. klog.V(1).Infof("Error returned to client: %s", err.Error())
  58. resp, _ = json.Marshal(&DataEnvelope{
  59. Code: 500,
  60. Status: "error",
  61. Message: err.Error(),
  62. Data: data,
  63. })
  64. } else {
  65. resp, _ = json.Marshal(&DataEnvelope{
  66. Code: 200,
  67. Status: "success",
  68. Data: data,
  69. })
  70. }
  71. return resp
  72. }
  73. // RefreshPricingData needs to be called when a new node joins the fleet, since we cache the relevant subsets of pricing data to avoid storing the whole thing.
  74. func (a *Accesses) RefreshPricingData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  75. w.Header().Set("Content-Type", "application/json")
  76. w.Header().Set("Access-Control-Allow-Origin", "*")
  77. err := a.Cloud.DownloadPricingData()
  78. w.Write(wrapData(nil, err))
  79. }
  80. func filterFields(fields string, data map[string]*costModel.CostData) map[string]costModel.CostData {
  81. fs := strings.Split(fields, ",")
  82. fmap := make(map[string]bool)
  83. for _, f := range fs {
  84. fieldNameLower := strings.ToLower(f) // convert to go struct name by uppercasing first letter
  85. klog.V(1).Infof("to delete: %s", fieldNameLower)
  86. fmap[fieldNameLower] = true
  87. }
  88. filteredData := make(map[string]costModel.CostData)
  89. for cname, costdata := range data {
  90. s := reflect.TypeOf(*costdata)
  91. val := reflect.ValueOf(*costdata)
  92. costdata2 := costModel.CostData{}
  93. cd2 := reflect.New(reflect.Indirect(reflect.ValueOf(costdata2)).Type()).Elem()
  94. n := s.NumField()
  95. for i := 0; i < n; i++ {
  96. field := s.Field(i)
  97. value := val.Field(i)
  98. value2 := cd2.Field(i)
  99. if _, ok := fmap[strings.ToLower(field.Name)]; !ok {
  100. value2.Set(reflect.Value(value))
  101. }
  102. }
  103. filteredData[cname] = cd2.Interface().(costModel.CostData)
  104. }
  105. return filteredData
  106. }
  107. func (a *Accesses) CostDataModel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  108. w.Header().Set("Content-Type", "application/json")
  109. w.Header().Set("Access-Control-Allow-Origin", "*")
  110. window := r.URL.Query().Get("timeWindow")
  111. offset := r.URL.Query().Get("offset")
  112. fields := r.URL.Query().Get("filterFields")
  113. namespace := r.URL.Query().Get("namespace")
  114. if offset != "" {
  115. offset = "offset " + offset
  116. }
  117. data, err := costModel.ComputeCostData(a.PrometheusClient, a.KubeClientSet, a.Cloud, window, offset, namespace)
  118. if fields != "" {
  119. filteredData := filterFields(fields, data)
  120. w.Write(wrapData(filteredData, err))
  121. } else {
  122. w.Write(wrapData(data, err))
  123. }
  124. }
  125. func (a *Accesses) ClusterCosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  126. w.Header().Set("Content-Type", "application/json")
  127. w.Header().Set("Access-Control-Allow-Origin", "*")
  128. window := r.URL.Query().Get("window")
  129. offset := r.URL.Query().Get("offset")
  130. if offset != "" {
  131. offset = "offset " + offset
  132. }
  133. data, err := costModel.ClusterCosts(a.PrometheusClient, a.Cloud, window, offset)
  134. w.Write(wrapData(data, err))
  135. }
  136. func (a *Accesses) ClusterCostsOverTime(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  137. w.Header().Set("Content-Type", "application/json")
  138. w.Header().Set("Access-Control-Allow-Origin", "*")
  139. start := r.URL.Query().Get("start")
  140. end := r.URL.Query().Get("end")
  141. window := r.URL.Query().Get("window")
  142. offset := r.URL.Query().Get("offset")
  143. if offset != "" {
  144. offset = "offset " + offset
  145. }
  146. data, err := costModel.ClusterCostsOverTime(a.PrometheusClient, a.Cloud, start, end, window, offset)
  147. w.Write(wrapData(data, err))
  148. }
  149. func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  150. w.Header().Set("Content-Type", "application/json")
  151. w.Header().Set("Access-Control-Allow-Origin", "*")
  152. start := r.URL.Query().Get("start")
  153. end := r.URL.Query().Get("end")
  154. window := r.URL.Query().Get("window")
  155. fields := r.URL.Query().Get("filterFields")
  156. namespace := r.URL.Query().Get("namespace")
  157. data, err := costModel.ComputeCostDataRange(a.PrometheusClient, a.KubeClientSet, a.Cloud, start, end, window, namespace)
  158. if fields != "" {
  159. filteredData := filterFields(fields, data)
  160. w.Write(wrapData(filteredData, err))
  161. } else {
  162. w.Write(wrapData(data, err))
  163. }
  164. }
  165. func (a *Accesses) OutofClusterCosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  166. w.Header().Set("Content-Type", "application/json")
  167. w.Header().Set("Access-Control-Allow-Origin", "*")
  168. start := r.URL.Query().Get("start")
  169. end := r.URL.Query().Get("end")
  170. aggregator := r.URL.Query().Get("aggregator")
  171. data, err := a.Cloud.ExternalAllocations(start, end, aggregator)
  172. w.Write(wrapData(data, err))
  173. }
  174. func (p *Accesses) GetAllNodePricing(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  175. w.Header().Set("Content-Type", "application/json")
  176. w.Header().Set("Access-Control-Allow-Origin", "*")
  177. data, err := p.Cloud.AllNodePricing()
  178. w.Write(wrapData(data, err))
  179. }
  180. func (p *Accesses) GetConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  181. w.Header().Set("Content-Type", "application/json")
  182. w.Header().Set("Access-Control-Allow-Origin", "*")
  183. data, err := p.Cloud.GetConfig()
  184. w.Write(wrapData(data, err))
  185. }
  186. func (p *Accesses) UpdateSpotInfoConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  187. w.Header().Set("Content-Type", "application/json")
  188. w.Header().Set("Access-Control-Allow-Origin", "*")
  189. data, err := p.Cloud.UpdateConfig(r.Body, costAnalyzerCloud.SpotInfoUpdateType)
  190. if err != nil {
  191. w.Write(wrapData(data, err))
  192. return
  193. }
  194. w.Write(wrapData(data, err))
  195. err = p.Cloud.DownloadPricingData()
  196. if err != nil {
  197. klog.V(1).Infof("Error redownloading data on config update: %s", err.Error())
  198. }
  199. return
  200. }
  201. func (p *Accesses) UpdateAthenaInfoConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  202. w.Header().Set("Content-Type", "application/json")
  203. w.Header().Set("Access-Control-Allow-Origin", "*")
  204. data, err := p.Cloud.UpdateConfig(r.Body, costAnalyzerCloud.AthenaInfoUpdateType)
  205. if err != nil {
  206. w.Write(wrapData(data, err))
  207. return
  208. }
  209. w.Write(wrapData(data, err))
  210. return
  211. }
  212. func (p *Accesses) UpdateBigQueryInfoConfigs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  213. w.Header().Set("Content-Type", "application/json")
  214. w.Header().Set("Access-Control-Allow-Origin", "*")
  215. data, err := p.Cloud.UpdateConfig(r.Body, costAnalyzerCloud.BigqueryUpdateType)
  216. if err != nil {
  217. w.Write(wrapData(data, err))
  218. return
  219. }
  220. w.Write(wrapData(data, err))
  221. return
  222. }
  223. func (p *Accesses) UpdateConfigByKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  224. w.Header().Set("Content-Type", "application/json")
  225. w.Header().Set("Access-Control-Allow-Origin", "*")
  226. data, err := p.Cloud.UpdateConfig(r.Body, "")
  227. if err != nil {
  228. w.Write(wrapData(data, err))
  229. return
  230. }
  231. w.Write(wrapData(data, err))
  232. return
  233. }
  234. func (p *Accesses) ManagementPlatform(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  235. w.Header().Set("Content-Type", "application/json")
  236. w.Header().Set("Access-Control-Allow-Origin", "*")
  237. data, err := p.Cloud.GetManagementPlatform()
  238. if err != nil {
  239. w.Write(wrapData(data, err))
  240. return
  241. }
  242. w.Write(wrapData(data, err))
  243. return
  244. }
  245. func (p *Accesses) ClusterInfo(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  246. w.Header().Set("Content-Type", "application/json")
  247. w.Header().Set("Access-Control-Allow-Origin", "*")
  248. data, err := p.Cloud.ClusterInfo()
  249. w.Write(wrapData(data, err))
  250. }
  251. func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  252. w.WriteHeader(200)
  253. w.Header().Set("Content-Length", "0")
  254. w.Header().Set("Content-Type", "text/plain")
  255. }
  256. func (p *Accesses) GetPrometheusMetadata(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  257. w.Header().Set("Content-Type", "application/json")
  258. w.Header().Set("Access-Control-Allow-Origin", "*")
  259. w.Write(wrapData(costModel.ValidatePrometheus(p.PrometheusClient)))
  260. }
  261. func (p *Accesses) ContainerUptimes(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  262. w.Header().Set("Content-Type", "application/json")
  263. w.Header().Set("Access-Control-Allow-Origin", "*")
  264. res, err := costModel.ComputeUptimes(p.PrometheusClient)
  265. w.Write(wrapData(res, err))
  266. }
  267. func (a *Accesses) recordPrices() {
  268. go func() {
  269. for {
  270. klog.V(4).Info("Recording prices...")
  271. data, err := costModel.ComputeCostData(a.PrometheusClient, a.KubeClientSet, a.Cloud, "2m", "", "")
  272. if err != nil {
  273. klog.V(1).Info("Error in price recording: " + err.Error())
  274. // zero the for loop so the time.Sleep will still work
  275. data = map[string]*costModel.CostData{}
  276. }
  277. for _, costs := range data {
  278. nodeName := costs.NodeName
  279. node := costs.NodeData
  280. if node == nil {
  281. klog.V(4).Infof("Skipping Node \"%s\" due to missing Node Data costs", nodeName)
  282. continue
  283. }
  284. cpuCost, _ := strconv.ParseFloat(node.VCPUCost, 64)
  285. cpu, _ := strconv.ParseFloat(node.VCPU, 64)
  286. ramCost, _ := strconv.ParseFloat(node.RAMCost, 64)
  287. ram, _ := strconv.ParseFloat(node.RAMBytes, 64)
  288. gpu, _ := strconv.ParseFloat(node.GPU, 64)
  289. gpuCost, _ := strconv.ParseFloat(node.GPUCost, 64)
  290. totalCost := cpu*cpuCost + ramCost*(ram/1024/1024/1024) + gpu*gpuCost
  291. if costs.PVCData != nil {
  292. for _, pvc := range costs.PVCData {
  293. pvCost, _ := strconv.ParseFloat(pvc.Volume.Cost, 64)
  294. a.PersistentVolumePriceRecorder.WithLabelValues(pvc.VolumeName, pvc.VolumeName).Set(pvCost)
  295. }
  296. }
  297. a.CPUPriceRecorder.WithLabelValues(nodeName, nodeName).Set(cpuCost)
  298. a.RAMPriceRecorder.WithLabelValues(nodeName, nodeName).Set(ramCost)
  299. a.GPUPriceRecorder.WithLabelValues(nodeName, nodeName).Set(gpuCost)
  300. a.NodeTotalPriceRecorder.WithLabelValues(nodeName, nodeName).Set(totalCost)
  301. namespace := costs.Namespace
  302. podName := costs.PodName
  303. containerName := costs.Name
  304. if len(costs.RAMAllocation) > 0 {
  305. a.RAMAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.RAMAllocation[0].Value)
  306. }
  307. if len(costs.CPUAllocation) > 0 {
  308. a.CPUAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.CPUAllocation[0].Value)
  309. }
  310. if len(costs.GPUReq) > 0 {
  311. // allocation here is set to the request because shared GPU usage not yet supported.
  312. a.GPUAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.GPUReq[0].Value)
  313. }
  314. storageClasses, _ := a.KubeClientSet.StorageV1().StorageClasses().List(metav1.ListOptions{})
  315. storageClassMap := make(map[string]map[string]string)
  316. for _, storageClass := range storageClasses.Items {
  317. params := storageClass.Parameters
  318. storageClassMap[storageClass.ObjectMeta.Name] = params
  319. }
  320. pvs, _ := a.KubeClientSet.CoreV1().PersistentVolumes().List(metav1.ListOptions{})
  321. for _, pv := range pvs.Items {
  322. parameters, ok := storageClassMap[pv.Spec.StorageClassName]
  323. if !ok {
  324. klog.V(4).Infof("Unable to find parameters for storage class \"%s\". Does pv \"%s\" have a storageClassName?", pv.Spec.StorageClassName, pv.Name)
  325. }
  326. cacPv := &costAnalyzerCloud.PV{
  327. Class: pv.Spec.StorageClassName,
  328. Region: pv.Labels[v1.LabelZoneRegion],
  329. Parameters: parameters,
  330. }
  331. costModel.GetPVCost(cacPv, &pv, a.Cloud)
  332. c, _ := strconv.ParseFloat(cacPv.Cost, 64)
  333. a.PersistentVolumePriceRecorder.WithLabelValues(pv.Name, pv.Name).Set(c)
  334. }
  335. containerUptime, _ := costModel.ComputeUptimes(a.PrometheusClient)
  336. for key, uptime := range containerUptime {
  337. container, _ := costModel.NewContainerMetricFromKey(key)
  338. a.ContainerUptimeRecorder.WithLabelValues(container.Namespace, container.PodName, container.ContainerName).Set(uptime)
  339. }
  340. }
  341. time.Sleep(time.Minute)
  342. }
  343. }()
  344. }
  345. func main() {
  346. klog.InitFlags(nil)
  347. flag.Set("v", "3")
  348. flag.Parse()
  349. klog.V(1).Infof("Starting cost-model (git commit \"%s\")", gitCommit)
  350. address := os.Getenv(prometheusServerEndpointEnvVar)
  351. if address == "" {
  352. klog.Fatalf("No address for prometheus set in $%s. Aborting.", prometheusServerEndpointEnvVar)
  353. }
  354. var LongTimeoutRoundTripper http.RoundTripper = &http.Transport{ // may be necessary for long prometheus queries. TODO: make this configurable
  355. Proxy: http.ProxyFromEnvironment,
  356. DialContext: (&net.Dialer{
  357. Timeout: 120 * time.Second,
  358. KeepAlive: 120 * time.Second,
  359. }).DialContext,
  360. TLSHandshakeTimeout: 10 * time.Second,
  361. }
  362. pc := prometheusClient.Config{
  363. Address: address,
  364. RoundTripper: LongTimeoutRoundTripper,
  365. }
  366. promCli, _ := prometheusClient.NewClient(pc)
  367. api := prometheusAPI.NewAPI(promCli)
  368. _, err := api.Config(context.Background())
  369. if err != nil {
  370. klog.Fatalf("No valid prometheus config file at %s. Error: %s . Troubleshooting help available at: %s", address, err.Error(), prometheusTroubleshootingEp)
  371. }
  372. klog.V(1).Info("Success: retrieved a prometheus config file from: " + address)
  373. _, err = costModel.ValidatePrometheus(promCli)
  374. if err != nil {
  375. klog.Fatalf("Failed to query prometheus at %s. Error: %s . Troubleshooting help available at: %s", address, err.Error(), prometheusTroubleshootingEp)
  376. }
  377. klog.V(1).Info("Success: retrieved the 'up' query against prometheus at: " + address)
  378. // Kubernetes API setup
  379. kc, err := rest.InClusterConfig()
  380. if err != nil {
  381. panic(err.Error())
  382. }
  383. kubeClientset, err := kubernetes.NewForConfig(kc)
  384. if err != nil {
  385. panic(err.Error())
  386. }
  387. cloudProviderKey := os.Getenv("CLOUD_PROVIDER_API_KEY")
  388. cloudProvider, err := costAnalyzerCloud.NewProvider(kubeClientset, cloudProviderKey)
  389. if err != nil {
  390. panic(err.Error())
  391. }
  392. cpuGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  393. Name: "node_cpu_hourly_cost",
  394. Help: "node_cpu_hourly_cost hourly cost for each cpu on this node",
  395. }, []string{"instance", "node"})
  396. ramGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  397. Name: "node_ram_hourly_cost",
  398. Help: "node_ram_hourly_cost hourly cost for each gb of ram on this node",
  399. }, []string{"instance", "node"})
  400. gpuGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  401. Name: "node_gpu_hourly_cost",
  402. Help: "node_gpu_hourly_cost hourly cost for each gpu on this node",
  403. }, []string{"instance", "node"})
  404. totalGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  405. Name: "node_total_hourly_cost",
  406. Help: "node_total_hourly_cost Total node cost per hour",
  407. }, []string{"instance", "node"})
  408. pvGv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  409. Name: "pv_hourly_cost",
  410. Help: "pv_hourly_cost Cost per GB per hour on a persistent disk",
  411. }, []string{"volumename", "persistentvolume"})
  412. RAMAllocation := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  413. Name: "container_memory_allocation_bytes",
  414. Help: "container_memory_allocation_bytes Bytes of RAM used",
  415. }, []string{"namespace", "pod", "container", "instance", "node"})
  416. CPUAllocation := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  417. Name: "container_cpu_allocation",
  418. Help: "container_cpu_allocation Percent of a single CPU used in a minute",
  419. }, []string{"namespace", "pod", "container", "instance", "node"})
  420. GPUAllocation := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  421. Name: "container_gpu_allocation",
  422. Help: "container_gpu_allocation GPU used",
  423. }, []string{"namespace", "pod", "container", "instance", "node"})
  424. ContainerUptimeRecorder := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  425. Name: "container_uptime_seconds",
  426. Help: "container_uptime_seconds Seconds a container has been running",
  427. }, []string{"namespace", "pod", "container"})
  428. prometheus.MustRegister(cpuGv)
  429. prometheus.MustRegister(ramGv)
  430. prometheus.MustRegister(gpuGv)
  431. prometheus.MustRegister(totalGv)
  432. prometheus.MustRegister(pvGv)
  433. prometheus.MustRegister(RAMAllocation)
  434. prometheus.MustRegister(CPUAllocation)
  435. prometheus.MustRegister(ContainerUptimeRecorder)
  436. a := Accesses{
  437. PrometheusClient: promCli,
  438. KubeClientSet: kubeClientset,
  439. Cloud: cloudProvider,
  440. CPUPriceRecorder: cpuGv,
  441. RAMPriceRecorder: ramGv,
  442. GPUPriceRecorder: gpuGv,
  443. NodeTotalPriceRecorder: totalGv,
  444. RAMAllocationRecorder: RAMAllocation,
  445. CPUAllocationRecorder: CPUAllocation,
  446. GPUAllocationRecorder: GPUAllocation,
  447. ContainerUptimeRecorder: ContainerUptimeRecorder,
  448. PersistentVolumePriceRecorder: pvGv,
  449. }
  450. err = a.Cloud.DownloadPricingData()
  451. if err != nil {
  452. klog.V(1).Info("Failed to download pricing data: " + err.Error())
  453. }
  454. a.recordPrices()
  455. router := httprouter.New()
  456. router.GET("/costDataModel", a.CostDataModel)
  457. router.GET("/costDataModelRange", a.CostDataModelRange)
  458. router.GET("/outOfClusterCosts", a.OutofClusterCosts)
  459. router.GET("/allNodePricing", a.GetAllNodePricing)
  460. router.GET("/healthz", Healthz)
  461. router.GET("/getConfigs", a.GetConfigs)
  462. router.POST("/refreshPricing", a.RefreshPricingData)
  463. router.POST("/updateSpotInfoConfigs", a.UpdateSpotInfoConfigs)
  464. router.POST("/updateAthenaInfoConfigs", a.UpdateAthenaInfoConfigs)
  465. router.POST("/updateBigQueryInfoConfigs", a.UpdateBigQueryInfoConfigs)
  466. router.POST("/updateConfigByKey", a.UpdateConfigByKey)
  467. router.GET("/clusterCostsOverTime", a.ClusterCostsOverTime)
  468. router.GET("/clusterCosts", a.ClusterCosts)
  469. router.GET("/validatePrometheus", a.GetPrometheusMetadata)
  470. router.GET("/managementPlatform", a.ManagementPlatform)
  471. router.GET("/clusterInfo", a.ClusterInfo)
  472. router.GET("/containerUptimes", a.ContainerUptimes)
  473. rootMux := http.NewServeMux()
  474. rootMux.Handle("/", router)
  475. rootMux.Handle("/metrics", promhttp.Handler())
  476. klog.Fatal(http.ListenAndServe(":9003", rootMux))
  477. }