costmodelenv.go 11 KB

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