costmodel.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package env
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/env"
  5. )
  6. // FilePaths
  7. const (
  8. ClusterInfoFile = "cluster-info.json"
  9. ClusterCacheFile
  10. GCPAuthSecretFile = "key.json"
  11. MetricConfigFile = "metrics.json"
  12. DefaultLocalCollectorDir = "collector"
  13. )
  14. // Env Variables
  15. const (
  16. // Open configs
  17. // We assume that Kubernetes is enabled if there is a KUBERNETES_PORT environment variable present
  18. KubernetesEnabledEnvVar = "KUBERNETES_PORT"
  19. // Cloud Provider
  20. AWSAccessKeyIDEnvVar = "AWS_ACCESS_KEY_ID"
  21. AWSAccessKeySecretEnvVar = "AWS_SECRET_ACCESS_KEY"
  22. AWSClusterIDEnvVar = "AWS_CLUSTER_ID"
  23. AWSPricingURL = "AWS_PRICING_URL"
  24. AWSECSPricingURLOverride = "AWS_ECS_PRICING_URL"
  25. AlibabaAccessKeyIDEnvVar = "ALIBABA_ACCESS_KEY_ID"
  26. AlibabaAccessKeySecretEnvVar = "ALIBABA_SECRET_ACCESS_KEY"
  27. AzureOfferIDEnvVar = "AZURE_OFFER_ID"
  28. AzureBillingAccountEnvVar = "AZURE_BILLING_ACCOUNT"
  29. // Azure rate card filter environment variables
  30. AzureLocaleEnvVar = "AZURE_LOCALE"
  31. AzureCurrencyEnvVar = "AZURE_CURRENCY"
  32. AzureRegionInfoEnvVar = "AZURE_REGION_INFO"
  33. // Currently being used for OCI and DigitalOcean
  34. ProviderPricingURL = "PROVIDER_PRICING_URL"
  35. ClusterProfileEnvVar = "CLUSTER_PROFILE"
  36. RemoteEnabledEnvVar = "REMOTE_WRITE_ENABLED"
  37. RemotePWEnvVar = "REMOTE_WRITE_PASSWORD"
  38. SQLAddressEnvVar = "SQL_ADDRESS"
  39. UseCSVProviderEnvVar = "USE_CSV_PROVIDER"
  40. UseCustomProviderEnvVar = "USE_CUSTOM_PROVIDER"
  41. CSVRegionEnvVar = "CSV_REGION"
  42. CSVEndpointEnvVar = "CSV_ENDPOINT"
  43. CSVPathEnvVar = "CSV_PATH"
  44. CloudProviderAPIKeyEnvVar = "CLOUD_PROVIDER_API_KEY"
  45. CollectorDataSourceEnabledEnvVar = "COLLECTOR_DATA_SOURCE_ENABLED"
  46. LocalCollectorDirectoryEnvVar = "LOCAL_COLLECTOR_DIRECTORY"
  47. EmitPodAnnotationsMetricEnvVar = "EMIT_POD_ANNOTATIONS_METRIC"
  48. EmitNamespaceAnnotationsMetricEnvVar = "EMIT_NAMESPACE_ANNOTATIONS_METRIC"
  49. EmitDeprecatedMetrics = "EMIT_DEPRECATED_METRICS"
  50. EmitKsmV1MetricsEnvVar = "EMIT_KSM_V1_METRICS"
  51. EmitKsmV1MetricsOnly = "EMIT_KSM_V1_METRICS_ONLY"
  52. LogCollectionEnabledEnvVar = "LOG_COLLECTION_ENABLED"
  53. ProductAnalyticsEnabledEnvVar = "PRODUCT_ANALYTICS_ENABLED"
  54. ErrorReportingEnabledEnvVar = "ERROR_REPORTING_ENABLED"
  55. ValuesReportingEnabledEnvVar = "VALUES_REPORTING_ENABLED"
  56. PricingConfigmapName = "PRICING_CONFIGMAP_NAME"
  57. MetricsConfigmapName = "METRICS_CONFIGMAP_NAME"
  58. ClusterInfoFileEnabledEnvVar = "CLUSTER_INFO_FILE_ENABLED"
  59. IngestPodUIDEnvVar = "INGEST_POD_UID"
  60. AllocationNodeLabelsEnabled = "ALLOCATION_NODE_LABELS_ENABLED"
  61. AssetIncludeLocalDiskCostEnvVar = "ASSET_INCLUDE_LOCAL_DISK_COST"
  62. regionOverrideList = "REGION_OVERRIDE_LIST"
  63. ExportCSVFile = "EXPORT_CSV_FILE"
  64. ExportCSVLabelsList = "EXPORT_CSV_LABELS_LIST"
  65. ExportCSVLabelsAll = "EXPORT_CSV_LABELS_ALL"
  66. ExportCSVMaxDays = "EXPORT_CSV_MAX_DAYS"
  67. CarbonEstimatesEnabledEnvVar = "CARBON_ESTIMATES_ENABLED"
  68. KubernetesResourceAccessEnvVar = "KUBERNETES_RESOURCE_ACCESS"
  69. UseCacheV1 = "USE_CACHE_V1"
  70. // Cloud provider override
  71. CloudProviderVar = "CLOUD_PROVIDER"
  72. // MCP Server
  73. MCPServerEnabledEnvVar = "MCP_SERVER_ENABLED"
  74. MCPHTTPPortEnvVar = "MCP_HTTP_PORT"
  75. // Metrics Emitter
  76. MetricsEmitterQueryWindowEnvVar = "METRICS_EMITTER_QUERY_WINDOW"
  77. )
  78. func GetGCPAuthSecretFilePath() string {
  79. return env.GetPathFromConfig(GCPAuthSecretFile)
  80. }
  81. func GetExportCSVFile() string {
  82. return env.Get(ExportCSVFile, "")
  83. }
  84. func GetExportCSVLabelsAll() bool {
  85. return env.GetBool(ExportCSVLabelsAll, false)
  86. }
  87. func GetExportCSVLabelsList() []string {
  88. return env.GetList(ExportCSVLabelsList, ",")
  89. }
  90. func GetExportCSVMaxDays() int {
  91. return env.GetInt(ExportCSVMaxDays, 90)
  92. }
  93. // IsClusterInfoFileEnabled returns true if the cluster info is read from a file or pulled from the local
  94. // cloud provider and kubernetes.
  95. func IsClusterInfoFileEnabled() bool {
  96. return env.GetBool(ClusterInfoFileEnabledEnvVar, false)
  97. }
  98. func GetClusterInfoFilePath() string {
  99. return env.GetPathFromConfig(ClusterInfoFile)
  100. }
  101. func GetClusterCacheFilePath() string {
  102. return env.GetPathFromConfig(ClusterCacheFile)
  103. }
  104. func GetPricingConfigmapName() string {
  105. return env.Get(PricingConfigmapName, "pricing-configs")
  106. }
  107. func GetMetricsConfigmapName() string {
  108. return env.Get(MetricsConfigmapName, "metrics-config")
  109. }
  110. // IsEmitNamespaceAnnotationsMetric returns true if cost-model is configured to emit the kube_namespace_annotations metric
  111. // containing the namespace annotations
  112. func IsEmitNamespaceAnnotationsMetric() bool {
  113. return env.GetBool(EmitNamespaceAnnotationsMetricEnvVar, false)
  114. }
  115. // IsEmitPodAnnotationsMetric returns true if cost-model is configured to emit the kube_pod_annotations metric containing
  116. // pod annotations.
  117. func IsEmitPodAnnotationsMetric() bool {
  118. return env.GetBool(EmitPodAnnotationsMetricEnvVar, false)
  119. }
  120. // IsEmitKsmV1Metrics returns true if cost-model is configured to emit all necessary KSM v1
  121. // metrics that were removed in KSM v2
  122. func IsEmitKsmV1Metrics() bool {
  123. return env.GetBool(EmitKsmV1MetricsEnvVar, true)
  124. }
  125. func IsEmitKsmV1MetricsOnly() bool {
  126. return env.GetBool(EmitKsmV1MetricsOnly, false)
  127. }
  128. func IsEmitDeprecatedMetrics() bool {
  129. return env.GetBool(EmitDeprecatedMetrics, false)
  130. }
  131. // GetAWSAccessKeyID returns the environment variable value for AWSAccessKeyIDEnvVar which represents
  132. // the AWS access key for authentication
  133. func GetAWSAccessKeyID() string {
  134. return env.Get(AWSAccessKeyIDEnvVar, "")
  135. }
  136. // GetAWSAccessKeySecret returns the environment variable value for AWSAccessKeySecretEnvVar which represents
  137. // the AWS access key secret for authentication
  138. func GetAWSAccessKeySecret() string {
  139. return env.Get(AWSAccessKeySecretEnvVar, "")
  140. }
  141. // GetAWSClusterID returns the environment variable value for AWSClusterIDEnvVar which represents
  142. // an AWS specific cluster identifier.
  143. func GetAWSClusterID() string {
  144. return env.Get(AWSClusterIDEnvVar, "")
  145. }
  146. // GetAWSPricingURL returns an optional alternative URL to fetch AWS pricing data from; for use in airgapped environments
  147. func GetAWSPricingURL() string {
  148. return env.Get(AWSPricingURL, "")
  149. }
  150. // GetAWSECSPricingURLOverride returns an optional alternative URL to fetch AmazonECS pricing data from; for use in airgapped environments
  151. func GetAWSECSPricingURLOverride() string {
  152. return env.Get(AWSECSPricingURLOverride, "")
  153. }
  154. // GetAlibabaAccessKeyID returns the environment variable value for AlibabaAccessKeyIDEnvVar which represents
  155. // the Alibaba access key for authentication
  156. func GetAlibabaAccessKeyID() string {
  157. return env.Get(AlibabaAccessKeyIDEnvVar, "")
  158. }
  159. // GetAlibabaAccessKeySecret returns the environment variable value for AlibabaAccessKeySecretEnvVar which represents
  160. // the Alibaba access key secret for authentication
  161. func GetAlibabaAccessKeySecret() string {
  162. return env.Get(AlibabaAccessKeySecretEnvVar, "")
  163. }
  164. // GetAzureOfferID returns the environment variable value for AzureOfferIDEnvVar which represents
  165. // the Azure offer ID for determining prices.
  166. func GetAzureOfferID() string {
  167. return env.Get(AzureOfferIDEnvVar, "")
  168. }
  169. // GetAzureBillingAccount returns the environment variable value for
  170. // AzureBillingAccountEnvVar which represents the Azure billing
  171. // account for determining prices. If this is specified
  172. // customer-specific prices will be downloaded from the consumption
  173. // price sheet API.
  174. func GetAzureBillingAccount() string {
  175. return env.Get(AzureBillingAccountEnvVar, "")
  176. }
  177. // GetAzureLocale returns the environment variable value for AzureLocaleEnvVar which represents
  178. // the Azure rate card locale filter. Defaults to "en-US" if not specified.
  179. func GetAzureLocale() string {
  180. return env.Get(AzureLocaleEnvVar, "en-US")
  181. }
  182. // GetAzureCurrency returns the environment variable value for AzureCurrencyEnvVar which represents
  183. // the Azure rate card currency filter. This overrides the default currency from config if specified.
  184. func GetAzureCurrency() string {
  185. return env.Get(AzureCurrencyEnvVar, "")
  186. }
  187. // GetAzureRegionInfo returns the environment variable value for AzureRegionInfoEnvVar which represents
  188. // the Azure rate card region filter. This overrides the default region from config if specified.
  189. func GetAzureRegionInfo() string {
  190. return env.Get(AzureRegionInfoEnvVar, "")
  191. }
  192. // IsAzureDownloadBillingDataToDisk returns the environment variable value for
  193. // AzureDownloadBillingDataToDiskEnvVar which indicates whether the Azure
  194. // Billing Data should be held in memory or written to disk.
  195. func IsAzureDownloadBillingDataToDisk() bool {
  196. return env.GetBool(AzureDownloadBillingDataToDiskEnvVar, true)
  197. }
  198. // GetClusterProfile returns the environment variable value for ClusterProfileEnvVar which
  199. // represents the cluster profile configured for
  200. func GetClusterProfile() string {
  201. return env.Get(ClusterProfileEnvVar, "development")
  202. }
  203. // IsRemoteEnabled returns the environment variable value for RemoteEnabledEnvVar which represents whether
  204. // or not remote write is enabled for prometheus for use with SQL backed persistent storage.
  205. func IsRemoteEnabled() bool {
  206. return env.GetBool(RemoteEnabledEnvVar, false)
  207. }
  208. // GetRemotePW returns the environment variable value for RemotePWEnvVar which represents the remote
  209. // persistent storage password.
  210. func GetRemotePW() string {
  211. return env.Get(RemotePWEnvVar, "")
  212. }
  213. // GetSQLAddress returns the environment variable value for SQLAddressEnvVar which represents the SQL
  214. // database address used with remote persistent storage.
  215. func GetSQLAddress() string {
  216. return env.Get(SQLAddressEnvVar, "")
  217. }
  218. // IsUseCSVProvider returns the environment variable value for UseCSVProviderEnvVar which represents
  219. // whether or not the use of a CSV cost provider is enabled.
  220. func IsUseCSVProvider() bool {
  221. return env.GetBool(UseCSVProviderEnvVar, false)
  222. }
  223. // IsUseCustomProvider returns the environment variable value for UseCustomProviderEnvVar which represents
  224. // whether or not the use of a custom cost provider is enabled.
  225. func IsUseCustomProvider() bool {
  226. return env.GetBool(UseCustomProviderEnvVar, false)
  227. }
  228. // GetCSVRegion returns the environment variable value for CSVRegionEnvVar which represents the
  229. // region configured for a CSV provider.
  230. func GetCSVRegion() string {
  231. return env.Get(CSVRegionEnvVar, "")
  232. }
  233. // GetCSVEndpoint returns the environment variable value for CSVEndpointEnvVar which represents the
  234. // endpoint configured for a S3 CSV provider another than AWS S3.
  235. func GetCSVEndpoint() string {
  236. return env.Get(CSVEndpointEnvVar, "")
  237. }
  238. // GetCSVPath returns the environment variable value for CSVPathEnvVar which represents the key path
  239. // configured for a CSV provider.
  240. func GetCSVPath() string {
  241. return env.Get(CSVPathEnvVar, "")
  242. }
  243. // GetCloudProviderAPI returns the environment variable value for CloudProviderAPIEnvVar which represents
  244. // the API key provided for the cloud provider.
  245. func GetCloudProviderAPIKey() string {
  246. return env.Get(CloudProviderAPIKeyEnvVar, "")
  247. }
  248. // IsCollectorDataSourceEnabeled returns the environment variable which enables a source.OpencostDatasource which does not use uses Prometheus
  249. func IsCollectorDataSourceEnabled() bool {
  250. return env.GetBool(CollectorDataSourceEnabledEnvVar, false)
  251. }
  252. // IsLogCollectionEnabled returns the environment variable value for LogCollectionEnabledEnvVar which represents
  253. // whether or not log collection has been enabled for kubecost deployments.
  254. func IsLogCollectionEnabled() bool {
  255. return env.GetBool(LogCollectionEnabledEnvVar, true)
  256. }
  257. // IsProductAnalyticsEnabled returns the environment variable value for ProductAnalyticsEnabledEnvVar
  258. func IsProductAnalyticsEnabled() bool {
  259. return env.GetBool(ProductAnalyticsEnabledEnvVar, true)
  260. }
  261. // IsErrorReportingEnabled returns the environment variable value for ErrorReportingEnabledEnvVar
  262. func IsErrorReportingEnabled() bool {
  263. return env.GetBool(ErrorReportingEnabledEnvVar, true)
  264. }
  265. // IsValuesReportingEnabled returns the environment variable value for ValuesReportingEnabledEnvVar
  266. func IsValuesReportingEnabled() bool {
  267. return env.GetBool(ValuesReportingEnabledEnvVar, true)
  268. }
  269. // IsIngestingPodUID returns the env variable from ingestPodUID, which alters the
  270. // contents of podKeys in Allocation
  271. func IsIngestingPodUID() bool {
  272. return env.GetBool(IngestPodUIDEnvVar, false)
  273. }
  274. func IsAllocationNodeLabelsEnabled() bool {
  275. return env.GetBool(AllocationNodeLabelsEnabled, true)
  276. }
  277. func IsAssetIncludeLocalDiskCost() bool {
  278. return env.GetBool(AssetIncludeLocalDiskCostEnvVar, true)
  279. }
  280. func GetRegionOverrideList() []string {
  281. regionList := env.GetList(regionOverrideList, ",")
  282. if regionList == nil {
  283. return []string{}
  284. }
  285. return regionList
  286. }
  287. func IsKubernetesEnabled() bool {
  288. return env.Get(KubernetesEnabledEnvVar, "") != ""
  289. }
  290. func GetOCIPricingURL() string {
  291. return env.Get(ProviderPricingURL, "https://apexapps.oracle.com/pls/apex/cetools/api/v1/products")
  292. }
  293. func IsCarbonEstimatesEnabled() bool {
  294. return env.GetBool(CarbonEstimatesEnabledEnvVar, false)
  295. }
  296. // HasKubernetesResourceAccess can be set to false if Opencost is run without access to the kubernetes resources
  297. func HasKubernetesResourceAccess() bool { return env.GetBool(KubernetesResourceAccessEnvVar, true) }
  298. // GetUseCacheV1 is a temporary flag to allow users to opt-in to using the old cache
  299. // Mainly for comparison purposes
  300. func GetUseCacheV1() bool {
  301. return env.GetBool(UseCacheV1, false)
  302. }
  303. // GetCloudProvider returns the explicitly set cloud provider from environment variable
  304. func GetCloudProvider() string {
  305. return env.Get(CloudProviderVar, "")
  306. }
  307. func GetMetricConfigFile() string {
  308. return env.GetPathFromConfig(MetricConfigFile)
  309. }
  310. func GetLocalCollectorDirectory() string {
  311. dir := env.Get(LocalCollectorDirectoryEnvVar, DefaultLocalCollectorDir)
  312. return env.GetPathFromConfig(dir)
  313. }
  314. func GetDOKSPricingURL() string {
  315. return env.Get(ProviderPricingURL, "https://api.digitalocean.com/v2/billing/pricing")
  316. }
  317. // IsMCPServerEnabled returns the environment variable value for MCPServerEnabledEnvVar which represents
  318. // whether or not the MCP server is enabled.
  319. func IsMCPServerEnabled() bool {
  320. return env.GetBool(MCPServerEnabledEnvVar, true)
  321. }
  322. // GetMCPHTTPPort returns the environment variable value for MCPHTTPPortEnvVar which represents
  323. // the HTTP port for the MCP server.
  324. func GetMCPHTTPPort() int {
  325. return env.GetInt(MCPHTTPPortEnvVar, 8081)
  326. }
  327. // GetMetricsEmitterQueryWindow returns the time window for the metrics emitter
  328. // to query historical data. This controls the time range used in ComputeCostData queries.
  329. // Default is 2m.
  330. func GetMetricsEmitterQueryWindow() time.Duration {
  331. return env.GetDuration(MetricsEmitterQueryWindowEnvVar, 2*time.Minute)
  332. }