Skip to content

Commit 4a092c2

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 4a092c2

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

pkg/metrics/server.go

Lines changed: 24 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,7 @@ 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+
server.caBundleCh <- caBundle // This will block if no one is listening
5153
}
5254
}
5355

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

0 commit comments

Comments
 (0)