diff --git a/src/main/java/io/cryostat/discovery/KubeApiDiscovery.java b/src/main/java/io/cryostat/discovery/KubeApiDiscovery.java index 9c255cac1..848cb3043 100644 --- a/src/main/java/io/cryostat/discovery/KubeApiDiscovery.java +++ b/src/main/java/io/cryostat/discovery/KubeApiDiscovery.java @@ -71,7 +71,6 @@ @ApplicationScoped public class KubeApiDiscovery implements ResourceEventHandler { - private static final String ALL_NAMESPACES = "*"; private static final String NAMESPACE_QUERY_ADDR = "NS_QUERY"; private static final String ENDPOINTS_DISCOVERY_ADDR = "ENDPOINTS_DISC"; @@ -111,9 +110,9 @@ public class KubeApiDiscovery implements ResourceEventHandler { protected HashMap> initialize() throws ConcurrentException { var result = new HashMap>(); - if (watchAllNamespaces()) { + if (kubeConfig.watchAllNamespaces()) { result.put( - ALL_NAMESPACES, + KubeConfig.ALL_NAMESPACES, client.endpoints() .inAnyNamespace() .inform( @@ -146,10 +145,6 @@ protected HashMap> initialize() } }; - private boolean watchAllNamespaces() { - return kubeConfig.getWatchNamespaces().stream().anyMatch(ns -> ALL_NAMESPACES.equals(ns)); - } - void onStart(@Observes StartupEvent evt) { if (!enabled()) { return; @@ -168,19 +163,7 @@ void onStart(@Observes StartupEvent evt) { () -> { try { logger.debug("Resyncing"); - List namespaces; - if (watchAllNamespaces()) { - namespaces = - client.namespaces().list().getItems().stream() - .map(ns -> ns.getMetadata().getName()) - .toList(); - } else { - namespaces = - kubeConfig.getWatchNamespaces().stream() - .filter(ns -> !ALL_NAMESPACES.equals(ns)) - .toList(); - } - notify(NamespaceQueryEvent.from(namespaces)); + notify(NamespaceQueryEvent.from(getWatchNamespaces())); } catch (Exception e) { logger.warn(e); } @@ -190,6 +173,17 @@ void onStart(@Observes StartupEvent evt) { TimeUnit.MILLISECONDS); } + private List getWatchNamespaces() { + if (kubeConfig.watchAllNamespaces()) { + return client.namespaces().list().getItems().stream() + .map(ns -> ns.getMetadata().getName()) + .toList(); + } + return kubeConfig.getWatchNamespaces().stream() + .filter(ns -> !KubeConfig.ALL_NAMESPACES.equals(ns)) + .toList(); + } + void onStop(@Observes ShutdownEvent evt) { if (!(enabled() && available())) { return; @@ -337,9 +331,13 @@ public void handleQueryEvent(NamespaceQueryEvent evt) { } Stream endpoints; - if (watchAllNamespaces()) { + if (kubeConfig.watchAllNamespaces()) { endpoints = - safeGetInformers().get(ALL_NAMESPACES).getStore().list().stream() + safeGetInformers() + .get(KubeConfig.ALL_NAMESPACES) + .getStore() + .list() + .stream() .filter( ep -> Objects.equals( @@ -586,6 +584,7 @@ private Pair queryForNode( @ApplicationScoped static final class KubeConfig { + static final String ALL_NAMESPACES = "*"; private static final String OWN_NAMESPACE = "."; @Inject Logger logger; @@ -600,6 +599,11 @@ static final class KubeConfig { @ConfigProperty(name = "cryostat.discovery.kubernetes.namespace-path") String namespacePath; + boolean watchAllNamespaces() { + return watchNamespaces.orElse(List.of()).stream() + .anyMatch(ns -> ALL_NAMESPACES.equals(ns)); + } + Collection getWatchNamespaces() { return watchNamespaces.orElse(List.of()).stream() .map(