router.go 22 KB

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