agent.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package agent
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "path"
  7. "time"
  8. "github.com/opencost/opencost/core/pkg/clusters"
  9. "github.com/opencost/opencost/core/pkg/log"
  10. "github.com/opencost/opencost/core/pkg/source"
  11. "github.com/opencost/opencost/core/pkg/util/retry"
  12. "github.com/opencost/opencost/pkg/util/watcher"
  13. "github.com/opencost/opencost/core/pkg/version"
  14. "github.com/opencost/opencost/modules/prometheus-source/pkg/prom"
  15. "github.com/opencost/opencost/pkg/cloud/provider"
  16. "github.com/opencost/opencost/pkg/clustercache"
  17. "github.com/opencost/opencost/pkg/config"
  18. "github.com/opencost/opencost/pkg/costmodel"
  19. "github.com/opencost/opencost/pkg/env"
  20. "github.com/opencost/opencost/pkg/kubeconfig"
  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 *clustercache.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 := clustercache.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. const maxRetries = 10
  56. const retryInterval = 10 * time.Second
  57. var fatalErr error
  58. ctx, cancel := context.WithCancel(context.Background())
  59. dataSource, err := retry.Retry(
  60. ctx,
  61. func() (source.OpenCostDataSource, error) {
  62. ds, e := prom.NewDefaultPrometheusDataSource()
  63. if e != nil {
  64. if source.IsRetryable(e) {
  65. return nil, e
  66. }
  67. fatalErr = e
  68. cancel()
  69. }
  70. return ds, e
  71. },
  72. maxRetries,
  73. retryInterval,
  74. )
  75. if fatalErr != nil {
  76. log.Fatalf("Failed to create Prometheus data source: %s", fatalErr)
  77. panic(fatalErr)
  78. }
  79. // initialize kubernetes client and cluster cache
  80. k8sClient, clusterCache, err := newKubernetesClusterCache()
  81. if err != nil {
  82. panic(err.Error())
  83. }
  84. // Create ConfigFileManager for synchronization of shared configuration
  85. confManager := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  86. BucketStoreConfig: env.GetKubecostConfigBucket(),
  87. LocalConfigPath: "/",
  88. })
  89. cloudProviderKey := env.GetCloudProviderAPIKey()
  90. cloudProvider, err := provider.NewProvider(clusterCache, cloudProviderKey, confManager)
  91. if err != nil {
  92. panic(err.Error())
  93. }
  94. // Append the pricing config watcher
  95. kubecostNamespace := env.GetKubecostNamespace()
  96. configWatchers := watcher.NewConfigMapWatchers(k8sClient, kubecostNamespace)
  97. configWatchers.AddWatcher(provider.ConfigWatcherFor(cloudProvider))
  98. configWatchers.Watch()
  99. configPrefix := env.GetConfigPathWithDefault(env.DefaultConfigMountPath)
  100. // Initialize cluster exporting if it's enabled
  101. if env.IsExportClusterCacheEnabled() {
  102. cacheLocation := confManager.ConfigFileAt(path.Join(configPrefix, "cluster-cache.json"))
  103. clusterExporter = clustercache.NewClusterExporter(clusterCache, cacheLocation, ClusterExportInterval)
  104. clusterExporter.Run()
  105. }
  106. // ClusterInfo Provider to provide the cluster map with local and remote cluster data
  107. localClusterInfo := costmodel.NewLocalClusterInfoProvider(k8sClient, dataSource, cloudProvider)
  108. var clusterInfoProvider clusters.ClusterInfoProvider
  109. if env.IsExportClusterInfoEnabled() {
  110. clusterInfoConf := confManager.ConfigFileAt(path.Join(configPrefix, "cluster-info.json"))
  111. clusterInfoProvider = costmodel.NewClusterInfoWriteOnRequest(localClusterInfo, clusterInfoConf)
  112. } else {
  113. clusterInfoProvider = localClusterInfo
  114. }
  115. // Initialize ClusterMap for maintaining ClusterInfo by ClusterID
  116. clusterMap := dataSource.NewClusterMap(clusterInfoProvider)
  117. costModel := costmodel.NewCostModel(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. }