costmodelenv.go 13 KB

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