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..cd839865 100644 --- a/runtime-watcher/main.go +++ b/runtime-watcher/main.go @@ -23,6 +23,7 @@ import ( "os" "github.com/prometheus/client_golang/prometheus/promhttp" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/serializer" @@ -59,16 +60,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 +68,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 {