Procházet zdrojové kódy

Make the port that the API listens on configurable.

Continues the work started in https://github.com/opencost/opencost/pull/2283
Closes https://github.com/opencost/opencost/issues/2223

Signed-off-by: Matt Ray <github@mattray.dev>
Matt Ray před 2 roky
rodič
revize
c8cf04aca3
2 změnil soubory, kde provedl 9 přidání a 1 odebrání
  1. 1 1
      pkg/cmd/costmodel/costmodel.go
  2. 8 0
      pkg/env/costmodelenv.go

+ 1 - 1
pkg/cmd/costmodel/costmodel.go

@@ -78,7 +78,7 @@ func Execute(opts *CostModelOpts) error {
 	telemetryHandler := metrics.ResponseMetricMiddleware(rootMux)
 	handler := cors.AllowAll().Handler(telemetryHandler)
 
-	return http.ListenAndServe(":9003", errors.PanicHandlerMiddleware(handler))
+	return http.ListenAndServe(":"+env.GetAPIPort(), errors.PanicHandlerMiddleware(handler))
 }
 
 func StartExportWorker(ctx context.Context, model costmodel.AllocationModel) error {

+ 8 - 0
pkg/env/costmodelenv.go

@@ -11,6 +11,8 @@ import (
 )
 
 const (
+	APIPortEnvVar = "API_PORT"
+
 	AWSAccessKeyIDEnvVar     = "AWS_ACCESS_KEY_ID"
 	AWSAccessKeySecretEnvVar = "AWS_SECRET_ACCESS_KEY"
 	AWSClusterIDEnvVar       = "AWS_CLUSTER_ID"
@@ -147,6 +149,12 @@ func GetExportCSVMaxDays() int {
 	return GetInt(ExportCSVMaxDays, 90)
 }
 
+// GetAPIPort returns the environment variable value for APIPortEnvVar which
+// is the port number the API is available on.
+func GetAPIPort() string {
+	return Get(APIPortEnvVar, "9003")
+}
+
 // GetKubecostConfigBucket returns a file location for a mounted bucket configuration which is used to store
 // a subset of kubecost configurations that require sharing via remote storage.
 func GetKubecostConfigBucket() string {