costmodelenv.go 11 KB

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