Skip to content

Commit

Permalink
fix(k8s): Endpoints discovery port names/numbers are configurable (#1862
Browse files Browse the repository at this point in the history
) (#1874)

(cherry picked from commit 838affd)

Co-authored-by: Andrew Azores <aazores@redhat.com>
  • Loading branch information
mergify[bot] and andrewazores authored Feb 12, 2024
1 parent 756d5ba commit bf58882
Show file tree
Hide file tree
Showing 7 changed files with 1,341 additions and 777 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,14 @@ service endpoints and expose all discovered services as potential targets. This
is runtime dynamic, allowing `cryostat` to discover new services which come
online after `cryostat`, or to detect when known services disappear later.
This requires the `cryostat` pod to have authorization to list services
within its own namespace.
within its own namespace. By default this will look for `Endpoints` objects
with ports named `jfr-jmx` or numbered `9091`. This behaviour can be overridden
using the environment variables `CRYOSTAT_DISCOVERY_K8S_PORT_NAMES` and
`CRYOSTAT_DISCOVERY_K8S_PORT_NUMBERS` respectively. Both of these accept
comma-separated lists as values. Any observed `Endpoints` object with a name
in the given list or a number in the given list will be taken as a connectable
target application. To set the names list to the empty list use `-`. To set the
numbers list to the empty list use `0`.

The second discovery mechanism is JDP (Java Discovery Protocol). This relies on
target JVMs being configured with the JVM flags to enable JDP and requires the
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/cryostat/configuration/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ private Variables() {}
public static final String DISABLE_BUILTIN_DISCOVERY = "CRYOSTAT_DISABLE_BUILTIN_DISCOVERY";
public static final String DISCOVERY_PING_PERIOD_MS = "CRYOSTAT_DISCOVERY_PING_PERIOD";
public static final String K8S_NAMESPACES = "CRYOSTAT_K8S_NAMESPACES";
public static final String K8S_PORT_NAMES = "CRYOSTAT_DISCOVERY_K8S_PORT_NAMES";
public static final String K8S_PORT_NUMBERS = "CRYOSTAT_DISCOVERY_K8S_PORT_NUMBERS";
public static final String VERTX_POOL_SIZE = "CRYOSTAT_VERTX_POOL_SIZE";

// webserver configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import javax.management.remote.JMXServiceURL;

import io.cryostat.core.log.Logger;
import io.cryostat.core.net.JFRConnectionToolkit;
import io.cryostat.core.net.discovery.JvmDiscoveryClient.EventKind;
import io.cryostat.core.sys.Environment;
import io.cryostat.platform.AbstractPlatformClient;
Expand All @@ -44,7 +43,6 @@
import io.cryostat.platform.discovery.NodeType;
import io.cryostat.platform.discovery.TargetNode;

import dagger.Lazy;
import io.fabric8.kubernetes.api.model.EndpointAddress;
import io.fabric8.kubernetes.api.model.EndpointPort;
import io.fabric8.kubernetes.api.model.EndpointSubset;
Expand All @@ -68,6 +66,8 @@ public class KubeApiPlatformClient extends AbstractPlatformClient {

private final KubernetesClient k8sClient;
private final Set<String> namespaces;
private final Set<String> portNames;
private final Set<Integer> portNumbers;
private final LazyInitializer<HashMap<String, SharedIndexInformer<Endpoints>>> nsInformers =
new LazyInitializer<HashMap<String, SharedIndexInformer<Endpoints>>>() {
@Override
Expand Down Expand Up @@ -98,7 +98,6 @@ protected HashMap<String, SharedIndexInformer<Endpoints>> initialize()
};
private Integer memoHash;
private EnvironmentNode memoTree;
private final Lazy<JFRConnectionToolkit> connectionToolkit;
private final Logger logger;
private final Map<Triple<String, String, String>, Pair<HasMetadata, EnvironmentNode>>
discoveryNodeCache = new ConcurrentHashMap<>();
Expand All @@ -108,13 +107,15 @@ protected HashMap<String, SharedIndexInformer<Endpoints>> initialize()
KubeApiPlatformClient(
Environment environment,
Collection<String> namespaces,
Collection<String> portNames,
Collection<Integer> portNumbers,
KubernetesClient k8sClient,
Lazy<JFRConnectionToolkit> connectionToolkit,
Logger logger) {
super(environment);
this.namespaces = new HashSet<>(namespaces);
this.portNames = new HashSet<>(portNames);
this.portNumbers = new HashSet<>(portNumbers);
this.k8sClient = k8sClient;
this.connectionToolkit = connectionToolkit;
this.logger = logger;
}

Expand Down Expand Up @@ -289,7 +290,7 @@ private Pair<HasMetadata, EnvironmentNode> queryForNode(
}

private boolean isCompatiblePort(EndpointPort port) {
return "jfr-jmx".equals(port.getName()) || 9091 == port.getPort();
return portNames.contains(port.getName()) || portNumbers.contains(port.getPort());
}

private List<ServiceRef> getAllServiceRefs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import io.cryostat.configuration.Variables;
import io.cryostat.core.log.Logger;
import io.cryostat.core.net.JFRConnectionToolkit;
import io.cryostat.core.sys.Environment;
import io.cryostat.core.sys.FileSystem;
import io.cryostat.net.AuthManager;
Expand All @@ -39,21 +38,18 @@

class KubeApiPlatformStrategy implements PlatformDetectionStrategy<KubeApiPlatformClient> {

public static final String NO_PORT_NAME = "-";
public static final Integer NO_PORT_NUMBER = 0;

protected final Lazy<? extends AuthManager> authMgr;
protected final Environment env;
protected final FileSystem fs;
protected final Lazy<JFRConnectionToolkit> connectionToolkit;
protected final Logger logger;

KubeApiPlatformStrategy(
Lazy<? extends AuthManager> authMgr,
Lazy<JFRConnectionToolkit> connectionToolkit,
Environment env,
FileSystem fs,
Logger logger) {
Lazy<? extends AuthManager> authMgr, Environment env, FileSystem fs, Logger logger) {
this.logger = logger;
this.authMgr = authMgr;
this.connectionToolkit = connectionToolkit;
this.env = env;
this.fs = fs;
}
Expand All @@ -72,8 +68,19 @@ public boolean isAvailable() {
@Override
public KubeApiPlatformClient getPlatformClient() {
logger.info("Selected {} Strategy", getClass().getSimpleName());
List<String> portNames =
Arrays.asList(env.getEnv(Variables.K8S_PORT_NAMES, "jfr-jmx").split(",")).stream()
.map(String::strip)
.filter(n -> !NO_PORT_NAME.equals(n))
.toList();
List<Integer> portNumbers =
Arrays.asList(env.getEnv(Variables.K8S_PORT_NUMBERS, "9091").split(",")).stream()
.map(String::strip)
.map(Integer::parseInt)
.filter(n -> !NO_PORT_NUMBER.equals(n))
.toList();
return new KubeApiPlatformClient(
env, getNamespaces(), createClient(), connectionToolkit, logger);
env, getNamespaces(), portNames, portNumbers, createClient(), logger);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.cryostat.platform.internal;

import io.cryostat.core.log.Logger;
import io.cryostat.core.net.JFRConnectionToolkit;
import io.cryostat.core.sys.Environment;
import io.cryostat.core.sys.FileSystem;
import io.cryostat.net.AuthManager;
Expand All @@ -28,12 +27,8 @@
class OpenShiftPlatformStrategy extends KubeApiPlatformStrategy {

OpenShiftPlatformStrategy(
Lazy<? extends AuthManager> authMgr,
Lazy<JFRConnectionToolkit> connectionToolkit,
Environment env,
FileSystem fs,
Logger logger) {
super(authMgr, connectionToolkit, env, fs, logger);
Lazy<? extends AuthManager> authMgr, Environment env, FileSystem fs, Logger logger) {
super(authMgr, env, fs, logger);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,15 @@ static CustomTargetPlatformStrategy provideCustomTargetPlatformStrategy(
@Provides
@Singleton
static OpenShiftPlatformStrategy provideOpenShiftPlatformStrategy(
Logger logger,
Lazy<OpenShiftAuthManager> authManager,
Lazy<JFRConnectionToolkit> connectionToolkit,
Environment env,
FileSystem fs) {
return new OpenShiftPlatformStrategy(authManager, connectionToolkit, env, fs, logger);
Lazy<OpenShiftAuthManager> authManager, Environment env, FileSystem fs, Logger logger) {
return new OpenShiftPlatformStrategy(authManager, env, fs, logger);
}

@Provides
@Singleton
static KubeApiPlatformStrategy provideKubeApiPlatformStrategy(
Lazy<NoopAuthManager> noopAuthManager,
Lazy<JFRConnectionToolkit> connectionToolkit,
Environment env,
FileSystem fs,
Logger logger) {
return new KubeApiPlatformStrategy(noopAuthManager, connectionToolkit, env, fs, logger);
Lazy<NoopAuthManager> noopAuthManager, Environment env, FileSystem fs, Logger logger) {
return new KubeApiPlatformStrategy(noopAuthManager, env, fs, logger);
}

@Provides
Expand Down
Loading

0 comments on commit bf58882

Please sign in to comment.