costmodelenv.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. package env
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/env"
  6. "github.com/opencost/opencost/core/pkg/log"
  7. "github.com/opencost/opencost/core/pkg/util/timeutil"
  8. )
  9. const (
  10. APIPortEnvVar = "API_PORT"
  11. AWSAccessKeyIDEnvVar = "AWS_ACCESS_KEY_ID"
  12. AWSAccessKeySecretEnvVar = "AWS_SECRET_ACCESS_KEY"
  13. AWSClusterIDEnvVar = "AWS_CLUSTER_ID"
  14. AWSPricingURL = "AWS_PRICING_URL"
  15. AlibabaAccessKeyIDEnvVar = "ALIBABA_ACCESS_KEY_ID"
  16. AlibabaAccessKeySecretEnvVar = "ALIBABA_SECRET_ACCESS_KEY"
  17. AzureOfferIDEnvVar = "AZURE_OFFER_ID"
  18. AzureBillingAccountEnvVar = "AZURE_BILLING_ACCOUNT"
  19. AzureDownloadBillingDataToDiskEnvVar = "AZURE_DOWNLOAD_BILLING_DATA_TO_DISK"
  20. KubecostNamespaceEnvVar = "KUBECOST_NAMESPACE"
  21. PodNameEnvVar = "POD_NAME"
  22. ClusterIDEnvVar = "CLUSTER_ID"
  23. ClusterProfileEnvVar = "CLUSTER_PROFILE"
  24. PrometheusServerEndpointEnvVar = "%sPROMETHEUS_SERVER_ENDPOINT"
  25. MaxQueryConcurrencyEnvVar = "MAX_QUERY_CONCURRENCY"
  26. QueryLoggingFileEnvVar = "QUERY_LOGGING_FILE"
  27. RemoteEnabledEnvVar = "REMOTE_WRITE_ENABLED"
  28. RemotePWEnvVar = "REMOTE_WRITE_PASSWORD"
  29. SQLAddressEnvVar = "SQL_ADDRESS"
  30. UseCSVProviderEnvVar = "USE_CSV_PROVIDER"
  31. CSVRegionEnvVar = "CSV_REGION"
  32. CSVEndpointEnvVar = "CSV_ENDPOINT"
  33. CSVPathEnvVar = "CSV_PATH"
  34. ConfigPathEnvVar = "CONFIG_PATH"
  35. CloudProviderAPIKeyEnvVar = "CLOUD_PROVIDER_API_KEY"
  36. DisableAggregateCostModelCache = "DISABLE_AGGREGATE_COST_MODEL_CACHE"
  37. EmitPodAnnotationsMetricEnvVar = "EMIT_POD_ANNOTATIONS_METRIC"
  38. EmitNamespaceAnnotationsMetricEnvVar = "EMIT_NAMESPACE_ANNOTATIONS_METRIC"
  39. EmitDeprecatedMetrics = "EMIT_DEPRECATED_METRICS"
  40. EmitKsmV1MetricsEnvVar = "EMIT_KSM_V1_METRICS"
  41. EmitKsmV1MetricsOnly = "EMIT_KSM_V1_METRICS_ONLY"
  42. ThanosEnabledEnvVar = "THANOS_ENABLED"
  43. ThanosQueryUrlEnvVar = "THANOS_QUERY_URL"
  44. ThanosOffsetEnvVar = "THANOS_QUERY_OFFSET"
  45. ThanosMaxSourceResEnvVar = "THANOS_MAX_SOURCE_RESOLUTION"
  46. PProfEnabledEnvVar = "PPROF_ENABLED"
  47. LogCollectionEnabledEnvVar = "LOG_COLLECTION_ENABLED"
  48. ProductAnalyticsEnabledEnvVar = "PRODUCT_ANALYTICS_ENABLED"
  49. ErrorReportingEnabledEnvVar = "ERROR_REPORTING_ENABLED"
  50. ValuesReportingEnabledEnvVar = "VALUES_REPORTING_ENABLED"
  51. DBBasicAuthUsername = "%sDB_BASIC_AUTH_USERNAME"
  52. DBBasicAuthPassword = "%sDB_BASIC_AUTH_PW"
  53. DBBearerToken = "%sDB_BEARER_TOKEN"
  54. MultiClusterBasicAuthUsername = "MC_BASIC_AUTH_USERNAME"
  55. MultiClusterBasicAuthPassword = "MC_BASIC_AUTH_PW"
  56. MultiClusterBearerToken = "MC_BEARER_TOKEN"
  57. InsecureSkipVerify = "%sINSECURE_SKIP_VERIFY"
  58. KubeConfigPathEnvVar = "KUBECONFIG_PATH"
  59. UTCOffsetEnvVar = "UTC_OFFSET"
  60. CurrentClusterIdFilterEnabledVar = "CURRENT_CLUSTER_ID_FILTER_ENABLED"
  61. CacheWarmingEnabledEnvVar = "CACHE_WARMING_ENABLED"
  62. ETLEnabledEnvVar = "ETL_ENABLED"
  63. ETLMaxPrometheusQueryDurationMinutes = "ETL_MAX_PROMETHEUS_QUERY_DURATION_MINUTES"
  64. ETLResolutionSeconds = "ETL_RESOLUTION_SECONDS"
  65. LegacyExternalAPIDisabledVar = "LEGACY_EXTERNAL_API_DISABLED"
  66. PromClusterIDLabelEnvVar = "PROM_CLUSTER_ID_LABEL"
  67. PricingConfigmapName = "PRICING_CONFIGMAP_NAME"
  68. MetricsConfigmapName = "METRICS_CONFIGMAP_NAME"
  69. KubecostJobNameEnvVar = "KUBECOST_JOB_NAME"
  70. KubecostConfigBucketEnvVar = "KUBECOST_CONFIG_BUCKET"
  71. ClusterInfoFileEnabledEnvVar = "CLUSTER_INFO_FILE_ENABLED"
  72. ClusterCacheFileEnabledEnvVar = "CLUSTER_CACHE_FILE_ENABLED"
  73. PrometheusQueryOffsetEnvVar = "PROMETHEUS_QUERY_OFFSET"
  74. PrometheusRetryOnRateLimitResponseEnvVar = "PROMETHEUS_RETRY_ON_RATE_LIMIT"
  75. PrometheusRetryOnRateLimitMaxRetriesEnvVar = "PROMETHEUS_RETRY_ON_RATE_LIMIT_MAX_RETRIES"
  76. PrometheusRetryOnRateLimitDefaultWaitEnvVar = "PROMETHEUS_RETRY_ON_RATE_LIMIT_DEFAULT_WAIT"
  77. PrometheusHeaderXScopeOrgIdEnv = "%sPROMETHEUS_HEADER_X_SCOPE_ORGID"
  78. IngestPodUIDEnvVar = "INGEST_POD_UID"
  79. ETLReadOnlyMode = "ETL_READ_ONLY"
  80. AllocationNodeLabelsEnabled = "ALLOCATION_NODE_LABELS_ENABLED"
  81. AllocationNodeLabelsIncludeList = "ALLOCATION_NODE_LABELS_INCLUDE_LIST"
  82. regionOverrideList = "REGION_OVERRIDE_LIST"
  83. ExportCSVFile = "EXPORT_CSV_FILE"
  84. ExportCSVLabelsList = "EXPORT_CSV_LABELS_LIST"
  85. ExportCSVLabelsAll = "EXPORT_CSV_LABELS_ALL"
  86. ExportCSVMaxDays = "EXPORT_CSV_MAX_DAYS"
  87. DataRetentionDailyResolutionDaysEnvVar = "DATA_RETENTION_DAILY_RESOLUTION_DAYS"
  88. // We assume that Kubernetes is enabled if there is a KUBERNETES_PORT environment variable present
  89. KubernetesEnabledEnvVar = "KUBERNETES_PORT"
  90. CloudCostEnabledEnvVar = "CLOUD_COST_ENABLED"
  91. CloudCostConfigPath = "CLOUD_COST_CONFIG_PATH"
  92. CloudCostMonthToDateIntervalVar = "CLOUD_COST_MONTH_TO_DATE_INTERVAL"
  93. CloudCostRefreshRateHoursEnvVar = "CLOUD_COST_REFRESH_RATE_HOURS"
  94. CloudCostQueryWindowDaysEnvVar = "CLOUD_COST_QUERY_WINDOW_DAYS"
  95. CloudCostRunWindowDaysEnvVar = "CLOUD_COST_RUN_WINDOW_DAYS"
  96. OCIPricingURL = "OCI_PRICING_URL"
  97. )
  98. type PrometheusType int64
  99. const (
  100. Server PrometheusType = iota
  101. Scrape
  102. )
  103. func (pt PrometheusType) envFormat(envStr string) string {
  104. value := ""
  105. if pt == Scrape {
  106. value = "SCRAPE_"
  107. }
  108. return fmt.Sprintf(envStr, value)
  109. }
  110. const DefaultConfigMountPath = "/var/configs"
  111. func IsETLReadOnlyMode() bool {
  112. return env.GetBool(ETLReadOnlyMode, false)
  113. }
  114. func GetExportCSVFile() string {
  115. return env.Get(ExportCSVFile, "")
  116. }
  117. func GetExportCSVLabelsAll() bool {
  118. return env.GetBool(ExportCSVLabelsAll, false)
  119. }
  120. func GetExportCSVLabelsList() []string {
  121. return env.GetList(ExportCSVLabelsList, ",")
  122. }
  123. func IsPProfEnabled() bool {
  124. return env.GetBool(PProfEnabledEnvVar, false)
  125. }
  126. func GetExportCSVMaxDays() int {
  127. return env.GetInt(ExportCSVMaxDays, 90)
  128. }
  129. // GetAPIPort returns the environment variable value for APIPortEnvVar which
  130. // is the port number the API is available on.
  131. func GetAPIPort() int {
  132. return env.GetInt(APIPortEnvVar, 9003)
  133. }
  134. // GetKubecostConfigBucket returns a file location for a mounted bucket configuration which is used to store
  135. // a subset of kubecost configurations that require sharing via remote storage.
  136. func GetKubecostConfigBucket() string {
  137. return env.Get(KubecostConfigBucketEnvVar, "")
  138. }
  139. // IsClusterInfoFileEnabled returns true if the cluster info is read from a file or pulled from the local
  140. // cloud provider and kubernetes.
  141. func IsClusterInfoFileEnabled() bool {
  142. return env.GetBool(ClusterInfoFileEnabledEnvVar, false)
  143. }
  144. // IsClusterCacheFileEnabled returns true if the kubernetes cluster data is read from a file or pulled from the local
  145. // kubernetes API.
  146. func IsClusterCacheFileEnabled() bool {
  147. return env.GetBool(ClusterCacheFileEnabledEnvVar, false)
  148. }
  149. // IsPrometheusRetryOnRateLimitResponse will attempt to retry if a 429 response is received OR a 400 with a body containing
  150. // ThrottleException (common in AWS services like AMP)
  151. func IsPrometheusRetryOnRateLimitResponse() bool {
  152. return env.GetBool(PrometheusRetryOnRateLimitResponseEnvVar, true)
  153. }
  154. // GetPrometheusRetryOnRateLimitMaxRetries returns the maximum number of retries that should be attempted prior to failing.
  155. // Only used if IsPrometheusRetryOnRateLimitResponse() is true.
  156. func GetPrometheusRetryOnRateLimitMaxRetries() int {
  157. return env.GetInt(PrometheusRetryOnRateLimitMaxRetriesEnvVar, 5)
  158. }
  159. // GetPrometheusRetryOnRateLimitDefaultWait returns the default wait time for a retriable rate limit response without a
  160. // Retry-After header.
  161. func GetPrometheusRetryOnRateLimitDefaultWait() time.Duration {
  162. return env.GetDuration(PrometheusRetryOnRateLimitDefaultWaitEnvVar, 100*time.Millisecond)
  163. }
  164. // GetPrometheusHeaderXScopeOrgId returns the default value for X-Scope-OrgID header used for requests in Mimir/Cortex-Tenant API.
  165. // To use Mimir(or Cortex-Tenant) instead of Prometheus add variable from cluster settings:
  166. // "PROMETHEUS_HEADER_X_SCOPE_ORGID": "my-cluster-name"
  167. // Then set Prometheus URL to prometheus API endpoint:
  168. // "PROMETHEUS_SERVER_ENDPOINT": "http://mimir-url/prometheus/"
  169. func GetPrometheusHeaderXScopeOrgId(clientType PrometheusType) string {
  170. return env.Get(clientType.envFormat(PrometheusHeaderXScopeOrgIdEnv), "")
  171. }
  172. // GetPrometheusQueryOffset returns the time.Duration to offset all prometheus queries by. NOTE: This env var is applied
  173. // to all non-range queries made via our query context. This should only be applied when there is a significant delay in
  174. // data arriving in the target prom db. For example, if supplying a thanos or cortex querier for the prometheus server, using
  175. // a 3h offset will ensure that current time = current time - 3h.
  176. //
  177. // This offset is NOT the same as the GetThanosOffset() option, as that is only applied to queries made specifically targeting
  178. // thanos. This offset is applied globally.
  179. func GetPrometheusQueryOffset() time.Duration {
  180. offset := env.Get(PrometheusQueryOffsetEnvVar, "")
  181. if offset == "" {
  182. return 0
  183. }
  184. dur, err := timeutil.ParseDuration(offset)
  185. if err != nil {
  186. return 0
  187. }
  188. return dur
  189. }
  190. func GetPricingConfigmapName() string {
  191. return env.Get(PricingConfigmapName, "pricing-configs")
  192. }
  193. func GetMetricsConfigmapName() string {
  194. return env.Get(MetricsConfigmapName, "metrics-config")
  195. }
  196. // IsEmitNamespaceAnnotationsMetric returns true if cost-model is configured to emit the kube_namespace_annotations metric
  197. // containing the namespace annotations
  198. func IsEmitNamespaceAnnotationsMetric() bool {
  199. return env.GetBool(EmitNamespaceAnnotationsMetricEnvVar, false)
  200. }
  201. // IsEmitPodAnnotationsMetric returns true if cost-model is configured to emit the kube_pod_annotations metric containing
  202. // pod annotations.
  203. func IsEmitPodAnnotationsMetric() bool {
  204. return env.GetBool(EmitPodAnnotationsMetricEnvVar, false)
  205. }
  206. // IsEmitKsmV1Metrics returns true if cost-model is configured to emit all necessary KSM v1
  207. // metrics that were removed in KSM v2
  208. func IsEmitKsmV1Metrics() bool {
  209. return env.GetBool(EmitKsmV1MetricsEnvVar, true)
  210. }
  211. func IsEmitKsmV1MetricsOnly() bool {
  212. return env.GetBool(EmitKsmV1MetricsOnly, false)
  213. }
  214. func IsEmitDeprecatedMetrics() bool {
  215. return env.GetBool(EmitDeprecatedMetrics, false)
  216. }
  217. // GetAWSAccessKeyID returns the environment variable value for AWSAccessKeyIDEnvVar which represents
  218. // the AWS access key for authentication
  219. func GetAWSAccessKeyID() string {
  220. awsAccessKeyID := env.Get(AWSAccessKeyIDEnvVar, "")
  221. // If the sample nil service key name is set, zero it out so that it is not
  222. // misinterpreted as a real service key.
  223. if awsAccessKeyID == "AKIXXX" {
  224. awsAccessKeyID = ""
  225. }
  226. return awsAccessKeyID
  227. }
  228. // GetAWSAccessKeySecret returns the environment variable value for AWSAccessKeySecretEnvVar which represents
  229. // the AWS access key secret for authentication
  230. func GetAWSAccessKeySecret() string {
  231. return env.Get(AWSAccessKeySecretEnvVar, "")
  232. }
  233. // GetAWSClusterID returns the environment variable value for AWSClusterIDEnvVar which represents
  234. // an AWS specific cluster identifier.
  235. func GetAWSClusterID() string {
  236. return env.Get(AWSClusterIDEnvVar, "")
  237. }
  238. // GetAWSPricingURL returns an optional alternative URL to fetch AWS pricing data from; for use in airgapped environments
  239. func GetAWSPricingURL() string {
  240. return env.Get(AWSPricingURL, "")
  241. }
  242. // GetAlibabaAccessKeyID returns the environment variable value for AlibabaAccessKeyIDEnvVar which represents
  243. // the Alibaba access key for authentication
  244. func GetAlibabaAccessKeyID() string {
  245. return env.Get(AlibabaAccessKeyIDEnvVar, "")
  246. }
  247. // GetAlibabaAccessKeySecret returns the environment variable value for AlibabaAccessKeySecretEnvVar which represents
  248. // the Alibaba access key secret for authentication
  249. func GetAlibabaAccessKeySecret() string {
  250. return env.Get(AlibabaAccessKeySecretEnvVar, "")
  251. }
  252. // GetAzureOfferID returns the environment variable value for AzureOfferIDEnvVar which represents
  253. // the Azure offer ID for determining prices.
  254. func GetAzureOfferID() string {
  255. return env.Get(AzureOfferIDEnvVar, "")
  256. }
  257. // GetAzureBillingAccount returns the environment variable value for
  258. // AzureBillingAccountEnvVar which represents the Azure billing
  259. // account for determining prices. If this is specified
  260. // customer-specific prices will be downloaded from the consumption
  261. // price sheet API.
  262. func GetAzureBillingAccount() string {
  263. return env.Get(AzureBillingAccountEnvVar, "")
  264. }
  265. // IsAzureDownloadBillingDataToDisk returns the environment variable value for
  266. // AzureDownloadBillingDataToDiskEnvVar which indicates whether the Azure
  267. // Billing Data should be held in memory or written to disk.
  268. func IsAzureDownloadBillingDataToDisk() bool {
  269. return env.GetBool(AzureDownloadBillingDataToDiskEnvVar, true)
  270. }
  271. // GetKubecostNamespace returns the environment variable value for KubecostNamespaceEnvVar which
  272. // represents the namespace the cost model exists in.
  273. func GetKubecostNamespace() string {
  274. return env.Get(KubecostNamespaceEnvVar, "kubecost")
  275. }
  276. // GetPodName returns the name of the current running pod. If this environment variable is not set,
  277. // empty string is returned.
  278. func GetPodName() string {
  279. return env.Get(PodNameEnvVar, "")
  280. }
  281. // GetClusterProfile returns the environment variable value for ClusterProfileEnvVar which
  282. // represents the cluster profile configured for
  283. func GetClusterProfile() string {
  284. return env.Get(ClusterProfileEnvVar, "development")
  285. }
  286. // GetClusterID returns the environment variable value for ClusterIDEnvVar which represents the
  287. // configurable identifier used for multi-cluster metric emission.
  288. func GetClusterID() string {
  289. return env.Get(ClusterIDEnvVar, "")
  290. }
  291. // GetPromClusterFilter returns environment variable value CurrentClusterIdFilterEnabledVar which
  292. // represents additional prometheus filter for all metrics for current cluster id
  293. func GetPromClusterFilter() string {
  294. if env.GetBool(CurrentClusterIdFilterEnabledVar, false) {
  295. return fmt.Sprintf("%s=\"%s\"", GetPromClusterLabel(), GetClusterID())
  296. }
  297. return ""
  298. }
  299. // GetPrometheusEndpoints returns the environment variables values for
  300. // PrometheusServerEndpointEnvVar which represents the prometheus server endpoint used to execute prometheus queries and
  301. // SCRAPE_PrometheusServerEndpointEnvVar which represents prometheus scrape server endpoint used to get scrape config.
  302. func GetPrometheusEndpoints() map[PrometheusType]string {
  303. output := map[PrometheusType]string{
  304. Server: env.Get(Server.envFormat(PrometheusServerEndpointEnvVar), ""),
  305. }
  306. if value := env.Get(Scrape.envFormat(PrometheusServerEndpointEnvVar), ""); value != "" {
  307. output[Scrape] = value
  308. }
  309. return output
  310. }
  311. func GetInsecureSkipVerify(clientType PrometheusType) bool {
  312. return env.GetBool(clientType.envFormat(InsecureSkipVerify), false)
  313. }
  314. // IsAggregateCostModelCacheDisabled returns the environment variable value for DisableAggregateCostModelCache which
  315. // will inform the aggregator on whether to load cached data. Defaults to false
  316. func IsAggregateCostModelCacheDisabled() bool {
  317. return env.GetBool(DisableAggregateCostModelCache, false)
  318. }
  319. // IsRemoteEnabled returns the environment variable value for RemoteEnabledEnvVar which represents whether
  320. // or not remote write is enabled for prometheus for use with SQL backed persistent storage.
  321. func IsRemoteEnabled() bool {
  322. return env.GetBool(RemoteEnabledEnvVar, false)
  323. }
  324. // GetRemotePW returns the environment variable value for RemotePWEnvVar which represents the remote
  325. // persistent storage password.
  326. func GetRemotePW() string {
  327. return env.Get(RemotePWEnvVar, "")
  328. }
  329. // GetSQLAddress returns the environment variable value for SQLAddressEnvVar which represents the SQL
  330. // database address used with remote persistent storage.
  331. func GetSQLAddress() string {
  332. return env.Get(SQLAddressEnvVar, "")
  333. }
  334. // IsUseCSVProvider returns the environment variable value for UseCSVProviderEnvVar which represents
  335. // whether or not the use of a CSV cost provider is enabled.
  336. func IsUseCSVProvider() bool {
  337. return env.GetBool(UseCSVProviderEnvVar, false)
  338. }
  339. // GetCSVRegion returns the environment variable value for CSVRegionEnvVar which represents the
  340. // region configured for a CSV provider.
  341. func GetCSVRegion() string {
  342. return env.Get(CSVRegionEnvVar, "")
  343. }
  344. // GetCSVEndpoint returns the environment variable value for CSVEndpointEnvVar which represents the
  345. // endpoint configured for a S3 CSV provider another than AWS S3.
  346. func GetCSVEndpoint() string {
  347. return env.Get(CSVEndpointEnvVar, "")
  348. }
  349. // GetCSVPath returns the environment variable value for CSVPathEnvVar which represents the key path
  350. // configured for a CSV provider.
  351. func GetCSVPath() string {
  352. return env.Get(CSVPathEnvVar, "")
  353. }
  354. // GetCostAnalyzerVolumeMountPath is an alias of GetConfigPath, which returns the mount path for the
  355. // Cost Analyzer volume, which stores configs, persistent data, etc.
  356. func GetCostAnalyzerVolumeMountPath() string {
  357. return GetConfigPathWithDefault(DefaultConfigMountPath)
  358. }
  359. // GetConfigPath returns the environment variable value for ConfigPathEnvVar which represents the cost
  360. // model configuration path
  361. func GetConfigPathWithDefault(defaultValue string) string {
  362. return env.Get(ConfigPathEnvVar, defaultValue)
  363. }
  364. // GetCloudProviderAPI returns the environment variable value for CloudProviderAPIEnvVar which represents
  365. // the API key provided for the cloud provider.
  366. func GetCloudProviderAPIKey() string {
  367. return env.Get(CloudProviderAPIKeyEnvVar, "")
  368. }
  369. // IsThanosEnabled returns the environment variable value for ThanosEnabledEnvVar which represents whether
  370. // or not thanos is enabled.
  371. func IsThanosEnabled() bool {
  372. return env.GetBool(ThanosEnabledEnvVar, false)
  373. }
  374. // GetThanosQueryUrl returns the environment variable value for ThanosQueryUrlEnvVar which represents the
  375. // target query endpoint for hitting thanos.
  376. func GetThanosQueryUrl() string {
  377. return env.Get(ThanosQueryUrlEnvVar, "")
  378. }
  379. // GetThanosOffset returns the environment variable value for ThanosOffsetEnvVar which represents the total
  380. // amount of time to offset all queries made to thanos.
  381. func GetThanosOffset() string {
  382. return env.Get(ThanosOffsetEnvVar, "3h")
  383. }
  384. // GetThanosMaxSourceResolution returns the environment variable value for ThanosMaxSourceResEnvVar which represents
  385. // the max source resolution to use when querying thanos.
  386. func GetThanosMaxSourceResolution() string {
  387. res := env.Get(ThanosMaxSourceResEnvVar, "raw")
  388. switch res {
  389. case "raw":
  390. return "0s"
  391. case "0s":
  392. fallthrough
  393. case "5m":
  394. fallthrough
  395. case "1h":
  396. return res
  397. default:
  398. return "0s"
  399. }
  400. }
  401. // IsLogCollectionEnabled returns the environment variable value for LogCollectionEnabledEnvVar which represents
  402. // whether or not log collection has been enabled for kubecost deployments.
  403. func IsLogCollectionEnabled() bool {
  404. return env.GetBool(LogCollectionEnabledEnvVar, true)
  405. }
  406. // IsProductAnalyticsEnabled returns the environment variable value for ProductAnalyticsEnabledEnvVar
  407. func IsProductAnalyticsEnabled() bool {
  408. return env.GetBool(ProductAnalyticsEnabledEnvVar, true)
  409. }
  410. // IsErrorReportingEnabled returns the environment variable value for ErrorReportingEnabledEnvVar
  411. func IsErrorReportingEnabled() bool {
  412. return env.GetBool(ErrorReportingEnabledEnvVar, true)
  413. }
  414. // IsValuesReportingEnabled returns the environment variable value for ValuesReportingEnabledEnvVar
  415. func IsValuesReportingEnabled() bool {
  416. return env.GetBool(ValuesReportingEnabledEnvVar, true)
  417. }
  418. // GetMaxQueryConcurrency returns the environment variable value for MaxQueryConcurrencyEnvVar
  419. func GetMaxQueryConcurrency() int {
  420. return env.GetInt(MaxQueryConcurrencyEnvVar, 5)
  421. }
  422. // GetQueryLoggingFile returns a file location if query logging is enabled. Otherwise, empty string
  423. func GetQueryLoggingFile() string {
  424. return env.Get(QueryLoggingFileEnvVar, "")
  425. }
  426. func GetDBBasicAuthUsername(clientType PrometheusType) string {
  427. return env.Get(clientType.envFormat(DBBasicAuthUsername), "")
  428. }
  429. func GetDBBasicAuthUserPassword(clientType PrometheusType) string {
  430. return env.Get(clientType.envFormat(DBBasicAuthPassword), "")
  431. }
  432. func GetDBBearerToken(clientType PrometheusType) string {
  433. return env.Get(clientType.envFormat(DBBearerToken), "")
  434. }
  435. // GetMultiClusterBasicAuthUsername returns the environment variable value for MultiClusterBasicAuthUsername
  436. func GetMultiClusterBasicAuthUsername() string {
  437. return env.Get(MultiClusterBasicAuthUsername, "")
  438. }
  439. // GetMultiClusterBasicAuthPassword returns the environment variable value for MultiClusterBasicAuthPassword
  440. func GetMultiClusterBasicAuthPassword() string {
  441. return env.Get(MultiClusterBasicAuthPassword, "")
  442. }
  443. func GetMultiClusterBearerToken() string {
  444. return env.Get(MultiClusterBearerToken, "")
  445. }
  446. // GetKubeConfigPath returns the environment variable value for KubeConfigPathEnvVar
  447. func GetKubeConfigPath() string {
  448. return env.Get(KubeConfigPathEnvVar, "")
  449. }
  450. // GetUTCOffset returns the environment variable value for UTCOffset
  451. func GetUTCOffset() string {
  452. return env.Get(UTCOffsetEnvVar, "")
  453. }
  454. // GetParsedUTCOffset returns the duration of the configured UTC offset
  455. func GetParsedUTCOffset() time.Duration {
  456. offset, err := timeutil.ParseUTCOffset(GetUTCOffset())
  457. if err != nil {
  458. log.Warnf("Failed to parse UTC offset: %s", err)
  459. return time.Duration(0)
  460. }
  461. return offset
  462. }
  463. // GetKubecostJobName returns the environment variable value for KubecostJobNameEnvVar
  464. func GetKubecostJobName() string {
  465. return env.Get(KubecostJobNameEnvVar, "kubecost")
  466. }
  467. func IsCacheWarmingEnabled() bool {
  468. return env.GetBool(CacheWarmingEnabledEnvVar, true)
  469. }
  470. func IsETLEnabled() bool {
  471. return env.GetBool(ETLEnabledEnvVar, true)
  472. }
  473. func GetETLMaxPrometheusQueryDuration() time.Duration {
  474. dayMins := 60 * 24
  475. mins := time.Duration(env.GetInt64(ETLMaxPrometheusQueryDurationMinutes, int64(dayMins)))
  476. return mins * time.Minute
  477. }
  478. // GetETLResolution determines the resolution of ETL queries. The smaller the
  479. // duration, the higher the resolution; the higher the resolution, the more
  480. // accurate the query results, but the more computationally expensive.
  481. func GetETLResolution() time.Duration {
  482. // Use the configured ETL resolution, or default to
  483. // 5m (i.e. 300s)
  484. secs := time.Duration(env.GetInt64(ETLResolutionSeconds, 300))
  485. return secs * time.Second
  486. }
  487. func LegacyExternalCostsAPIDisabled() bool {
  488. return env.GetBool(LegacyExternalAPIDisabledVar, false)
  489. }
  490. // GetPromClusterLabel returns the environment variable value for PromClusterIDLabel
  491. func GetPromClusterLabel() string {
  492. return env.Get(PromClusterIDLabelEnvVar, "cluster_id")
  493. }
  494. // IsIngestingPodUID returns the env variable from ingestPodUID, which alters the
  495. // contents of podKeys in Allocation
  496. func IsIngestingPodUID() bool {
  497. return env.GetBool(IngestPodUIDEnvVar, false)
  498. }
  499. func GetAllocationNodeLabelsEnabled() bool {
  500. return env.GetBool(AllocationNodeLabelsEnabled, true)
  501. }
  502. var defaultAllocationNodeLabelsIncludeList []string = []string{
  503. "cloud.google.com/gke-nodepool",
  504. "eks.amazonaws.com/nodegroup",
  505. "kubernetes.azure.com/agentpool",
  506. "node.kubernetes.io/instance-type",
  507. "topology.kubernetes.io/region",
  508. "topology.kubernetes.io/zone",
  509. }
  510. func GetAllocationNodeLabelsIncludeList() []string {
  511. // If node labels are not enabled, return an empty list.
  512. if !GetAllocationNodeLabelsEnabled() {
  513. return []string{}
  514. }
  515. list := env.GetList(AllocationNodeLabelsIncludeList, ",")
  516. // If node labels are enabled, but the white list is empty, use defaults.
  517. if len(list) == 0 {
  518. return defaultAllocationNodeLabelsIncludeList
  519. }
  520. return list
  521. }
  522. func GetRegionOverrideList() []string {
  523. regionList := env.GetList(regionOverrideList, ",")
  524. if regionList == nil {
  525. return []string{}
  526. }
  527. return regionList
  528. }
  529. func GetDataRetentionDailyResolutionDays() int64 {
  530. return env.GetInt64(DataRetentionDailyResolutionDaysEnvVar, 15)
  531. }
  532. func IsKubernetesEnabled() bool {
  533. return env.Get(KubernetesEnabledEnvVar, "") != ""
  534. }
  535. func IsCloudCostEnabled() bool {
  536. return env.GetBool(CloudCostEnabledEnvVar, false)
  537. }
  538. func GetCloudCostConfigPath() string {
  539. return env.Get(CloudCostConfigPath, "cloud-integration.json")
  540. }
  541. func GetCloudCostMonthToDateInterval() int {
  542. return env.GetInt(CloudCostMonthToDateIntervalVar, 6)
  543. }
  544. func GetCloudCostRefreshRateHours() int64 {
  545. return env.GetInt64(CloudCostRefreshRateHoursEnvVar, 6)
  546. }
  547. func GetCloudCostQueryWindowDays() int64 {
  548. return env.GetInt64(CloudCostQueryWindowDaysEnvVar, 7)
  549. }
  550. func GetCloudCostRunWindowDays() int64 {
  551. return env.GetInt64(CloudCostRunWindowDaysEnvVar, 3)
  552. }
  553. func GetOCIPricingURL() string {
  554. return env.Get(OCIPricingURL, "https://apexapps.oracle.com/pls/apex/cetools/api/v1/products")
  555. }