costmodelenv.go 13 KB

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