costmodel.go 13 KB

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