costmodelenv.go 23 KB

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