From df3314e58cc967f232e4cee1ca56d8a4bc397c4c Mon Sep 17 00:00:00 2001 From: georgelipceanu Date: Tue, 24 Jun 2025 12:44:05 +0100 Subject: [PATCH] Change how the metrics server is run in HyperShift This is to accommodate for the change in the metrics port 60000 from HTTPS to HTTP, which allows the metrics to run without the need for additional permissions to RBAC resources in the kube-system namespace to the HyperShift operator. Signed-off-by: George Lipceanu --- pkg/metrics/server.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/metrics/server.go b/pkg/metrics/server.go index 5bfbd12e2f..cb2bd794c3 100644 --- a/pkg/metrics/server.go +++ b/pkg/metrics/server.go @@ -13,6 +13,8 @@ import ( "os" "time" + ntoconfig "github.com/openshift/cluster-node-tuning-operator/pkg/config" + "k8s.io/klog/v2" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -47,7 +49,11 @@ func init() { // it does so only if the CA bundle changed from the current CA bundle on record. func DumpCA(caBundle string) { if caBundle != server.caBundle { - server.caBundleCh <- caBundle + select { + case server.caBundleCh <- caBundle: + default: + klog.Infof("Metrics server CA channel not ready, skipping CA bundle update") + } } } @@ -126,6 +132,27 @@ func (Server) Start(ctx context.Context) error { // and restarted with the current files. Every non-nil return from this function is fatal // and will restart the whole operator. func RunServer(port int, ctx context.Context) error { + if ntoconfig.InHyperShift() { + klog.Info("starting metrics server.") + handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) + router := http.NewServeMux() + router.Handle("/metrics", handler) + srv := &http.Server{ + Addr: fmt.Sprintf(":%d", port), + Handler: router, + } + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + klog.Errorf("error from metrics server: %v", err) + } + }() + <-ctx.Done() + klog.Info("stopping insecure metrics server") + + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + return srv.Shutdown(shutdownCtx) + } // Set up and start the file watcher. watcher, err := fsnotify.NewWatcher() if watcher == nil || err != nil {