costmodelenv.go 10 KB

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