Skip to content

Commit b91c200

Browse files
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 <glipcean@redhat.com>
1 parent fd761db commit b91c200

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

pkg/metrics/server.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"crypto/x509"
99
"errors"
1010
"fmt"
11+
ntoconfig "github.com/openshift/cluster-node-tuning-operator/pkg/config"
1112
"io"
1213
"net/http"
1314
"os"
@@ -47,7 +48,11 @@ func init() {
4748
// it does so only if the CA bundle changed from the current CA bundle on record.
4849
func DumpCA(caBundle string) {
4950
if caBundle != server.caBundle {
50-
server.caBundleCh <- caBundle
51+
select {
52+
case server.caBundleCh <- caBundle:
53+
default:
54+
klog.Infof("Metrics server CA channel not ready, skipping CA bundle update")
55+
}
5156
}
5257
}
5358

@@ -126,6 +131,27 @@ func (Server) Start(ctx context.Context) error {
126131
// and restarted with the current files. Every non-nil return from this function is fatal
127132
// and will restart the whole operator.
128133
func RunServer(port int, ctx context.Context) error {
134+
if ntoconfig.InHyperShift() {
135+
klog.Info("starting metrics server.")
136+
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
137+
router := http.NewServeMux()
138+
router.Handle("/metrics", handler)
139+
srv := &http.Server{
140+
Addr: fmt.Sprintf(":%d", port),
141+
Handler: router,
142+
}
143+
go func() {
144+
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
145+
klog.Errorf("error from metrics server: %v", err)
146+
}
147+
}()
148+
<-ctx.Done()
149+
klog.Info("stopping insecure metrics server")
150+
151+
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
152+
defer cancel()
153+
return srv.Shutdown(shutdownCtx)
154+
}
129155
// Set up and start the file watcher.
130156
watcher, err := fsnotify.NewWatcher()
131157
if watcher == nil || err != nil {

0 commit comments

Comments
 (0)