diff --git a/pkg/manager/internal.go b/pkg/manager/internal.go index 230b78f89c..25d2f21788 100644 --- a/pkg/manager/internal.go +++ b/pkg/manager/internal.go @@ -42,10 +42,10 @@ import ( var log = logf.RuntimeLog.WithName("manager") type controllerManager struct { - // config is the rest.config used to talk to the apiserver. Required. + // config is the rest.config used to talk to the apiserver. Required. config *rest.Config - // scheme is the scheme injected into Controllers, EventHandlers, Sources and Predicates. Defaults + // scheme is the scheme injected into Controllers, EventHandlers, Sources and Predicates. Defaults // to scheme.scheme. scheme *runtime.Scheme @@ -75,6 +75,10 @@ type controllerManager struct { // metricsListener is used to serve prometheus metrics metricsListener net.Listener + // metricsServingDisabled is used to disable the serving of metrics + // by default these are enabled, as the value is false. + metricsServingDisabled bool + mu sync.Mutex started bool errChan chan error @@ -167,7 +171,7 @@ func (cm *controllerManager) GetRESTMapper() meta.RESTMapper { return cm.mapper } -func (cm *controllerManager) serveMetrics(stop <-chan struct{}) { +func (cm *controllerManager) ServeMetrics(stop <-chan struct{}) { handler := promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{ ErrorHandling: promhttp.HTTPErrorOnError, }) @@ -200,8 +204,8 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error { // Metrics should be served whether the controller is leader or not. // (If we don't serve metrics for non-leaders, prometheus will still scrape // the pod but will get a connection refused) - if cm.metricsListener != nil { - go cm.serveMetrics(cm.internalStop) + if cm.metricsListener != nil && !cm.metricsServingDisabled { + go cm.ServeMetrics(cm.internalStop) } if cm.resourceLock != nil { diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 0fb73d14f3..2dd711c0f6 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -46,6 +46,10 @@ type Manager interface { // implements the inject interface - e.g. inject.Client Add(Runnable) error + // ServeMetrics serves the metrics until the stop channel is closed. + // It serves the metrics on `/metrics` on the port configured through the MetricsBindAddress. + ServeMetrics(<-chan struct{}) + // SetFields will set any dependencies on an object for which the object has implemented the inject // interface - e.g. inject.Client. SetFields(interface{}) error @@ -116,6 +120,11 @@ type Options struct { // for serving prometheus metrics MetricsBindAddress string + // MetricsServingDisabled does not serve the metrics. This can be used when + // user wants to serve the metrics separately. + // If not set it is set to false by default. + MetricsServingDisabled bool + // Functions to all for a user to customize the values that will be injected. // 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) { stop := make(chan struct{}) return &controllerManager{ - config: config, - scheme: options.Scheme, - errChan: make(chan error), - cache: cache, - fieldIndexes: cache, - client: writeObj, - recorderProvider: recorderProvider, - resourceLock: resourceLock, - mapper: mapper, - metricsListener: metricsListener, - internalStop: stop, - internalStopper: stop, + config: config, + scheme: options.Scheme, + errChan: make(chan error), + cache: cache, + fieldIndexes: cache, + client: writeObj, + recorderProvider: recorderProvider, + resourceLock: resourceLock, + mapper: mapper, + metricsListener: metricsListener, + metricsServingDisabled: options.MetricsServingDisabled, + internalStop: stop, + internalStopper: stop, }, nil }