costmodelenv.go 11 KB

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