Skip to content

Commit

Permalink
use smallrye config
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Aug 17, 2022
1 parent f7a71c9 commit db659d6
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 56 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<io.fabric8.client.version>5.12.2</io.fabric8.client.version>
<io.vertx.web.version>4.2.5</io.vertx.web.version>
<com.fasterxml.jackson.version>2.13.3</com.fasterxml.jackson.version>
<javax.annotation.version>1.3.2</javax.annotation.version><!-- used by smallrye -->
<io.smallrye.config.version>2.11.1</io.smallrye.config.version>
<org.slf4j.version>1.7.30</org.slf4j.version>

<com.github.spotbugs.version>4.5.3</com.github.spotbugs.version>
Expand Down Expand Up @@ -71,6 +73,16 @@
<artifactId>jackson-databind</artifactId>
<version>${com.fasterxml.jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax.annotation.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config</artifactId>
<version>${io.smallrye.config.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
Expand Down
62 changes: 37 additions & 25 deletions src/main/java/io/cryostat/agent/CryostatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
package io.cryostat.agent;

import java.io.Closeable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set;
import java.util.UUID;

import io.cryostat.agent.model.DiscoveryNode;
import io.cryostat.agent.model.PluginInfo;
import io.cryostat.agent.model.RegistrationInfo;

import io.smallrye.config.SmallRyeConfig;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpHeaders;
Expand All @@ -56,38 +59,53 @@

class CryostatClient implements Closeable {

private static final String CRYOSTAT_AGENT_CALLBACK = "cryostat.agent.callback";
private static final String CRYOSTAT_AGENT_REALM = "cryostat.agent.realm";
private static final String CRYOSTAT_AGENT_TRUST_ALL = "cryostat.agent.trust-all";
private static final String CRYOSTAT_AGENT_BASEURI = "cryostat.agent.baseuri";
private static final String CRYOSTAT_AGENT_AUTHORIZATION = "cryostat.agent.authorization";

private final Logger log = LoggerFactory.getLogger(getClass());

private WebClient http;
private final UUID instanceId;
private final SmallRyeConfig config;

CryostatClient(Vertx vertx, UUID instanceId) {
CryostatClient(Vertx vertx, UUID instanceId, SmallRyeConfig config) {
this.config = config;
WebClientOptions opts = new WebClientOptions();

// TODO make configurable
opts.setDefaultHost("localhost");
opts.setDefaultPort(8181);
opts.setSsl(true);
opts.setTrustAll(true);
opts.setVerifyHost(false);
String rawUri = config.getValue(CRYOSTAT_AGENT_BASEURI, String.class);
URI baseUri;
try {
baseUri = new URI(rawUri);
} catch (URISyntaxException e) {
log.error("Invalid {}: {}", CRYOSTAT_AGENT_BASEURI, rawUri);
baseUri = URI.create("http://localhost:8181/");
}
log.info("Using Cryostat baseuri {}", baseUri);

opts.setDefaultHost(baseUri.getHost());
opts.setDefaultPort(baseUri.getPort());
opts.setSsl("https".equals(baseUri.getScheme()));
if (config.getValue(CRYOSTAT_AGENT_TRUST_ALL, Boolean.class)) {
opts.setTrustAll(true);
opts.setVerifyHost(false);
}

this.http = WebClient.create(vertx, opts);
this.instanceId = instanceId;
}

Future<PluginInfo> register() {
String auth = System.getenv("CRYOSTAT_AGENT_AUTHORIZATION");
if (StringUtils.isBlank(auth)) {
auth = "None";
}
String realm = System.getenv("CRYOSTAT_AGENT_REALM");
if (StringUtils.isBlank(realm)) {
realm = "cryostat-agent-" + instanceId;
}
String callback = System.getenv("CRYOSTAT_AGENT_CALLBACK");
String auth = config.getValue(CRYOSTAT_AGENT_AUTHORIZATION, String.class);
String realm =
config.getOptionalValue(CRYOSTAT_AGENT_REALM, String.class)
.orElse("cryostat-agent-" + instanceId);
String callback = config.getValue(CRYOSTAT_AGENT_CALLBACK, String.class);
// do this at startup time
if (StringUtils.isBlank(callback)) {
throw new UndefinedVariableException("CRYOSTAT_AGENT_CALLBACK");
throw new NoConfigurationException(CRYOSTAT_AGENT_CALLBACK);
}
RegistrationInfo registrationInfo = new RegistrationInfo(realm, callback);
return http.post("/api/v2.2/discovery")
Expand All @@ -102,10 +120,7 @@ Future<PluginInfo> register() {
}

Future<Void> deregister(String id) {
String auth = System.getenv("CRYOSTAT_AGENT_AUTHORIZATION");
if (StringUtils.isBlank(auth)) {
auth = "None";
}
String auth = config.getValue(CRYOSTAT_AGENT_AUTHORIZATION, String.class);
return http.delete("/api/v2.2/discovery/" + id)
.putHeader(HttpHeaders.AUTHORIZATION.toString(), auth)
.expect(ResponsePredicate.SC_SUCCESS)
Expand All @@ -116,10 +131,7 @@ Future<Void> deregister(String id) {
}

Future<Void> update(String id, Set<DiscoveryNode> subtree) {
String auth = System.getenv("CRYOSTAT_AGENT_AUTHORIZATION");
if (StringUtils.isBlank(auth)) {
auth = "None";
}
String auth = config.getValue(CRYOSTAT_AGENT_AUTHORIZATION, String.class);
return http.post("/api/v2.2/discovery/" + id)
.putHeader(HttpHeaders.AUTHORIZATION.toString(), auth)
.expect(ResponsePredicate.SC_SUCCESS)
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/io/cryostat/agent/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@

import dagger.Module;
import dagger.Provides;
import io.smallrye.config.SmallRyeConfig;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import org.eclipse.microprofile.config.ConfigProvider;

@Module
public abstract class MainModule {
Expand All @@ -57,25 +59,33 @@ public static Vertx provideVertx() {

@Provides
@Singleton
public static WebServer provideHttpServer() {
return new WebServer();
public static WebServer provideHttpServer(SmallRyeConfig config) {
return new WebServer(config);
}

@Provides
@Singleton
public static CryostatClient provideCryostatClient(Vertx vertx, UUID instanceId) {
return new CryostatClient(vertx, instanceId);
public static CryostatClient provideCryostatClient(
Vertx vertx, UUID instanceId, SmallRyeConfig config) {
return new CryostatClient(vertx, instanceId, config);
}

@Provides
@Singleton
public static Registration provideRegistration(CryostatClient cryostat, UUID instanceId) {
return new Registration(cryostat, instanceId);
public static Registration provideRegistration(
CryostatClient cryostat, UUID instanceId, SmallRyeConfig config) {
return new Registration(cryostat, instanceId, config);
}

@Provides
@Singleton
public static UUID provideInstanceID() {
return UUID.randomUUID();
}

@Provides
@Singleton
public static SmallRyeConfig provideConfig() {
return ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
*/
package io.cryostat.agent;

public class UndefinedVariableException extends RuntimeException {
public class NoConfigurationException extends RuntimeException {

public UndefinedVariableException(String name) {
super("Environment variable \"" + name + "\" must be defined and non-blank");
public NoConfigurationException(String name) {
super("Configuration key \"" + name + "\" must haved defined, non-blank value");
}
}
15 changes: 12 additions & 3 deletions src/main/java/io/cryostat/agent/Registration.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import io.cryostat.agent.model.DiscoveryNode;
import io.cryostat.agent.model.PluginInfo;

import io.smallrye.config.SmallRyeConfig;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.eventbus.MessageConsumer;
Expand All @@ -56,19 +57,24 @@

class Registration extends AbstractVerticle {

private static final String CRYOSTAT_AGENT_REGISTRATION_RETRY_MS =
"cryostat.agent.registration.retry-ms";
static final String EVENT_BUS_ADDRESS = Registration.class.getName() + ".UPDATE";
private static final String NODE_TYPE = "JVM";

private final Logger log = LoggerFactory.getLogger(getClass());

private final CryostatClient cryostat;
private final UUID instanceId;
private final SmallRyeConfig config;

private PluginInfo pluginInfo;
private MessageConsumer<Object> consumer;

Registration(CryostatClient cryostat, UUID instanceId) {
Registration(CryostatClient cryostat, UUID instanceId, SmallRyeConfig config) {
this.cryostat = cryostat;
this.instanceId = instanceId;
this.config = config;
}

@Override
Expand Down Expand Up @@ -97,8 +103,11 @@ private void tryRegister(Long id) {
.onFailure(
t -> {
log.error("Registration failure", t);
Duration registrationRetryPeriod = Duration.ofSeconds(5);
vertx.setTimer(registrationRetryPeriod.toMillis(), this::tryRegister);
int registrationRetryMs =
config.getValue(
CRYOSTAT_AGENT_REGISTRATION_RETRY_MS, int.class);
log.info("Registration retry period: {}(ms)", registrationRetryMs);
vertx.setTimer(registrationRetryMs, this::tryRegister);
});
}

Expand Down
31 changes: 12 additions & 19 deletions src/main/java/io/cryostat/agent/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
package io.cryostat.agent;

import io.smallrye.config.SmallRyeConfig;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpMethod;
Expand All @@ -48,30 +49,24 @@

class WebServer extends AbstractVerticle {

private static final String DEFAULT_HTTP_HOST = "0.0.0.0";
private static final int DEFAULT_HTTP_PORT = 9977;
private static final String CRYOSTAT_AGENT_WEBSERVER_HOST = "cryostat.agent.webserver.host";
private static final String CRYOSTAT_AGENT_WEBSERVER_PORT = "cryostat.agent.webserver.port";

private final Logger log = LoggerFactory.getLogger(getClass());

private final SmallRyeConfig config;
private HttpServer http;

WebServer(SmallRyeConfig config) {
this.config = config;
}

@Override
public void start(Promise<Void> promise) {
String host = System.getenv("CRYOSTAT_AGENT_HTTP_HOST");
if (StringUtils.isBlank(host)) {
host = DEFAULT_HTTP_HOST;
}
final String fHost = host;
String port = System.getenv("CRYOSTAT_AGENT_HTTP_PORT");
if (StringUtils.isBlank(port)) {
port = String.valueOf(DEFAULT_HTTP_PORT);
}
String host = config.getValue(CRYOSTAT_AGENT_WEBSERVER_HOST, String.class);
int port = config.getValue(CRYOSTAT_AGENT_WEBSERVER_PORT, int.class);
this.http =
getVertx()
.createHttpServer(
new HttpServerOptions()
.setHost(host)
.setPort(Integer.valueOf(port)));
getVertx().createHttpServer(new HttpServerOptions().setHost(host).setPort(port));

Router router = Router.router(getVertx());
router.route()
Expand All @@ -94,9 +89,7 @@ public void start(Promise<Void> promise) {
}
promise.complete();
log.info(
"HTTP Server started on {}:{}",
fHost,
ar.result().actualPort());
"HTTP Server started on {}:{}", host, ar.result().actualPort());
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/cryostat/agent/model/DiscoveryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public Target(
annotations.setPlatform(Map.of("INSTANCE_ID", instanceId));
annotations.setCryostat(
Map.of(
"REALM",
"cryostat-agent",
"PID",
pid,
"HOST",
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/META-INF/microprofile-config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cryostat.agent.baseuri=https://localhost:8181/
cryostat.agent.trust-all=true
cryostat.agent.authorization=None
cryostat.agent.callback=
cryostat.agent.realm=
cryostat.agent.registration.retry-ms=5000
cryostat.agent.webserver.host=0.0.0.0
cryostat.agent.webserver.port=9977

0 comments on commit db659d6

Please sign in to comment.