pkg/metrics/service-monitor.go: Populate all ports
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.
@@ -59,6 +59,7 @@ func GenerateServiceMonitor(s *v1.Service) *monitoringv1.ServiceMonitor {
for k, v := range s.ObjectMeta.Labels {
labels[k] = v
}
+ endpoints := populateEndpointsFromServicePorts(s)
return &monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
@@ -70,15 +71,19 @@ func GenerateServiceMonitor(s *v1.Service) *monitoringv1.ServiceMonitor {
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
- Endpoints: []monitoringv1.Endpoint{
- {
- Port: s.Spec.Ports[0].Name,
- },
- },
+ Endpoints: endpoints,
},
}
}
+func populateEndpointsFromServicePorts(s *v1.Service) []monitoringv1.Endpoint {
+ var endpoints []monitoringv1.Endpoint
+ for _, port := range s.Spec.Ports {
+ endpoints = append(endpoints, monitoringv1.Endpoint{Port: port.Name})
+ }
+ return endpoints
+}
+
// hasServiceMonitor checks if ServiceMonitor is registered in the cluster.
func hasServiceMonitor(config *rest.Config) (bool, error) {
dc := discovery.NewDiscoveryClientForConfigOrDie(config)
