agent.go 5.4 KB

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