costmodelenv.go 13 KB

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