costmodel.go 15 KB

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