costmodelenv.go 14 KB

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