Skip to content

Commit 1018749

Browse files
alouriehasbro17
authored andcommitted
pkg/metrics: add an updater function to CreateServiceMonitors (#1991)
With this addition it would be possible to provide a list of updater functions to modify the ServiceMonitor after they are created and before they are added to k8. This can be used for scenarios like updating the labels on the ServiceMonitor, changing metadata or updating exposed endpoints (service port); or literally any other change that a user might need. Signed-off-by: Alex Lourie <[email protected]>
1 parent c32710d commit 1018749

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

hack/tests/sanity-check.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set -ex
33

44
go vet ./...
5+
go fmt ./...
56
./hack/check_license.sh
67
./hack/check_error_log_msg_format.sh
78

pkg/metrics/service-monitor.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ import (
2828

2929
var ErrServiceMonitorNotPresent = fmt.Errorf("no ServiceMonitor registered with the API")
3030

31+
type ServiceMonitorUpdater func(*monitoringv1.ServiceMonitor) error
32+
3133
// CreateServiceMonitors creates ServiceMonitors objects based on an array of Service objects.
3234
// If CR ServiceMonitor is not registered in the Cluster it will not attempt at creating resources.
33-
func CreateServiceMonitors(config *rest.Config, ns string, services []*v1.Service) ([]*monitoringv1.ServiceMonitor, error) {
35+
func CreateServiceMonitors(config *rest.Config, ns string, services []*v1.Service, updaters ...ServiceMonitorUpdater) ([]*monitoringv1.ServiceMonitor, error) {
3436
// check if we can even create ServiceMonitors
3537
exists, err := hasServiceMonitor(config)
3638
if err != nil {
@@ -48,6 +50,12 @@ func CreateServiceMonitors(config *rest.Config, ns string, services []*v1.Servic
4850
continue
4951
}
5052
sm := GenerateServiceMonitor(s)
53+
for _, update := range updaters {
54+
if err := update(sm); err != nil {
55+
return nil, err
56+
}
57+
}
58+
5159
smc, err := mclient.ServiceMonitors(ns).Create(sm)
5260
if err != nil {
5361
return serviceMonitors, err

0 commit comments

Comments
 (0)