costmodelenv.go 25 KB

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