costmodelenv.go 12 KB

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