Skip to content

Commit 00d826e

Browse files
committed
✨ pkg/manager/*: Add MetricsServingDisabled option
MetricsServingDisabled option when set to true disables serving metrics by manager.
1 parent 68ae79e commit 00d826e

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

pkg/manager/internal.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ import (
4242
var log = logf.RuntimeLog.WithName("manager")
4343

4444
type controllerManager struct {
45-
// config is the rest.config used to talk to the apiserver. Required.
45+
// config is the rest.config used to talk to the apiserver. Required.
4646
config *rest.Config
4747

48-
// scheme is the scheme injected into Controllers, EventHandlers, Sources and Predicates. Defaults
48+
// scheme is the scheme injected into Controllers, EventHandlers, Sources and Predicates. Defaults
4949
// to scheme.scheme.
5050
scheme *runtime.Scheme
5151

@@ -75,6 +75,10 @@ type controllerManager struct {
7575
// metricsListener is used to serve prometheus metrics
7676
metricsListener net.Listener
7777

78+
// metricsServingDisabled is used to disable the serving of metrics
79+
// by default these are enabled, as the value is false.
80+
metricsServingDisabled bool
81+
7882
mu sync.Mutex
7983
started bool
8084
errChan chan error
@@ -167,7 +171,7 @@ func (cm *controllerManager) GetRESTMapper() meta.RESTMapper {
167171
return cm.mapper
168172
}
169173

170-
func (cm *controllerManager) serveMetrics(stop <-chan struct{}) {
174+
func (cm *controllerManager) ServeMetrics(stop <-chan struct{}) {
171175
handler := promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{
172176
ErrorHandling: promhttp.HTTPErrorOnError,
173177
})
@@ -200,8 +204,8 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
200204
// Metrics should be served whether the controller is leader or not.
201205
// (If we don't serve metrics for non-leaders, prometheus will still scrape
202206
// the pod but will get a connection refused)
203-
if cm.metricsListener != nil {
204-
go cm.serveMetrics(cm.internalStop)
207+
if cm.metricsListener != nil && !cm.metricsServingDisabled {
208+
go cm.ServeMetrics(cm.internalStop)
205209
}
206210

207211
if cm.resourceLock != nil {

pkg/manager/manager.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type Manager interface {
4646
// implements the inject interface - e.g. inject.Client
4747
Add(Runnable) error
4848

49+
// ServeMetrics serves the metrics until the stop channel is closed.
50+
// It serves the metrics on `/metrics` on the port configured through the MetricsBindAddress.
51+
ServeMetrics(<-chan struct{})
52+
4953
// SetFields will set any dependencies on an object for which the object has implemented the inject
5054
// interface - e.g. inject.Client.
5155
SetFields(interface{}) error
@@ -116,6 +120,11 @@ type Options struct {
116120
// for serving prometheus metrics
117121
MetricsBindAddress string
118122

123+
// MetricsServingDisabled does not serve the metrics. This can be used when
124+
// user wants to serve the metrics separately.
125+
// If not set it is set to false by default.
126+
MetricsServingDisabled bool
127+
119128
// Functions to all for a user to customize the values that will be injected.
120129

121130
// NewCache is the function that will create the cache to be used
@@ -211,18 +220,19 @@ func New(config *rest.Config, options Options) (Manager, error) {
211220
stop := make(chan struct{})
212221

213222
return &controllerManager{
214-
config: config,
215-
scheme: options.Scheme,
216-
errChan: make(chan error),
217-
cache: cache,
218-
fieldIndexes: cache,
219-
client: writeObj,
220-
recorderProvider: recorderProvider,
221-
resourceLock: resourceLock,
222-
mapper: mapper,
223-
metricsListener: metricsListener,
224-
internalStop: stop,
225-
internalStopper: stop,
223+
config: config,
224+
scheme: options.Scheme,
225+
errChan: make(chan error),
226+
cache: cache,
227+
fieldIndexes: cache,
228+
client: writeObj,
229+
recorderProvider: recorderProvider,
230+
resourceLock: resourceLock,
231+
mapper: mapper,
232+
metricsListener: metricsListener,
233+
metricsServingDisabled: options.MetricsServingDisabled,
234+
internalStop: stop,
235+
internalStopper: stop,
226236
}, nil
227237
}
228238

0 commit comments

Comments
 (0)