costmodelenv.go 24 KB

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