costmodelenv.go 23 KB

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