From 125fe49506777e171f8c17fc394d745778348da2 Mon Sep 17 00:00:00 2001 From: Benjamin Lindner Date: Fri, 8 Dec 2023 12:20:06 +0100 Subject: [PATCH 1/2] feat: Parse metrics port from env --- .../internal/serverconfig/config.go | 35 ++++++++++++++----- runtime-watcher/main.go | 22 ++++++------ 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/runtime-watcher/internal/serverconfig/config.go b/runtime-watcher/internal/serverconfig/config.go index 838ed97c..18e773a0 100644 --- a/runtime-watcher/internal/serverconfig/config.go +++ b/runtime-watcher/internal/serverconfig/config.go @@ -10,18 +10,21 @@ import ( ) const ( - minPort, maxPort = 1, 65535 - defaultPort = 8443 - envWebhookPort = "WEBHOOK_PORT" - envCACert = "CA_CERT" - envTLSCert = "TLS_CERT" - envTLSKey = "TLS_KEY" - envKCPAddress = "KCP_ADDR" - envKCPContract = "KCP_CONTRACT" + minPort, maxPort = 1, 65535 + defaultWebhookPort = 8443 + defaultMetricsPort = 2112 + envWebhookPort = "WEBHOOK_PORT" + envMetricsPort = "METRICS_PORT" + envCACert = "CA_CERT" + envTLSCert = "TLS_CERT" + envTLSKey = "TLS_KEY" + envKCPAddress = "KCP_ADDR" + envKCPContract = "KCP_CONTRACT" ) type ServerConfig struct { Port int + MetricsPort int CACertPath string TLSCertPath string TLSKeyPath string @@ -34,7 +37,7 @@ func ParseFromEnv(logger logr.Logger) (ServerConfig, error) { config := ServerConfig{} - config.Port = defaultPort + config.Port = defaultWebhookPort webhookPort, found := os.LookupEnv(envWebhookPort) if found { port, err := strconv.Atoi(webhookPort) @@ -48,6 +51,20 @@ func ParseFromEnv(logger logr.Logger) (ServerConfig, error) { } } + config.MetricsPort = defaultMetricsPort + metricsPort, found := os.LookupEnv(envMetricsPort) + if found { + port, err := strconv.Atoi(metricsPort) + if err != nil { + logger.Error(err, flagError(envMetricsPort).Error()) + } + if err = validatePortRange(port); err != nil { + logger.Error(err, flagError(envMetricsPort).Error()) + } else { + config.MetricsPort = port + } + } + config.CACertPath = os.Getenv(envCACert) if config.CACertPath == "" { return config, flagError(envCACert) diff --git a/runtime-watcher/main.go b/runtime-watcher/main.go index 0e684ae0..84780ec2 100644 --- a/runtime-watcher/main.go +++ b/runtime-watcher/main.go @@ -19,10 +19,10 @@ package main import ( "flag" "fmt" + "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" "os" - "github.com/prometheus/client_golang/prometheus/promhttp" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/serializer" @@ -59,16 +59,6 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - http.Handle("/metrics", promhttp.Handler()) - metricsServer := &http.Server{ - Addr: ":2112", - ReadHeaderTimeout: internal.HTTPTimeout, - } - err := metricsServer.ListenAndServe() - if err != nil { - logger.Error(err, "failed to wire up metrics endpoint") - } - logger.Info("starting the Runtime Watcher", "Version:", buildVersion) config, err := serverconfig.ParseFromEnv(logger) @@ -77,6 +67,16 @@ func main() { return } + http.Handle("/metrics", promhttp.Handler()) + metricsServer := &http.Server{ + Addr: fmt.Sprintf(":%d", config.MetricsPort), + ReadHeaderTimeout: internal.HTTPTimeout, + } + err = metricsServer.ListenAndServe() + if err != nil { + logger.Error(err, "failed to wire up metrics endpoint") + } + restConfig := ctrl.GetConfigOrDie() restClient, err := client.New(restConfig, client.Options{}) if err != nil { From 98e7495155713f18b73cbf1eda1873d17532fae2 Mon Sep 17 00:00:00 2001 From: Benjamin Lindner Date: Fri, 8 Dec 2023 12:25:38 +0100 Subject: [PATCH 2/2] gofmt --- runtime-watcher/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime-watcher/main.go b/runtime-watcher/main.go index 84780ec2..cd839865 100644 --- a/runtime-watcher/main.go +++ b/runtime-watcher/main.go @@ -19,10 +19,11 @@ package main import ( "flag" "fmt" - "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" "os" + "github.com/prometheus/client_golang/prometheus/promhttp" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/serializer"