costmodelenv.go 22 KB

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