costmodelenv.go 9.6 KB

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