costmodelenv.go 22 KB

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