agent.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package agent
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/clusters"
  8. "github.com/opencost/opencost/core/pkg/log"
  9. "github.com/opencost/opencost/core/pkg/source"
  10. "github.com/opencost/opencost/core/pkg/util/retry"
  11. "github.com/opencost/opencost/pkg/util/watcher"
  12. "github.com/opencost/opencost/core/pkg/clustercache"
  13. "github.com/opencost/opencost/core/pkg/kubeconfig"
  14. "github.com/opencost/opencost/core/pkg/version"
  15. "github.com/opencost/opencost/modules/prometheus-source/pkg/prom"
  16. "github.com/opencost/opencost/pkg/cloud/provider"
  17. cluster "github.com/opencost/opencost/pkg/clustercache"
  18. "github.com/opencost/opencost/pkg/config"
  19. "github.com/opencost/opencost/pkg/costmodel"
  20. "github.com/opencost/opencost/pkg/env"
  21. "github.com/opencost/opencost/pkg/metrics"
  22. "github.com/prometheus/client_golang/prometheus/promhttp"
  23. "github.com/rs/cors"
  24. "k8s.io/client-go/kubernetes"
  25. )
  26. // AgentOpts contain configuration options that can be passed to the Execute() method
  27. type AgentOpts struct {
  28. // Stubbed for future configuration
  29. }
  30. // ClusterExportInterval is the interval used to export the cluster if env.IsExportClusterCacheEnabled() is true
  31. const ClusterExportInterval = 5 * time.Minute
  32. // clusterExporter is used if env.IsExportClusterCacheEnabled() is set to true
  33. // it will export the kubernetes cluster data to a file on a specific interval
  34. var clusterExporter *cluster.ClusterExporter
  35. func Healthz(w http.ResponseWriter, _ *http.Request) {
  36. w.WriteHeader(200)
  37. w.Header().Set("Content-Length", "0")
  38. w.Header().Set("Content-Type", "text/plain")
  39. }
  40. // initializes the kubernetes client cache
  41. func newKubernetesClusterCache() (kubernetes.Interface, clustercache.ClusterCache, error) {
  42. var err error
  43. // Kubernetes API setup
  44. kubeClientset, err := kubeconfig.LoadKubeClient("")
  45. if err != nil {
  46. return nil, nil, err
  47. }
  48. // Create Kubernetes Cluster Cache + Watchers
  49. k8sCache := cluster.NewKubernetesClusterCache(kubeClientset)
  50. k8sCache.Run()
  51. return kubeClientset, k8sCache, nil
  52. }
  53. func Execute(opts *AgentOpts) error {
  54. log.Infof("Starting Kubecost Agent version %s", version.FriendlyVersion())
  55. // initialize kubernetes client and cluster cache
  56. k8sClient, clusterCache, err := newKubernetesClusterCache()
  57. if err != nil {
  58. panic(err.Error())
  59. }
  60. clusterUID, err := kubeconfig.GetClusterUID(k8sClient)
  61. if err != nil {
  62. return fmt.Errorf("error getting cluster UID: %w", err)
  63. }
  64. // Create ConfigFileManager for synchronization of shared configuration
  65. confManager := config.NewConfigFileManager(nil)
  66. cloudProviderKey := env.GetCloudProviderAPIKey()
  67. cloudProvider, err := provider.NewProvider(clusterCache, cloudProviderKey, confManager)
  68. if err != nil {
  69. panic(err.Error())
  70. }
  71. // ClusterInfo Provider to provide the cluster map with local and remote cluster data
  72. localClusterInfo := costmodel.NewLocalClusterInfoProvider(k8sClient, cloudProvider)
  73. var clusterInfoProvider clusters.ClusterInfoProvider
  74. if env.IsExportClusterInfoEnabled() {
  75. clusterInfoConf := confManager.ConfigFileAt(env.GetClusterInfoFilePath())
  76. clusterInfoProvider = costmodel.NewClusterInfoWriteOnRequest(localClusterInfo, clusterInfoConf)
  77. } else {
  78. clusterInfoProvider = localClusterInfo
  79. }
  80. const maxRetries = 10
  81. const retryInterval = 10 * time.Second
  82. var fatalErr error
  83. ctx, cancel := context.WithCancel(context.Background())
  84. dataSource, err := retry.Retry(
  85. ctx,
  86. func() (source.OpenCostDataSource, error) {
  87. ds, e := prom.NewDefaultPrometheusDataSource(clusterInfoProvider)
  88. if e != nil {
  89. if source.IsRetryable(e) {
  90. return nil, e
  91. }
  92. fatalErr = e
  93. cancel()
  94. }
  95. return ds, e
  96. },
  97. maxRetries,
  98. retryInterval,
  99. )
  100. if fatalErr != nil {
  101. log.Fatalf("Failed to create Prometheus data source: %s", fatalErr)
  102. panic(fatalErr)
  103. }
  104. // Append the pricing config watcher
  105. installNamespace := env.GetOpencostNamespace()
  106. configWatchers := watcher.NewConfigMapWatchers(k8sClient, installNamespace)
  107. configWatchers.AddWatcher(provider.ConfigWatcherFor(cloudProvider))
  108. configWatchers.Watch()
  109. // Initialize cluster exporting if it's enabled
  110. if env.IsExportClusterCacheEnabled() {
  111. cacheLocation := confManager.ConfigFileAt(env.GetClusterCacheFilePath())
  112. clusterExporter = cluster.NewClusterExporter(clusterCache, cacheLocation, ClusterExportInterval)
  113. clusterExporter.Run()
  114. }
  115. // Initialize ClusterMap for maintaining ClusterInfo by ClusterID
  116. clusterMap := dataSource.ClusterMap()
  117. costModel := costmodel.NewCostModel(clusterUID, dataSource, cloudProvider, clusterCache, clusterMap, dataSource.BatchDuration())
  118. // initialize Kubernetes Metrics Emitter
  119. metricsEmitter := costmodel.NewCostModelMetricsEmitter(clusterCache, cloudProvider, clusterInfoProvider, costModel)
  120. // download pricing data
  121. err = cloudProvider.DownloadPricingData()
  122. if err != nil {
  123. log.Errorf("Error downloading pricing data: %s", err)
  124. }
  125. // start emitting metrics
  126. metricsEmitter.Start()
  127. rootMux := http.NewServeMux()
  128. rootMux.HandleFunc("/healthz", Healthz)
  129. rootMux.Handle("/metrics", promhttp.Handler())
  130. telemetryHandler := metrics.ResponseMetricMiddleware(rootMux)
  131. handler := cors.AllowAll().Handler(telemetryHandler)
  132. return http.ListenAndServe(fmt.Sprintf(":%d", env.GetKubecostMetricsPort()), handler)
  133. }