costmodelenv.go 25 KB

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