costmodelenv.go 13 KB

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