|
@@ -4,15 +4,16 @@ import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.net.URI;
|
|
|
|
+import java.net.URISyntaxException;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
|
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
|
-import org.eclipse.microprofile.rest.client.inject.RestClient;
|
|
|
|
|
|
+import org.eclipse.microprofile.rest.client.RestClientBuilder;
|
|
import org.jboss.logging.Logger;
|
|
import org.jboss.logging.Logger;
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
import jakarta.annotation.PostConstruct;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
-import jakarta.inject.Inject;
|
|
|
|
import jakarta.ws.rs.POST;
|
|
import jakarta.ws.rs.POST;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.Path;
|
|
|
|
|
|
@@ -24,36 +25,47 @@ public class Activator {
|
|
@ConfigProperty(name = "api.token")
|
|
@ConfigProperty(name = "api.token")
|
|
Optional<String> token;
|
|
Optional<String> token;
|
|
|
|
|
|
- @Inject
|
|
|
|
- @RestClient
|
|
|
|
|
|
+ @ConfigProperty(name = "api.endpoint")
|
|
|
|
+ Optional<String> api;
|
|
|
|
+
|
|
ApiClient k8s;
|
|
ApiClient k8s;
|
|
|
|
|
|
@PostConstruct
|
|
@PostConstruct
|
|
public void checkEnv() {
|
|
public void checkEnv() {
|
|
if (token.isPresent() && !token.get().isEmpty()) {
|
|
if (token.isPresent() && !token.get().isEmpty()) {
|
|
- LOG.info("Got token from env.");
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- LOG.info("Token not found in env. Trying serviceaccount token.");
|
|
|
|
- File tf = new File("/var/run/secrets/kubernetes.io/serviceaccount/token");
|
|
|
|
- if (tf.exists()) {
|
|
|
|
- try {
|
|
|
|
- BufferedReader br = new BufferedReader(new FileReader(tf));
|
|
|
|
- this.token = Optional.of(br.readLine());
|
|
|
|
- br.close();
|
|
|
|
- return;
|
|
|
|
- } catch (IOException ioe) {
|
|
|
|
- LOG.error("Can not read service account token. This will probably all go to hell.");
|
|
|
|
|
|
+ LOG.debug("Got API token from environment.");
|
|
|
|
+ } else {
|
|
|
|
+ LOG.info("API token not found in environment. Trying service account.");
|
|
|
|
+ File tf = new File("/var/run/secrets/kubernetes.io/serviceaccount/token");
|
|
|
|
+ if (tf.exists()) {
|
|
|
|
+ try {
|
|
|
|
+ BufferedReader br = new BufferedReader(new FileReader(tf));
|
|
|
|
+ this.token = Optional.of(br.readLine());
|
|
|
|
+ br.close();
|
|
|
|
+ } catch (IOException ioe) {
|
|
|
|
+ throw new RuntimeException("Can not load service account token: " + ioe.getMessage());
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ throw new RuntimeException("API token unobtainable. Can not talk to API.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ if (api.isPresent() && !api.get().isEmpty()) {
|
|
|
|
+ LOG.debug("Got API server endpoint from environment.");
|
|
|
|
+ } else {
|
|
|
|
+ LOG.warn("API server endpoint not set, defaulting to internal API server.");
|
|
|
|
+ api = Optional.of("https://kubernetes.default/");
|
|
|
|
+ }
|
|
|
|
|
|
- throw new RuntimeException("API Token not set. Can not talk to API.");
|
|
|
|
|
|
+ try {
|
|
|
|
+ this.k8s = RestClientBuilder.newBuilder().baseUri(new URI(this.api.get())).build(ApiClient.class);
|
|
|
|
+ } catch (URISyntaxException use) {
|
|
|
|
+ throw new RuntimeException("Could not construct BASE URI for REST client: " + use.getMessage());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
@POST
|
|
@POST
|
|
public String createJob(JobDescription job) {
|
|
public String createJob(JobDescription job) {
|
|
- return k8s.createJob(token.get(),
|
|
|
|
|
|
+ return k8s.createJob("Bearer " + token.get(),
|
|
job.getNamespace(),
|
|
job.getNamespace(),
|
|
job.getName(),
|
|
job.getName(),
|
|
new Job(job.getNamespace(),
|
|
new Job(job.getNamespace(),
|