costmodel.go 13 KB

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