Skip to content

Commit df3314e

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 <[email protected]>
1 parent fd761db commit df3314e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

pkg/metrics/server.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"os"
1414
"time"
1515

16+
ntoconfig "github.com/openshift/cluster-node-tuning-operator/pkg/config"
17+
1618
"k8s.io/klog/v2"
1719

1820
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -47,7 +49,11 @@ func init() {
4749
// it does so only if the CA bundle changed from the current CA bundle on record.
4850
func DumpCA(caBundle string) {
4951
if caBundle != server.caBundle {
50-
server.caBundleCh <- caBundle
52+
select {
53+
case server.caBundleCh <- caBundle:
54+
default:
55+
klog.Infof("Metrics server CA channel not ready, skipping CA bundle update")
56+
}
5157
}
5258
}
5359

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

0 commit comments

Comments
 (0)