diff --git a/lib/promscrape/discovery/kubernetes/api_watcher.go b/lib/promscrape/discovery/kubernetes/api_watcher.go index fe9fb071d..92a9a6cd6 100644 --- a/lib/promscrape/discovery/kubernetes/api_watcher.go +++ b/lib/promscrape/discovery/kubernetes/api_watcher.go @@ -74,6 +74,11 @@ func newAPIWatcher(apiServer string, ac *promauth.Config, sdc *SDConfig, swcFunc func (aw *apiWatcher) mustStart() { aw.gw.startWatchersForRole(aw.role, aw) + if aw.role == "endpoints" || aw.role == "endpointslices" { + // endpoints and endpointslices watchers query pod and service objects. So start watchers for these roles as well. + aw.gw.startWatchersForRole("pod", nil) + aw.gw.startWatchersForRole("service", nil) + } } func (aw *apiWatcher) mustStop() { @@ -280,7 +285,6 @@ func (gw *groupWatcher) getObjectByRole(role, namespace, name string) object { func (gw *groupWatcher) getCachedObjectByRole(role, namespace, name string) object { key := namespace + "/" + name - gw.startWatchersForRole(role, nil) uws := gw.getURLWatchers() for _, uw := range uws { if uw.role != role { @@ -309,9 +313,6 @@ func (gw *groupWatcher) refreshEndpointsLabels(namespace, key string) { } func (gw *groupWatcher) refreshObjectLabels(role, namespace, key string) { - // There is no need in starting url watcher for the given role, - // since there is no (namespace, key) object yet for this role. - // gw.startWatchersForRole(role, nil) uws := gw.getURLWatchers() for _, uw := range uws { if uw.role != role { @@ -470,6 +471,10 @@ func (uw *urlWatcher) subscribeAPIWatcher(aw *apiWatcher) { func (uw *urlWatcher) registerPendingAPIWatchers() { uw.mu.Lock() + if len(uw.awsPending) == 0 { + uw.mu.Unlock() + return + } awsPending := make([]*apiWatcher, 0, len(uw.awsPending)) for aw := range uw.awsPending { awsPending = append(awsPending, aw)