Activator.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.redhat.training;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.net.URI;
  7. import java.net.URISyntaxException;
  8. import java.security.KeyManagementException;
  9. import java.security.KeyStoreException;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.security.cert.CertificateException;
  12. import java.util.Optional;
  13. import javax.net.ssl.SSLContext;
  14. import org.apache.http.ssl.SSLContextBuilder;
  15. import org.apache.http.ssl.SSLContexts;
  16. import org.eclipse.microprofile.config.inject.ConfigProperty;
  17. import org.eclipse.microprofile.rest.client.RestClientBuilder;
  18. import org.jboss.logging.Logger;
  19. import jakarta.annotation.PostConstruct;
  20. import jakarta.enterprise.context.ApplicationScoped;
  21. import jakarta.ws.rs.POST;
  22. import jakarta.ws.rs.Path;
  23. @ApplicationScoped
  24. @Path("/jobs")
  25. public class Activator {
  26. final Logger LOG = Logger.getLogger(Activator.class.getName());
  27. @ConfigProperty(name = "api.token")
  28. Optional<String> token;
  29. @ConfigProperty(name = "api.endpoint")
  30. Optional<String> apiserver;
  31. @ConfigProperty(name = "api.tlsca.file")
  32. Optional<String> tlsca;
  33. ApiClient k8s;
  34. @PostConstruct
  35. public void checkEnv() {
  36. // Check for API token.
  37. if (token.isPresent() && !token.get().isEmpty()) {
  38. LOG.debug("Got API token from environment.");
  39. } else {
  40. LOG.warn("API token not found in environment. Trying service account.");
  41. File tf = new File("/var/run/secrets/kubernetes.io/serviceaccount/token");
  42. if (tf.exists()) {
  43. try {
  44. BufferedReader br = new BufferedReader(new FileReader(tf));
  45. this.token = Optional.of(br.readLine());
  46. br.close();
  47. } catch (IOException ioe) {
  48. throw new RuntimeException("Can not load service account token: " + ioe.getMessage(), ioe);
  49. }
  50. } else {
  51. throw new RuntimeException("API token unobtainable. Can not talk to API.");
  52. }
  53. }
  54. // Check for API server.
  55. if (apiserver.isPresent() && !apiserver.get().isEmpty()) {
  56. LOG.debug("Got API server endpoint from environment.");
  57. } else {
  58. LOG.warn("API server endpoint not set, defaulting to internal API server.");
  59. apiserver = Optional.of("https://kubernetes.default/");
  60. }
  61. // Check for TLS CA cert.
  62. SSLContext sc = null;
  63. if (apiserver.get().startsWith("https://")) {
  64. File tlscaFile;
  65. if (tlsca.isPresent() && !tlsca.get().isEmpty()) {
  66. LOG.debug("Got TLS CA cert file from environment, checking.");
  67. tlscaFile = new File(tlsca.get());
  68. } else {
  69. LOG.warn("TLS CA cert not found in environment. Trying service account.");
  70. tlscaFile = new File("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt");
  71. }
  72. if (!tlscaFile.exists()) {
  73. throw new RuntimeException("TLS CA cert file set, but does not exist.");
  74. }
  75. LOG.info("Attempting to build SSLContext with " + tlscaFile.getAbsolutePath());
  76. try {
  77. SSLContextBuilder scb = SSLContexts.custom().loadTrustMaterial(tlscaFile);
  78. sc = scb.build();
  79. } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException | KeyManagementException e) {
  80. throw new RuntimeException("Could not load TLS CA: " + e.getMessage(), e);
  81. }
  82. }
  83. try {
  84. if (sc == null) {
  85. this.k8s = RestClientBuilder.newBuilder()
  86. .baseUri(new URI(this.apiserver.get()))
  87. .build(ApiClient.class);
  88. } else {
  89. this.k8s = RestClientBuilder.newBuilder()
  90. .baseUri(new URI(this.apiserver.get()))
  91. .sslContext(sc)
  92. .build(ApiClient.class);
  93. }
  94. } catch (URISyntaxException use) {
  95. throw new RuntimeException("Could not construct BASE URI for REST client: " + use.getMessage(), use);
  96. }
  97. }
  98. @POST
  99. public String createJob(JobDescription job) {
  100. return k8s.createJob("Bearer " + token.get(),
  101. job.getNamespace(),
  102. job.getName(),
  103. new Job(job.getNamespace(),
  104. job.getName(),
  105. job.getCommand()).getApiResource());
  106. }
  107. }