costmodel.go 15 KB

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