Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Dec 4, 2024
1 parent 41a4d4c commit 2169300
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/main/java/io/cryostat/discovery/KubeApiDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
@ApplicationScoped
public class KubeApiDiscovery implements ResourceEventHandler<Endpoints> {

private static final String ALL_NAMESPACES = "*";
private static final String NAMESPACE_QUERY_ADDR = "NS_QUERY";
private static final String ENDPOINTS_DISCOVERY_ADDR = "ENDPOINTS_DISC";

Expand Down Expand Up @@ -111,9 +110,9 @@ public class KubeApiDiscovery implements ResourceEventHandler<Endpoints> {
protected HashMap<String, SharedIndexInformer<Endpoints>> initialize()
throws ConcurrentException {
var result = new HashMap<String, SharedIndexInformer<Endpoints>>();
if (watchAllNamespaces()) {
if (kubeConfig.watchAllNamespaces()) {
result.put(
ALL_NAMESPACES,
KubeConfig.ALL_NAMESPACES,
client.endpoints()
.inAnyNamespace()
.inform(
Expand Down Expand Up @@ -146,10 +145,6 @@ protected HashMap<String, SharedIndexInformer<Endpoints>> initialize()
}
};

private boolean watchAllNamespaces() {
return kubeConfig.getWatchNamespaces().stream().anyMatch(ns -> ALL_NAMESPACES.equals(ns));
}

void onStart(@Observes StartupEvent evt) {
if (!enabled()) {
return;
Expand All @@ -168,19 +163,7 @@ void onStart(@Observes StartupEvent evt) {
() -> {
try {
logger.debug("Resyncing");
List<String> 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);
}
Expand All @@ -190,6 +173,17 @@ void onStart(@Observes StartupEvent evt) {
TimeUnit.MILLISECONDS);
}

private List<String> 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;
Expand Down Expand Up @@ -337,9 +331,13 @@ public void handleQueryEvent(NamespaceQueryEvent evt) {
}

Stream<Endpoints> 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(
Expand Down Expand Up @@ -586,6 +584,7 @@ private Pair<HasMetadata, DiscoveryNode> queryForNode(

@ApplicationScoped
static final class KubeConfig {
static final String ALL_NAMESPACES = "*";
private static final String OWN_NAMESPACE = ".";

@Inject Logger logger;
Expand All @@ -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<String> getWatchNamespaces() {
return watchNamespaces.orElse(List.of()).stream()
.map(
Expand Down

0 comments on commit 2169300

Please sign in to comment.