Skip to content

Commit cc6cba0

Browse files
authored
Merge pull request #2816 from k8s-infra-cherrypick-robot/cherry-pick-2813-to-release-0.16
[release-0.16] 🐛 Reintroduce AddMetricsServerExtraHandler on manager
2 parents 24c2359 + 0bb3250 commit cc6cba0

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

pkg/manager/internal.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ func (cm *controllerManager) add(r Runnable) error {
179179
return cm.runnables.Add(r)
180180
}
181181

182+
// AddMetricsServerExtraHandler adds extra handler served on path to the http server that serves metrics.
183+
func (cm *controllerManager) AddMetricsServerExtraHandler(path string, handler http.Handler) error {
184+
cm.Lock()
185+
defer cm.Unlock()
186+
if cm.started {
187+
return fmt.Errorf("unable to add new metrics handler because metrics endpoint has already been created")
188+
}
189+
if cm.metricsServer == nil {
190+
cm.GetLogger().Info("warn: metrics server is currently disabled, registering extra handler %q will be ignored", path)
191+
return nil
192+
}
193+
if err := cm.metricsServer.AddExtraHandler(path, handler); err != nil {
194+
return err
195+
}
196+
cm.logger.V(2).Info("Registering metrics http server extra handler", "path", path)
197+
return nil
198+
}
199+
182200
// AddHealthzCheck allows you to add Healthz checker.
183201
func (cm *controllerManager) AddHealthzCheck(name string, check healthz.Checker) error {
184202
cm.Lock()

pkg/manager/manager.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ type Manager interface {
6767
// election was configured.
6868
Elected() <-chan struct{}
6969

70+
// AddMetricsServerExtraHandler adds an extra handler served on path to the http server that serves metrics.
71+
// Might be useful to register some diagnostic endpoints e.g. pprof.
72+
//
73+
// Note that these endpoints are meant to be sensitive and shouldn't be exposed publicly.
74+
//
75+
// If the simple path -> handler mapping offered here is not enough,
76+
// a new http server/listener should be added as Runnable to the manager via Add method.
77+
AddMetricsServerExtraHandler(path string, handler http.Handler) error
78+
7079
// AddHealthzCheck allows you to add Healthz checker
7180
AddHealthzCheck(name string, check healthz.Checker) error
7281

pkg/manager/manager_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,12 @@ var _ = Describe("manger.Manager", func() {
13741374
m, err := New(cfg, opts)
13751375
Expect(err).NotTo(HaveOccurred())
13761376

1377+
// Should error when we add another extra endpoint on the already registered path.
1378+
err = m.AddMetricsServerExtraHandler("/debug", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
1379+
_, _ = w.Write([]byte("Another debug info"))
1380+
}))
1381+
Expect(err).To(HaveOccurred())
1382+
13771383
ctx, cancel := context.WithCancel(context.Background())
13781384
defer cancel()
13791385
go func() {

pkg/metrics/server/server.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ var DefaultBindAddress = ":8080"
4646

4747
// Server is a server that serves metrics.
4848
type Server interface {
49+
// AddExtraHandler adds extra handler served on path to the http server that serves metrics.
50+
AddExtraHandler(path string, handler http.Handler) error
51+
4952
// NeedLeaderElection implements the LeaderElectionRunnable interface, which indicates
5053
// the metrics server doesn't need leader election.
5154
NeedLeaderElection() bool
@@ -179,6 +182,23 @@ func (*defaultServer) NeedLeaderElection() bool {
179182
return false
180183
}
181184

185+
// AddExtraHandler adds extra handler served on path to the http server that serves metrics.
186+
func (s *defaultServer) AddExtraHandler(path string, handler http.Handler) error {
187+
s.mu.Lock()
188+
defer s.mu.Unlock()
189+
if s.options.ExtraHandlers == nil {
190+
s.options.ExtraHandlers = make(map[string]http.Handler)
191+
}
192+
if path == defaultMetricsEndpoint {
193+
return fmt.Errorf("overriding builtin %s endpoint is not allowed", defaultMetricsEndpoint)
194+
}
195+
if _, found := s.options.ExtraHandlers[path]; found {
196+
return fmt.Errorf("can't register extra handler by duplicate path %q on metrics http server", path)
197+
}
198+
s.options.ExtraHandlers[path] = handler
199+
return nil
200+
}
201+
182202
// Start runs the server.
183203
// It will install the metrics related resources depend on the server configuration.
184204
func (s *defaultServer) Start(ctx context.Context) error {

0 commit comments

Comments
 (0)