costmodelenv.go 12 KB

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