costmodel.go 15 KB

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