Skip to content

✨ pkg/manager/*: Add MetricsServingDisabled option #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 22 additions & 12 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down