Procházet zdrojové kódy

construct rest client dynamically, add api.endpoint config, fix lack of Bearer in Authorization header

Grega Bremec před 7 měsíci
rodič
revize
edfecfaa04
1 změnil soubory, kde provedl 32 přidání a 20 odebrání
  1. 32 20
      src/main/java/com/redhat/training/Activator.java

+ 32 - 20
src/main/java/com/redhat/training/Activator.java

@@ -4,15 +4,16 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Optional;
 
 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 jakarta.annotation.PostConstruct;
 import jakarta.enterprise.context.ApplicationScoped;
-import jakarta.inject.Inject;
 import jakarta.ws.rs.POST;
 import jakarta.ws.rs.Path;
 
@@ -24,36 +25,47 @@ public class Activator {
     @ConfigProperty(name = "api.token")
     Optional<String> token;
 
-    @Inject
-    @RestClient
+    @ConfigProperty(name = "api.endpoint")
+    Optional<String> api;
+
     ApiClient k8s;
 
     @PostConstruct
     public void checkEnv() {
         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
     public String createJob(JobDescription job) {
-        return k8s.createJob(token.get(),
+        return k8s.createJob("Bearer " + token.get(),
                             job.getNamespace(),
                             job.getName(),
                             new Job(job.getNamespace(),