2
0

costmodel.go 13 KB

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