Skip to content

Commit 97ba105

Browse files
authored
pkg/metrics/service-monitor.go: Populate all ports (#1562)
Whenever there are multiple ports in the Service object we take all of them to map to the Endpoints of the ServiceMonitor object. This way we can have one ServiceMonitor per Service with multiple ports exposed.
1 parent 166a073 commit 97ba105

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

pkg/metrics/service-monitor.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func GenerateServiceMonitor(s *v1.Service) *monitoringv1.ServiceMonitor {
5959
for k, v := range s.ObjectMeta.Labels {
6060
labels[k] = v
6161
}
62+
endpoints := populateEndpointsFromServicePorts(s)
6263

6364
return &monitoringv1.ServiceMonitor{
6465
ObjectMeta: metav1.ObjectMeta{
@@ -70,15 +71,19 @@ func GenerateServiceMonitor(s *v1.Service) *monitoringv1.ServiceMonitor {
7071
Selector: metav1.LabelSelector{
7172
MatchLabels: labels,
7273
},
73-
Endpoints: []monitoringv1.Endpoint{
74-
{
75-
Port: s.Spec.Ports[0].Name,
76-
},
77-
},
74+
Endpoints: endpoints,
7875
},
7976
}
8077
}
8178

79+
func populateEndpointsFromServicePorts(s *v1.Service) []monitoringv1.Endpoint {
80+
var endpoints []monitoringv1.Endpoint
81+
for _, port := range s.Spec.Ports {
82+
endpoints = append(endpoints, monitoringv1.Endpoint{Port: port.Name})
83+
}
84+
return endpoints
85+
}
86+
8287
// hasServiceMonitor checks if ServiceMonitor is registered in the cluster.
8388
func hasServiceMonitor(config *rest.Config) (bool, error) {
8489
dc := discovery.NewDiscoveryClientForConfigOrDie(config)

0 commit comments

Comments
 (0)