router.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package costmodel
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "reflect"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "github.com/opencost/opencost/core/pkg/kubeconfig"
  14. "github.com/opencost/opencost/core/pkg/nodestats"
  15. "github.com/opencost/opencost/core/pkg/protocol"
  16. "github.com/opencost/opencost/core/pkg/source"
  17. "github.com/opencost/opencost/core/pkg/storage"
  18. "github.com/opencost/opencost/core/pkg/util/retry"
  19. "github.com/opencost/opencost/core/pkg/util/timeutil"
  20. "github.com/opencost/opencost/core/pkg/version"
  21. cloudconfig "github.com/opencost/opencost/pkg/cloud/config"
  22. "github.com/opencost/opencost/pkg/cloud/provider"
  23. "github.com/opencost/opencost/pkg/cloudcost"
  24. "github.com/opencost/opencost/pkg/config"
  25. "github.com/opencost/opencost/pkg/customcost"
  26. "github.com/opencost/opencost/pkg/metrics"
  27. "github.com/opencost/opencost/pkg/util/watcher"
  28. "github.com/julienschmidt/httprouter"
  29. "github.com/opencost/opencost/core/pkg/clustercache"
  30. "github.com/opencost/opencost/core/pkg/clusters"
  31. sysenv "github.com/opencost/opencost/core/pkg/env"
  32. "github.com/opencost/opencost/core/pkg/log"
  33. "github.com/opencost/opencost/core/pkg/util/json"
  34. "github.com/opencost/opencost/modules/collector-source/pkg/collector"
  35. "github.com/opencost/opencost/modules/prometheus-source/pkg/prom"
  36. "github.com/opencost/opencost/pkg/cloud/models"
  37. clusterc "github.com/opencost/opencost/pkg/clustercache"
  38. "github.com/opencost/opencost/pkg/env"
  39. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  40. "github.com/patrickmn/go-cache"
  41. "k8s.io/client-go/kubernetes"
  42. )
  43. const (
  44. RFC3339Milli = "2006-01-02T15:04:05.000Z"
  45. CustomPricingSetting = "CustomPricing"
  46. DiscountSetting = "Discount"
  47. )
  48. var (
  49. // gitCommit is set by the build system
  50. gitCommit string
  51. proto = protocol.HTTP()
  52. )
  53. // Accesses defines a singleton application instance, providing access to
  54. // Prometheus, Kubernetes, the cloud provider, and caches.
  55. type Accesses struct {
  56. DataSource source.OpenCostDataSource
  57. KubeClientSet kubernetes.Interface
  58. ClusterCache clustercache.ClusterCache
  59. ClusterMap clusters.ClusterMap
  60. CloudProvider models.Provider
  61. ConfigFileManager *config.ConfigFileManager
  62. ClusterInfoProvider clusters.ClusterInfoProvider
  63. Model *CostModel
  64. MetricsEmitter *CostModelMetricsEmitter
  65. // SettingsCache stores current state of app settings
  66. SettingsCache *cache.Cache
  67. // settingsSubscribers tracks channels through which changes to different
  68. // settings will be published in a pub/sub model
  69. settingsSubscribers map[string][]chan string
  70. settingsMutex sync.Mutex
  71. }
  72. func filterFields(fields string, data map[string]*CostData) map[string]CostData {
  73. fs := strings.Split(fields, ",")
  74. fmap := make(map[string]bool)
  75. for _, f := range fs {
  76. fieldNameLower := strings.ToLower(f) // convert to go struct name by uppercasing first letter
  77. log.Debugf("to delete: %s", fieldNameLower)
  78. fmap[fieldNameLower] = true
  79. }
  80. filteredData := make(map[string]CostData)
  81. for cname, costdata := range data {
  82. s := reflect.TypeOf(*costdata)
  83. val := reflect.ValueOf(*costdata)
  84. costdata2 := CostData{}
  85. cd2 := reflect.New(reflect.Indirect(reflect.ValueOf(costdata2)).Type()).Elem()
  86. n := s.NumField()
  87. for i := 0; i < n; i++ {
  88. field := s.Field(i)
  89. value := val.Field(i)
  90. value2 := cd2.Field(i)
  91. if _, ok := fmap[strings.ToLower(field.Name)]; !ok {
  92. value2.Set(reflect.Value(value))
  93. }
  94. }
  95. filteredData[cname] = cd2.Interface().(CostData)
  96. }
  97. return filteredData
  98. }
  99. // ParsePercentString takes a string of expected format "N%" and returns a floating point 0.0N.
  100. // If the "%" symbol is missing, it just returns 0.0N. Empty string is interpreted as "0%" and
  101. // return 0.0.
  102. func ParsePercentString(percentStr string) (float64, error) {
  103. if len(percentStr) == 0 {
  104. return 0.0, nil
  105. }
  106. if percentStr[len(percentStr)-1:] == "%" {
  107. percentStr = percentStr[:len(percentStr)-1]
  108. }
  109. discount, err := strconv.ParseFloat(percentStr, 64)
  110. if err != nil {
  111. return 0.0, err
  112. }
  113. discount *= 0.01
  114. return discount, nil
  115. }
  116. func WriteData(w http.ResponseWriter, data interface{}, err error) {
  117. if err != nil {
  118. proto.WriteError(w, proto.InternalServerError(err.Error()))
  119. return
  120. }
  121. proto.WriteData(w, data)
  122. }
  123. // 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.
  124. func (a *Accesses) RefreshPricingData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  125. w.Header().Set("Content-Type", "application/json")
  126. w.Header().Set("Access-Control-Allow-Origin", "*")
  127. err := a.CloudProvider.DownloadPricingData()
  128. if err != nil {
  129. log.Errorf("Error refreshing pricing data: %s", err.Error())
  130. }
  131. WriteData(w, nil, err)
  132. }
  133. func (a *Accesses) CostDataModel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  134. w.Header().Set("Content-Type", "application/json")
  135. w.Header().Set("Access-Control-Allow-Origin", "*")
  136. window := r.URL.Query().Get("timeWindow")
  137. offset := r.URL.Query().Get("offset")
  138. fields := r.URL.Query().Get("filterFields")
  139. namespace := r.URL.Query().Get("namespace")
  140. duration, err := timeutil.ParseDuration(window)
  141. if err != nil {
  142. WriteData(w, nil, fmt.Errorf("error parsing window (%s): %s", window, err))
  143. return
  144. }
  145. end := time.Now()
  146. if offset != "" {
  147. offsetDur, err := timeutil.ParseDuration(offset)
  148. if err != nil {
  149. WriteData(w, nil, fmt.Errorf("error parsing offset (%s): %s", offset, err))
  150. return
  151. }
  152. end = end.Add(-offsetDur)
  153. }
  154. start := end.Add(-duration)
  155. data, err := a.Model.ComputeCostData(start, end)
  156. // apply filter by removing if != namespace
  157. if namespace != "" {
  158. for key, costData := range data {
  159. if costData.Namespace != namespace {
  160. delete(data, key)
  161. }
  162. }
  163. }
  164. if fields != "" {
  165. filteredData := filterFields(fields, data)
  166. WriteData(w, filteredData, err)
  167. } else {
  168. WriteData(w, data, err)
  169. }
  170. }
  171. func (a *Accesses) GetAllNodePricing(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  172. w.Header().Set("Content-Type", "application/json")
  173. w.Header().Set("Access-Control-Allow-Origin", "*")
  174. data, err := a.CloudProvider.AllNodePricing()
  175. WriteData(w, data, err)
  176. }
  177. func (a *Accesses) ManagementPlatform(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  178. w.Header().Set("Content-Type", "application/json")
  179. w.Header().Set("Access-Control-Allow-Origin", "*")
  180. data, err := a.CloudProvider.GetManagementPlatform()
  181. WriteData(w, data, err)
  182. }
  183. func (a *Accesses) ClusterInfo(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  184. w.Header().Set("Content-Type", "application/json")
  185. w.Header().Set("Access-Control-Allow-Origin", "*")
  186. data := a.ClusterInfoProvider.GetClusterInfo()
  187. WriteData(w, data, nil)
  188. }
  189. func (a *Accesses) GetClusterInfoMap(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  190. w.Header().Set("Content-Type", "application/json")
  191. w.Header().Set("Access-Control-Allow-Origin", "*")
  192. data := a.ClusterMap.AsMap()
  193. WriteData(w, data, nil)
  194. }
  195. func (a *Accesses) GetServiceAccountStatus(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  196. w.Header().Set("Content-Type", "application/json")
  197. w.Header().Set("Access-Control-Allow-Origin", "*")
  198. WriteData(w, a.CloudProvider.ServiceAccountStatus(), nil)
  199. }
  200. func (a *Accesses) GetPricingSourceStatus(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  201. w.Header().Set("Content-Type", "application/json")
  202. w.Header().Set("Access-Control-Allow-Origin", "*")
  203. data := a.CloudProvider.PricingSourceStatus()
  204. WriteData(w, data, nil)
  205. }
  206. func (a *Accesses) GetPricingSourceCounts(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
  207. w.Header().Set("Content-Type", "application/json")
  208. w.Header().Set("Access-Control-Allow-Origin", "*")
  209. data, err := a.Model.GetPricingSourceCounts()
  210. WriteData(w, data, err)
  211. }
  212. func (a *Accesses) GetPricingSourceSummary(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
  213. w.Header().Set("Content-Type", "application/json")
  214. w.Header().Set("Access-Control-Allow-Origin", "*")
  215. data := a.CloudProvider.PricingSourceSummary()
  216. WriteData(w, data, nil)
  217. }
  218. func (a *Accesses) GetOrphanedPods(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  219. w.Header().Set("Content-Type", "application/json")
  220. w.Header().Set("Access-Control-Allow-Origin", "*")
  221. podlist := a.ClusterCache.GetAllPods()
  222. var lonePods []*clustercache.Pod
  223. for _, pod := range podlist {
  224. if len(pod.OwnerReferences) == 0 {
  225. lonePods = append(lonePods, pod)
  226. }
  227. }
  228. body, err := json.Marshal(lonePods)
  229. if err != nil {
  230. fmt.Fprintf(w, "Error decoding pod: %s", err)
  231. } else {
  232. w.Write(body)
  233. }
  234. }
  235. func (a *Accesses) GetInstallNamespace(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  236. w.Header().Set("Content-Type", "application/json")
  237. w.Header().Set("Access-Control-Allow-Origin", "*")
  238. ns := env.GetOpencostNamespace()
  239. w.Write([]byte(ns))
  240. }
  241. type InstallInfo struct {
  242. Containers []ContainerInfo `json:"containers"`
  243. ClusterInfo map[string]string `json:"clusterInfo"`
  244. Version string `json:"version"`
  245. }
  246. type ContainerInfo struct {
  247. ContainerName string `json:"containerName"`
  248. Image string `json:"image"`
  249. StartTime string `json:"startTime"`
  250. }
  251. func (a *Accesses) GetInstallInfo(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  252. w.Header().Set("Content-Type", "application/json")
  253. w.Header().Set("Access-Control-Allow-Origin", "*")
  254. containers, err := GetKubecostContainers(a.KubeClientSet)
  255. if err != nil {
  256. http.Error(w, fmt.Sprintf("Unable to list pods: %s", err.Error()), http.StatusInternalServerError)
  257. return
  258. }
  259. info := InstallInfo{
  260. Containers: containers,
  261. ClusterInfo: make(map[string]string),
  262. Version: version.FriendlyVersion(),
  263. }
  264. nodes := a.ClusterCache.GetAllNodes()
  265. cachePods := a.ClusterCache.GetAllPods()
  266. info.ClusterInfo["nodeCount"] = strconv.Itoa(len(nodes))
  267. info.ClusterInfo["podCount"] = strconv.Itoa(len(cachePods))
  268. body, err := json.Marshal(info)
  269. if err != nil {
  270. http.Error(w, fmt.Sprintf("Error decoding pod: %s", err.Error()), http.StatusInternalServerError)
  271. return
  272. }
  273. w.Write(body)
  274. }
  275. func GetKubecostContainers(kubeClientSet kubernetes.Interface) ([]ContainerInfo, error) {
  276. pods, err := kubeClientSet.CoreV1().Pods(env.GetOpencostNamespace()).List(context.Background(), metav1.ListOptions{
  277. LabelSelector: "app=cost-analyzer",
  278. FieldSelector: "status.phase=Running",
  279. Limit: 1,
  280. })
  281. if err != nil {
  282. return nil, fmt.Errorf("failed to query kubernetes client for kubecost pods: %s", err)
  283. }
  284. // If we have zero pods either something is weird with the install since the app selector is not exposed in the helm
  285. // chart or more likely we are running locally - in either case Images field will return as null
  286. containers := make([]ContainerInfo, 0)
  287. if len(pods.Items) > 0 {
  288. for _, pod := range pods.Items {
  289. for _, container := range pod.Spec.Containers {
  290. c := ContainerInfo{
  291. ContainerName: container.Name,
  292. Image: container.Image,
  293. StartTime: pod.Status.StartTime.String(),
  294. }
  295. containers = append(containers, c)
  296. }
  297. }
  298. }
  299. return containers, nil
  300. }
  301. func (a *Accesses) AddServiceKey(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  302. w.Header().Set("Content-Type", "application/json")
  303. w.Header().Set("Access-Control-Allow-Origin", "*")
  304. r.ParseForm()
  305. key := r.PostForm.Get("key")
  306. k := []byte(key)
  307. err := os.WriteFile(env.GetGCPAuthSecretFilePath(), k, 0644)
  308. if err != nil {
  309. fmt.Fprintf(w, "Error writing service key: %s", err)
  310. }
  311. w.WriteHeader(http.StatusOK)
  312. }
  313. func (a *Accesses) GetHelmValues(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  314. w.Header().Set("Content-Type", "application/json")
  315. w.Header().Set("Access-Control-Allow-Origin", "*")
  316. encodedValues := sysenv.Get("HELM_VALUES", "")
  317. if encodedValues == "" {
  318. fmt.Fprintf(w, "Values reporting disabled")
  319. return
  320. }
  321. result, err := base64.StdEncoding.DecodeString(encodedValues)
  322. if err != nil {
  323. fmt.Fprintf(w, "Failed to decode encoded values: %s", err)
  324. return
  325. }
  326. w.Write(result)
  327. }
  328. func Initialize(router *httprouter.Router, additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses {
  329. var err error
  330. // Kubernetes API setup
  331. kubeClientset, err := kubeconfig.LoadKubeClient("")
  332. if err != nil {
  333. log.Fatalf("Failed to build Kubernetes client: %s", err.Error())
  334. }
  335. // Create Kubernetes Cluster Cache + Watchers
  336. k8sCache := clusterc.NewKubernetesClusterCache(kubeClientset)
  337. k8sCache.Run()
  338. // Create ConfigFileManager for synchronization of shared configuration
  339. confManager := config.NewConfigFileManager(nil)
  340. cloudProviderKey := env.GetCloudProviderAPIKey()
  341. cloudProvider, err := provider.NewProvider(k8sCache, cloudProviderKey, confManager)
  342. if err != nil {
  343. panic(err.Error())
  344. }
  345. // ClusterInfo Provider to provide the cluster map with local and remote cluster data
  346. var clusterInfoProvider clusters.ClusterInfoProvider
  347. if env.IsClusterInfoFileEnabled() {
  348. clusterInfoFile := confManager.ConfigFileAt(env.GetClusterInfoFilePath())
  349. clusterInfoProvider = NewConfiguredClusterInfoProvider(clusterInfoFile)
  350. } else {
  351. clusterInfoProvider = NewLocalClusterInfoProvider(kubeClientset, cloudProvider)
  352. }
  353. const maxRetries = 10
  354. const retryInterval = 10 * time.Second
  355. var fatalErr error
  356. ctx, cancel := context.WithCancel(context.Background())
  357. fn := func() (source.OpenCostDataSource, error) {
  358. ds, e := prom.NewDefaultPrometheusDataSource(clusterInfoProvider)
  359. if e != nil {
  360. if source.IsRetryable(e) {
  361. return nil, e
  362. }
  363. fatalErr = e
  364. cancel()
  365. }
  366. return ds, e
  367. }
  368. if env.IsCollectorDataSourceEnabled() {
  369. fn = func() (source.OpenCostDataSource, error) {
  370. store := GetDefaultCollectorStorage()
  371. nodeStatConf, err := NewNodeClientConfigFromEnv()
  372. if err != nil {
  373. return nil, fmt.Errorf("failed to get node client config: %w", err)
  374. }
  375. clusterConfig, err := kubeconfig.LoadKubeconfig("")
  376. if err != nil {
  377. return nil, fmt.Errorf("failed to load kube config: %w", err)
  378. }
  379. nodeStatClient := nodestats.NewNodeStatsSummaryClient(k8sCache, nodeStatConf, clusterConfig)
  380. ds := collector.NewDefaultCollectorDataSource(
  381. store,
  382. clusterInfoProvider,
  383. k8sCache,
  384. nodeStatClient,
  385. )
  386. return ds, nil
  387. }
  388. }
  389. dataSource, _ := retry.Retry(
  390. ctx,
  391. fn,
  392. maxRetries,
  393. retryInterval,
  394. )
  395. if fatalErr != nil {
  396. log.Fatalf("Failed to create Prometheus data source: %s", fatalErr)
  397. panic(fatalErr)
  398. }
  399. // Append the pricing config watcher
  400. installNamespace := env.GetOpencostNamespace()
  401. configWatchers := watcher.NewConfigMapWatchers(kubeClientset, installNamespace, additionalConfigWatchers...)
  402. configWatchers.AddWatcher(provider.ConfigWatcherFor(cloudProvider))
  403. configWatchers.AddWatcher(metrics.GetMetricsConfigWatcher())
  404. configWatchers.Watch()
  405. clusterMap := dataSource.ClusterMap()
  406. settingsCache := cache.New(cache.NoExpiration, cache.NoExpiration)
  407. costModel := NewCostModel(dataSource, cloudProvider, k8sCache, clusterMap, dataSource.BatchDuration())
  408. metricsEmitter := NewCostModelMetricsEmitter(k8sCache, cloudProvider, clusterInfoProvider, costModel)
  409. a := &Accesses{
  410. DataSource: dataSource,
  411. KubeClientSet: kubeClientset,
  412. ClusterCache: k8sCache,
  413. ClusterMap: clusterMap,
  414. CloudProvider: cloudProvider,
  415. ConfigFileManager: confManager,
  416. ClusterInfoProvider: clusterInfoProvider,
  417. Model: costModel,
  418. MetricsEmitter: metricsEmitter,
  419. SettingsCache: settingsCache,
  420. }
  421. // Initialize mechanism for subscribing to settings changes
  422. a.InitializeSettingsPubSub()
  423. err = a.CloudProvider.DownloadPricingData()
  424. if err != nil {
  425. log.Infof("Failed to download pricing data: %s", err)
  426. }
  427. if !env.IsKubecostMetricsPodEnabled() {
  428. a.MetricsEmitter.Start()
  429. }
  430. a.DataSource.RegisterEndPoints(router)
  431. router.GET("/costDataModel", a.CostDataModel)
  432. router.GET("/allocation/compute", a.ComputeAllocationHandler)
  433. router.GET("/allocation/compute/summary", a.ComputeAllocationHandlerSummary)
  434. router.GET("/allNodePricing", a.GetAllNodePricing)
  435. router.POST("/refreshPricing", a.RefreshPricingData)
  436. router.GET("/managementPlatform", a.ManagementPlatform)
  437. router.GET("/clusterInfo", a.ClusterInfo)
  438. router.GET("/clusterInfoMap", a.GetClusterInfoMap)
  439. router.GET("/serviceAccountStatus", a.GetServiceAccountStatus)
  440. router.GET("/pricingSourceStatus", a.GetPricingSourceStatus)
  441. router.GET("/pricingSourceSummary", a.GetPricingSourceSummary)
  442. router.GET("/pricingSourceCounts", a.GetPricingSourceCounts)
  443. router.GET("/orphanedPods", a.GetOrphanedPods)
  444. router.GET("/installNamespace", a.GetInstallNamespace)
  445. router.GET("/installInfo", a.GetInstallInfo)
  446. router.POST("/serviceKey", a.AddServiceKey)
  447. router.GET("/helmValues", a.GetHelmValues)
  448. return a
  449. }
  450. // GetDefaultStorage retrieves the default shared storage which is required for running an opencost collector.
  451. func GetDefaultCollectorStorage() storage.Storage {
  452. const warningMessage = `Failed to create local collector directory '%s' - %s.
  453. Did you mean to enable to collector? For persistent storage, it's recommended to use Prometheus,
  454. or set a storage bucket configuration at %s.
  455. %s`
  456. // Try bucket storage if it exists
  457. store, err := storage.TryGetDefaultStorage()
  458. if err == nil {
  459. return store
  460. }
  461. // Fallback to a local storage bucket
  462. dir := env.GetLocalCollectorDirectory()
  463. err = os.MkdirAll(dir, os.ModePerm)
  464. if err != nil {
  465. log.Warnf(
  466. warningMessage,
  467. dir,
  468. err.Error(),
  469. sysenv.GetDefaultStorageConfigFilePath(),
  470. "Falling back to an in-memory file system for collector, which will lose any persistent storage upon restart.",
  471. )
  472. return storage.NewMemoryStorage()
  473. }
  474. return storage.NewFileStorage(dir)
  475. }
  476. // InitializeCloudCost Initializes Cloud Cost pipeline and querier and registers endpoints
  477. func InitializeCloudCost(router *httprouter.Router, providerConfig models.ProviderConfig) *cloudcost.PipelineService {
  478. log.Debugf("Cloud Cost config path: %s", env.GetCloudCostConfigPath())
  479. cloudConfigController := cloudconfig.NewMemoryController(providerConfig)
  480. repo := cloudcost.NewMemoryRepository()
  481. cloudCostPipelineService := cloudcost.NewPipelineService(repo, cloudConfigController, cloudcost.DefaultIngestorConfiguration())
  482. repoQuerier := cloudcost.NewRepositoryQuerier(repo)
  483. cloudCostQueryService := cloudcost.NewQueryService(repoQuerier, repoQuerier)
  484. router.GET("/cloud/config/export", cloudConfigController.GetExportConfigHandler())
  485. router.GET("/cloud/config/enable", cloudConfigController.GetEnableConfigHandler())
  486. router.GET("/cloud/config/disable", cloudConfigController.GetDisableConfigHandler())
  487. router.GET("/cloud/config/delete", cloudConfigController.GetDeleteConfigHandler())
  488. router.GET("/cloudCost", cloudCostQueryService.GetCloudCostHandler())
  489. router.GET("/cloudCost/view/graph", cloudCostQueryService.GetCloudCostViewGraphHandler())
  490. router.GET("/cloudCost/view/totals", cloudCostQueryService.GetCloudCostViewTotalsHandler())
  491. router.GET("/cloudCost/view/table", cloudCostQueryService.GetCloudCostViewTableHandler(nil))
  492. router.GET("/cloudCost/status", cloudCostPipelineService.GetCloudCostStatusHandler())
  493. router.GET("/cloudCost/rebuild", cloudCostPipelineService.GetCloudCostRebuildHandler())
  494. router.GET("/cloudCost/repair", cloudCostPipelineService.GetCloudCostRepairHandler())
  495. return cloudCostPipelineService
  496. }
  497. func InitializeCustomCost(router *httprouter.Router) *customcost.PipelineService {
  498. hourlyRepo := customcost.NewMemoryRepository()
  499. dailyRepo := customcost.NewMemoryRepository()
  500. ingConfig := customcost.DefaultIngestorConfiguration()
  501. var err error
  502. customCostPipelineService, err := customcost.NewPipelineService(hourlyRepo, dailyRepo, ingConfig)
  503. if err != nil {
  504. log.Errorf("error instantiating custom cost pipeline service: %v", err)
  505. return nil
  506. }
  507. customCostQuerier := customcost.NewRepositoryQuerier(hourlyRepo, dailyRepo, ingConfig.HourlyDuration, ingConfig.DailyDuration)
  508. customCostQueryService := customcost.NewQueryService(customCostQuerier)
  509. router.GET("/customCost/total", customCostQueryService.GetCustomCostTotalHandler())
  510. router.GET("/customCost/timeseries", customCostQueryService.GetCustomCostTimeseriesHandler())
  511. return customCostPipelineService
  512. }