agent.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package agent
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/kubecost/cost-model/pkg/cloud"
  8. "github.com/kubecost/cost-model/pkg/clustercache"
  9. "github.com/kubecost/cost-model/pkg/config"
  10. "github.com/kubecost/cost-model/pkg/costmodel"
  11. "github.com/kubecost/cost-model/pkg/costmodel/clusters"
  12. "github.com/kubecost/cost-model/pkg/env"
  13. "github.com/kubecost/cost-model/pkg/log"
  14. "github.com/kubecost/cost-model/pkg/prom"
  15. "github.com/kubecost/cost-model/pkg/util/watcher"
  16. prometheus "github.com/prometheus/client_golang/api"
  17. prometheusAPI "github.com/prometheus/client_golang/api/prometheus/v1"
  18. "github.com/prometheus/client_golang/prometheus/promhttp"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/klog"
  21. "github.com/rs/cors"
  22. "k8s.io/client-go/kubernetes"
  23. "k8s.io/client-go/rest"
  24. "k8s.io/client-go/tools/clientcmd"
  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. var kc *rest.Config
  45. if kubeconfig := env.GetKubeConfigPath(); kubeconfig != "" {
  46. kc, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
  47. } else {
  48. kc, err = rest.InClusterConfig()
  49. }
  50. if err != nil {
  51. return nil, nil, err
  52. }
  53. kubeClientset, err := kubernetes.NewForConfig(kc)
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. // Create Kubernetes Cluster Cache + Watchers
  58. k8sCache := clustercache.NewKubernetesClusterCache(kubeClientset)
  59. k8sCache.Run()
  60. return kubeClientset, k8sCache, nil
  61. }
  62. func newPrometheusClient() (prometheus.Client, error) {
  63. address := env.GetPrometheusServerEndpoint()
  64. if address == "" {
  65. return nil, fmt.Errorf("No address for prometheus set in $%s. Aborting.", env.PrometheusServerEndpointEnvVar)
  66. }
  67. queryConcurrency := env.GetMaxQueryConcurrency()
  68. log.Infof("Prometheus Client Max Concurrency set to %d", queryConcurrency)
  69. timeout := 120 * time.Second
  70. keepAlive := 120 * time.Second
  71. tlsHandshakeTimeout := 10 * time.Second
  72. var rateLimitRetryOpts *prom.RateLimitRetryOpts = nil
  73. if env.IsPrometheusRetryOnRateLimitResponse() {
  74. rateLimitRetryOpts = &prom.RateLimitRetryOpts{
  75. MaxRetries: env.GetPrometheusRetryOnRateLimitMaxRetries(),
  76. DefaultRetryWait: env.GetPrometheusRetryOnRateLimitDefaultWait(),
  77. }
  78. }
  79. promCli, err := prom.NewPrometheusClient(address, &prom.PrometheusClientConfig{
  80. Timeout: timeout,
  81. KeepAlive: keepAlive,
  82. TLSHandshakeTimeout: tlsHandshakeTimeout,
  83. TLSInsecureSkipVerify: env.GetInsecureSkipVerify(),
  84. RateLimitRetryOpts: rateLimitRetryOpts,
  85. Auth: &prom.ClientAuth{
  86. Username: env.GetDBBasicAuthUsername(),
  87. Password: env.GetDBBasicAuthUserPassword(),
  88. BearerToken: env.GetDBBearerToken(),
  89. },
  90. QueryConcurrency: queryConcurrency,
  91. QueryLogFile: "",
  92. })
  93. if err != nil {
  94. return nil, fmt.Errorf("Failed to create prometheus client, Error: %v", err)
  95. }
  96. m, err := prom.Validate(promCli)
  97. if err != nil || !m.Running {
  98. if err != nil {
  99. log.Errorf("Failed to query prometheus at %s. Error: %s . Troubleshooting help available at: %s", address, err.Error(), prom.PrometheusTroubleshootingURL)
  100. } else if !m.Running {
  101. log.Errorf("Prometheus at %s is not running. Troubleshooting help available at: %s", address, prom.PrometheusTroubleshootingURL)
  102. }
  103. } else {
  104. log.Infof("Success: retrieved the 'up' query against prometheus at: %s", address)
  105. }
  106. api := prometheusAPI.NewAPI(promCli)
  107. _, err = api.Config(context.Background())
  108. if err != nil {
  109. log.Infof("No valid prometheus config file at %s. Error: %s . Troubleshooting help available at: %s. Ignore if using cortex/thanos here.", address, err.Error(), prom.PrometheusTroubleshootingURL)
  110. } else {
  111. log.Infof("Retrieved a prometheus config file from: %s", address)
  112. }
  113. return promCli, nil
  114. }
  115. func Execute(opts *AgentOpts) error {
  116. log.Infof("Starting Kubecost Agent")
  117. configWatchers := watcher.NewConfigMapWatchers()
  118. scrapeInterval := time.Minute
  119. promCli, err := newPrometheusClient()
  120. if err != nil {
  121. panic(err.Error())
  122. }
  123. // Lookup scrape interval for kubecost job, update if found
  124. si, err := prom.ScrapeIntervalFor(promCli, env.GetKubecostJobName())
  125. if err == nil {
  126. scrapeInterval = si
  127. }
  128. klog.Infof("Using scrape interval of %f", scrapeInterval.Seconds())
  129. // initialize kubernetes client and cluster cache
  130. k8sClient, clusterCache, err := newKubernetesClusterCache()
  131. if err != nil {
  132. panic(err.Error())
  133. }
  134. // Create ConfigFileManager for synchronization of shared configuration
  135. confManager := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  136. BucketStoreConfig: env.GetKubecostConfigBucket(),
  137. LocalConfigPath: "/",
  138. })
  139. cloudProviderKey := env.GetCloudProviderAPIKey()
  140. cloudProvider, err := cloud.NewProvider(clusterCache, cloudProviderKey, confManager)
  141. if err != nil {
  142. panic(err.Error())
  143. }
  144. // Append the pricing config watcher
  145. configWatchers.AddWatcher(cloud.ConfigWatcherFor(cloudProvider))
  146. watchConfigFunc := configWatchers.ToWatchFunc()
  147. watchedConfigs := configWatchers.GetWatchedConfigs()
  148. kubecostNamespace := env.GetKubecostNamespace()
  149. // We need an initial invocation because the init of the cache has happened before we had access to the provider.
  150. for _, cw := range watchedConfigs {
  151. configs, err := k8sClient.CoreV1().ConfigMaps(kubecostNamespace).Get(context.Background(), cw, metav1.GetOptions{})
  152. if err != nil {
  153. klog.Infof("No %s configmap found at install time, using existing configs: %s", cw, err.Error())
  154. } else {
  155. watchConfigFunc(configs)
  156. }
  157. }
  158. clusterCache.SetConfigMapUpdateFunc(watchConfigFunc)
  159. // Initialize cluster exporting if it's enabled
  160. if env.IsExportClusterCacheEnabled() {
  161. cacheLocation := confManager.ConfigFileAt("/var/configs/cluster-cache.json")
  162. clusterExporter = clustercache.NewClusterExporter(clusterCache, cacheLocation, ClusterExportInterval)
  163. clusterExporter.Run()
  164. }
  165. // ClusterInfo Provider to provide the cluster map with local and remote cluster data
  166. localClusterInfo := costmodel.NewLocalClusterInfoProvider(k8sClient, cloudProvider)
  167. var clusterInfoProvider clusters.ClusterInfoProvider
  168. if env.IsExportClusterInfoEnabled() {
  169. clusterInfoConf := confManager.ConfigFileAt("/var/configs/cluster-info.json")
  170. clusterInfoProvider = costmodel.NewClusterInfoWriteOnRequest(localClusterInfo, clusterInfoConf)
  171. } else {
  172. clusterInfoProvider = localClusterInfo
  173. }
  174. // Initialize ClusterMap for maintaining ClusterInfo by ClusterID
  175. clusterMap := clusters.NewClusterMap(promCli, clusterInfoProvider, 5*time.Minute)
  176. costModel := costmodel.NewCostModel(promCli, cloudProvider, clusterCache, clusterMap, scrapeInterval)
  177. // initialize Kubernetes Metrics Emitter
  178. metricsEmitter := costmodel.NewCostModelMetricsEmitter(promCli, clusterCache, cloudProvider, clusterInfoProvider, costModel)
  179. // download pricing data
  180. err = cloudProvider.DownloadPricingData()
  181. if err != nil {
  182. klog.Errorf("Error downloading pricing data: %s", err)
  183. }
  184. // start emitting metrics
  185. metricsEmitter.Start()
  186. rootMux := http.NewServeMux()
  187. rootMux.HandleFunc("/healthz", Healthz)
  188. rootMux.Handle("/metrics", promhttp.Handler())
  189. handler := cors.AllowAll().Handler(rootMux)
  190. return http.ListenAndServe(fmt.Sprintf(":%d", env.GetKubecostMetricsPort()), handler)
  191. }