costmodelenv.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package env
  2. import (
  3. "regexp"
  4. "strconv"
  5. "time"
  6. "github.com/kubecost/cost-model/pkg/log"
  7. )
  8. const (
  9. AppVersionEnvVar = "APP_VERSION"
  10. AWSAccessKeyIDEnvVar = "AWS_ACCESS_KEY_ID"
  11. AWSAccessKeySecretEnvVar = "AWS_SECRET_ACCESS_KEY"
  12. AWSClusterIDEnvVar = "AWS_CLUSTER_ID"
  13. AzureStorageAccessKeyEnvVar = "AZURE_STORAGE_ACCESS_KEY"
  14. AzureStorageAccountNameEnvVar = "AZURE_STORAGE_ACCOUNT"
  15. AzureStorageContainerNameEnvVar = "AZURE_STORAGE_CONTAINER"
  16. KubecostNamespaceEnvVar = "KUBECOST_NAMESPACE"
  17. ClusterIDEnvVar = "CLUSTER_ID"
  18. ClusterProfileEnvVar = "CLUSTER_PROFILE"
  19. PrometheusServerEndpointEnvVar = "PROMETHEUS_SERVER_ENDPOINT"
  20. MaxQueryConcurrencyEnvVar = "MAX_QUERY_CONCURRENCY"
  21. QueryLoggingFileEnvVar = "QUERY_LOGGING_FILE"
  22. RemoteEnabledEnvVar = "REMOTE_WRITE_ENABLED"
  23. RemotePWEnvVar = "REMOTE_WRITE_PASSWORD"
  24. SQLAddressEnvVar = "SQL_ADDRESS"
  25. UseCSVProviderEnvVar = "USE_CSV_PROVIDER"
  26. CSVRegionEnvVar = "CSV_REGION"
  27. CSVEndpointEnvVar = "CSV_ENDPOINT"
  28. CSVPathEnvVar = "CSV_PATH"
  29. ConfigPathEnvVar = "CONFIG_PATH"
  30. CloudProviderAPIKeyEnvVar = "CLOUD_PROVIDER_API_KEY"
  31. EmitPodAnnotationsMetricEnvVar = "EMIT_POD_ANNOTATIONS_METRIC"
  32. EmitNamespaceAnnotationsMetricEnvVar = "EMIT_NAMESPACE_ANNOTATIONS_METRIC"
  33. EmitKsmV1MetricsEnvVar = "EMIT_KSM_V1_METRICS"
  34. ThanosEnabledEnvVar = "THANOS_ENABLED"
  35. ThanosQueryUrlEnvVar = "THANOS_QUERY_URL"
  36. ThanosOffsetEnvVar = "THANOS_QUERY_OFFSET"
  37. ThanosMaxSourceResEnvVar = "THANOS_MAX_SOURCE_RESOLUTION"
  38. LogCollectionEnabledEnvVar = "LOG_COLLECTION_ENABLED"
  39. ProductAnalyticsEnabledEnvVar = "PRODUCT_ANALYTICS_ENABLED"
  40. ErrorReportingEnabledEnvVar = "ERROR_REPORTING_ENABLED"
  41. ValuesReportingEnabledEnvVar = "VALUES_REPORTING_ENABLED"
  42. DBBasicAuthUsername = "DB_BASIC_AUTH_USERNAME"
  43. DBBasicAuthPassword = "DB_BASIC_AUTH_PW"
  44. DBBearerToken = "DB_BEARER_TOKEN"
  45. MultiClusterBasicAuthUsername = "MC_BASIC_AUTH_USERNAME"
  46. MultiClusterBasicAuthPassword = "MC_BASIC_AUTH_PW"
  47. MultiClusterBearerToken = "MC_BEARER_TOKEN"
  48. InsecureSkipVerify = "INSECURE_SKIP_VERIFY"
  49. KubeConfigPathEnvVar = "KUBECONFIG_PATH"
  50. UTCOffsetEnvVar = "UTC_OFFSET"
  51. CacheWarmingEnabledEnvVar = "CACHE_WARMING_ENABLED"
  52. ETLEnabledEnvVar = "ETL_ENABLED"
  53. ETLMaxBatchHours = "ETL_MAX_BATCH_HOURS"
  54. ETLResolutionSeconds = "ETL_RESOLUTION_SECONDS"
  55. LegacyExternalAPIDisabledVar = "LEGACY_EXTERNAL_API_DISABLED"
  56. PromClusterIDLabelEnvVar = "PROM_CLUSTER_ID_LABEL"
  57. PricingConfigmapName = "PRICING_CONFIGMAP_NAME"
  58. )
  59. func GetPricingConfigmapName() string {
  60. return Get(PricingConfigmapName, "pricing-configs")
  61. }
  62. // GetAWSAccessKeyID returns the environment variable value for AWSAccessKeyIDEnvVar which represents
  63. // the AWS access key for authentication
  64. func GetAppVersion() string {
  65. return Get(AppVersionEnvVar, "1.86.0")
  66. }
  67. // IsEmitNamespaceAnnotationsMetric returns true if cost-model is configured to emit the kube_namespace_annotations metric
  68. // containing the namespace annotations
  69. func IsEmitNamespaceAnnotationsMetric() bool {
  70. return GetBool(EmitNamespaceAnnotationsMetricEnvVar, false)
  71. }
  72. // IsEmitPodAnnotationsMetric returns true if cost-model is configured to emit the kube_pod_annotations metric containing
  73. // pod annotations.
  74. func IsEmitPodAnnotationsMetric() bool {
  75. return GetBool(EmitPodAnnotationsMetricEnvVar, false)
  76. }
  77. // IsEmitKsmV1Metrics returns true if cost-model is configured to emit all necessary KSM v1
  78. // metrics that were removed in KSM v2
  79. func IsEmitKsmV1Metrics() bool {
  80. return GetBool(EmitKsmV1MetricsEnvVar, true)
  81. }
  82. // GetAWSAccessKeyID returns the environment variable value for AWSAccessKeyIDEnvVar which represents
  83. // the AWS access key for authentication
  84. func GetAWSAccessKeyID() string {
  85. return Get(AWSAccessKeyIDEnvVar, "")
  86. }
  87. // GetAWSAccessKeySecret returns the environment variable value for AWSAccessKeySecretEnvVar which represents
  88. // the AWS access key secret for authentication
  89. func GetAWSAccessKeySecret() string {
  90. return Get(AWSAccessKeySecretEnvVar, "")
  91. }
  92. // GetAWSClusterID returns the environment variable value for AWSClusterIDEnvVar which represents
  93. // an AWS specific cluster identifier.
  94. func GetAWSClusterID() string {
  95. return Get(AWSClusterIDEnvVar, "")
  96. }
  97. func GetAzureStorageAccessKey() string {
  98. return Get(AzureStorageAccessKeyEnvVar, "")
  99. }
  100. func GetAzureStorageAccountName() string {
  101. return Get(AzureStorageAccountNameEnvVar, "")
  102. }
  103. func GetAzureStorageContainerName() string {
  104. return Get(AzureStorageContainerNameEnvVar, "")
  105. }
  106. // GetKubecostNamespace returns the environment variable value for KubecostNamespaceEnvVar which
  107. // represents the namespace the cost model exists in.
  108. func GetKubecostNamespace() string {
  109. return Get(KubecostNamespaceEnvVar, "kubecost")
  110. }
  111. // GetClusterProfile returns the environment variable value for ClusterProfileEnvVar which
  112. // represents the cluster profile configured for
  113. func GetClusterProfile() string {
  114. return Get(ClusterProfileEnvVar, "development")
  115. }
  116. // GetClusterID returns the environment variable value for ClusterIDEnvVar which represents the
  117. // configurable identifier used for multi-cluster metric emission.
  118. func GetClusterID() string {
  119. return Get(ClusterIDEnvVar, "")
  120. }
  121. // GetPrometheusServerEndpoint returns the environment variable value for PrometheusServerEndpointEnvVar which
  122. // represents the prometheus server endpoint used to execute prometheus queries.
  123. func GetPrometheusServerEndpoint() string {
  124. return Get(PrometheusServerEndpointEnvVar, "")
  125. }
  126. func GetInsecureSkipVerify() bool {
  127. return GetBool(InsecureSkipVerify, false)
  128. }
  129. // IsRemoteEnabled returns the environment variable value for RemoteEnabledEnvVar which represents whether
  130. // or not remote write is enabled for prometheus for use with SQL backed persistent storage.
  131. func IsRemoteEnabled() bool {
  132. return GetBool(RemoteEnabledEnvVar, false)
  133. }
  134. // GetRemotePW returns the environment variable value for RemotePWEnvVar which represents the remote
  135. // persistent storage password.
  136. func GetRemotePW() string {
  137. return Get(RemotePWEnvVar, "")
  138. }
  139. // GetSQLAddress returns the environment variable value for SQLAddressEnvVar which represents the SQL
  140. // database address used with remote persistent storage.
  141. func GetSQLAddress() string {
  142. return Get(SQLAddressEnvVar, "")
  143. }
  144. // IsUseCSVProvider returns the environment variable value for UseCSVProviderEnvVar which represents
  145. // whether or not the use of a CSV cost provider is enabled.
  146. func IsUseCSVProvider() bool {
  147. return GetBool(UseCSVProviderEnvVar, false)
  148. }
  149. // GetCSVRegion returns the environment variable value for CSVRegionEnvVar which represents the
  150. // region configured for a CSV provider.
  151. func GetCSVRegion() string {
  152. return Get(CSVRegionEnvVar, "")
  153. }
  154. // GetCSVEndpoint returns the environment variable value for CSVEndpointEnvVar which represents the
  155. // endpoint configured for a S3 CSV provider another than AWS S3.
  156. func GetCSVEndpoint() string {
  157. return Get(CSVEndpointEnvVar, "")
  158. }
  159. // GetCSVPath returns the environment variable value for CSVPathEnvVar which represents the key path
  160. // configured for a CSV provider.
  161. func GetCSVPath() string {
  162. return Get(CSVPathEnvVar, "")
  163. }
  164. // GetConfigPath returns the environment variable value for ConfigPathEnvVar which represents the cost
  165. // model configuration path
  166. func GetConfigPath() string {
  167. return Get(ConfigPathEnvVar, "")
  168. }
  169. // GetConfigPath returns the environment variable value for ConfigPathEnvVar which represents the cost
  170. // model configuration path
  171. func GetConfigPathWithDefault(defaultValue string) string {
  172. return Get(ConfigPathEnvVar, defaultValue)
  173. }
  174. // GetCloudProviderAPI returns the environment variable value for CloudProviderAPIEnvVar which represents
  175. // the API key provided for the cloud provider.
  176. func GetCloudProviderAPIKey() string {
  177. return Get(CloudProviderAPIKeyEnvVar, "")
  178. }
  179. // IsThanosEnabled returns the environment variable value for ThanosEnabledEnvVar which represents whether
  180. // or not thanos is enabled.
  181. func IsThanosEnabled() bool {
  182. return GetBool(ThanosEnabledEnvVar, false)
  183. }
  184. // GetThanosQueryUrl returns the environment variable value for ThanosQueryUrlEnvVar which represents the
  185. // target query endpoint for hitting thanos.
  186. func GetThanosQueryUrl() string {
  187. return Get(ThanosQueryUrlEnvVar, "")
  188. }
  189. // GetThanosOffset returns the environment variable value for ThanosOffsetEnvVar which represents the total
  190. // amount of time to offset all queries made to thanos.
  191. func GetThanosOffset() string {
  192. return Get(ThanosOffsetEnvVar, "3h")
  193. }
  194. // GetThanosMaxSourceResolution returns the environment variable value for ThanosMaxSourceResEnvVar which represents
  195. // the max source resolution to use when querying thanos.
  196. func GetThanosMaxSourceResolution() string {
  197. res := Get(ThanosMaxSourceResEnvVar, "raw")
  198. switch res {
  199. case "raw":
  200. return "0s"
  201. case "0s":
  202. fallthrough
  203. case "5m":
  204. fallthrough
  205. case "1h":
  206. return res
  207. default:
  208. return "0s"
  209. }
  210. }
  211. // IsLogCollectionEnabled returns the environment variable value for LogCollectionEnabledEnvVar which represents
  212. // whether or not log collection has been enabled for kubecost deployments.
  213. func IsLogCollectionEnabled() bool {
  214. return GetBool(LogCollectionEnabledEnvVar, true)
  215. }
  216. // IsProductAnalyticsEnabled returns the environment variable value for ProductAnalyticsEnabledEnvVar
  217. func IsProductAnalyticsEnabled() bool {
  218. return GetBool(ProductAnalyticsEnabledEnvVar, true)
  219. }
  220. // IsErrorReportingEnabled returns the environment variable value for ErrorReportingEnabledEnvVar
  221. func IsErrorReportingEnabled() bool {
  222. return GetBool(ErrorReportingEnabledEnvVar, true)
  223. }
  224. // IsValuesReportingEnabled returns the environment variable value for ValuesReportingEnabledEnvVar
  225. func IsValuesReportingEnabled() bool {
  226. return GetBool(ValuesReportingEnabledEnvVar, true)
  227. }
  228. // GetMaxQueryConcurrency returns the environment variable value for MaxQueryConcurrencyEnvVar
  229. func GetMaxQueryConcurrency() int {
  230. return GetInt(MaxQueryConcurrencyEnvVar, 5)
  231. }
  232. // GetQueryLoggingFile returns a file location if query logging is enabled. Otherwise, empty string
  233. func GetQueryLoggingFile() string {
  234. return Get(QueryLoggingFileEnvVar, "")
  235. }
  236. func GetDBBasicAuthUsername() string {
  237. return Get(DBBasicAuthUsername, "")
  238. }
  239. func GetDBBasicAuthUserPassword() string {
  240. return Get(DBBasicAuthPassword, "")
  241. }
  242. func GetDBBearerToken() string {
  243. return Get(DBBearerToken, "")
  244. }
  245. // GetMultiClusterBasicAuthUsername returns the environemnt variable value for MultiClusterBasicAuthUsername
  246. func GetMultiClusterBasicAuthUsername() string {
  247. return Get(MultiClusterBasicAuthUsername, "")
  248. }
  249. // GetMultiClusterBasicAuthPassword returns the environemnt variable value for MultiClusterBasicAuthPassword
  250. func GetMultiClusterBasicAuthPassword() string {
  251. return Get(MultiClusterBasicAuthPassword, "")
  252. }
  253. func GetMultiClusterBearerToken() string {
  254. return Get(MultiClusterBearerToken, "")
  255. }
  256. // GetKubeConfigPath returns the environment variable value for KubeConfigPathEnvVar
  257. func GetKubeConfigPath() string {
  258. return Get(KubeConfigPathEnvVar, "")
  259. }
  260. // GetUTCOffset returns the environemnt variable value for UTCOffset
  261. func GetUTCOffset() string {
  262. return Get(UTCOffsetEnvVar, "")
  263. }
  264. // GetParsedUTCOffset returns the duration of the configured UTC offset
  265. func GetParsedUTCOffset() time.Duration {
  266. offset := time.Duration(0)
  267. if offsetStr := GetUTCOffset(); offsetStr != "" {
  268. regex := regexp.MustCompile(`^(\+|-)(\d\d):(\d\d)$`)
  269. match := regex.FindStringSubmatch(offsetStr)
  270. if match == nil {
  271. log.Warningf("Illegal UTC offset: %s", offsetStr)
  272. return offset
  273. }
  274. sig := 1
  275. if match[1] == "-" {
  276. sig = -1
  277. }
  278. hrs64, _ := strconv.ParseInt(match[2], 10, 64)
  279. hrs := sig * int(hrs64)
  280. mins64, _ := strconv.ParseInt(match[3], 10, 64)
  281. mins := sig * int(mins64)
  282. offset = time.Duration(hrs)*time.Hour + time.Duration(mins)
  283. }
  284. return offset
  285. }
  286. func IsCacheWarmingEnabled() bool {
  287. return GetBool(CacheWarmingEnabledEnvVar, true)
  288. }
  289. func IsETLEnabled() bool {
  290. return GetBool(ETLEnabledEnvVar, true)
  291. }
  292. // GetETLMaxBatchDuration limits the window duration of the most expensive ETL
  293. // queries to a maximum batch size, such that queries can be tuned to avoid
  294. // timeout for large windows; e.g. if a 24h query is expected to timeout, but
  295. // a 6h query is expected to complete in 1m, then 6h could be a good value.
  296. func GetETLMaxBatchDuration() time.Duration {
  297. // Default to 6h
  298. hrs := time.Duration(GetInt64(ETLMaxBatchHours, 6))
  299. return hrs * time.Hour
  300. }
  301. // GetETLResolution determines the resolution of ETL queries. The smaller the
  302. // duration, the higher the resolution; the higher the resolution, the more
  303. // accurate the query results, but the more computationally expensive. This
  304. // value is always 1m for Prometheus, but is configurable for Thanos.
  305. func GetETLResolution() time.Duration {
  306. // If Thanos is not enabled, hard-code to 1m resolution
  307. if !IsThanosEnabled() {
  308. return 60 * time.Second
  309. }
  310. // Thanos is enabled, so use the configured ETL resolution, or default to
  311. // 5m (i.e. 300s)
  312. secs := time.Duration(GetInt64(ETLResolutionSeconds, 300))
  313. return secs * time.Second
  314. }
  315. func LegacyExternalCostsAPIDisabled() bool {
  316. return GetBool(LegacyExternalAPIDisabledVar, false)
  317. }
  318. // GetPromClusterLabel returns the environemnt variable value for PromClusterIDLabel
  319. func GetPromClusterLabel() string {
  320. return Get(PromClusterIDLabelEnvVar, "cluster_id")
  321. }