diff --git a/cluster-controller/src/main/java/io/strimzi/controller/cluster/Main.java b/cluster-controller/src/main/java/io/strimzi/controller/cluster/Main.java index efff247f309..6d7050530db 100644 --- a/cluster-controller/src/main/java/io/strimzi/controller/cluster/Main.java +++ b/cluster-controller/src/main/java/io/strimzi/controller/cluster/Main.java @@ -7,6 +7,7 @@ import io.fabric8.kubernetes.client.DefaultKubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.openshift.client.OpenShiftClient; +import io.netty.handler.codec.http.HttpResponseStatus; import io.strimzi.controller.cluster.operations.cluster.KafkaClusterOperations; import io.strimzi.controller.cluster.operations.cluster.KafkaConnectClusterOperations; import io.strimzi.controller.cluster.operations.cluster.KafkaConnectS2IClusterOperations; @@ -23,9 +24,12 @@ import io.vertx.core.CompositeFuture; import io.vertx.core.Future; import io.vertx.core.Vertx; +import io.vertx.core.http.HttpClient; +import io.vertx.core.http.HttpClientOptions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -34,15 +38,25 @@ public class Main { private static final Logger log = LoggerFactory.getLogger(Main.class.getName()); public static void main(String[] args) { - run(Vertx.vertx(), new DefaultKubernetesClient(), System.getenv()).setHandler(ar -> { - if (ar.failed()) { - log.error("Unable to start controller for 1 or more namespace", ar.cause()); + Vertx vertx = Vertx.vertx(); + KubernetesClient client = new DefaultKubernetesClient(); + + isOnOpenShift(vertx, client).setHandler(os -> { + if (os.succeeded()) { + run(vertx, client, os.result().booleanValue(), System.getenv()).setHandler(ar -> { + if (ar.failed()) { + log.error("Unable to start controller for 1 or more namespace", ar.cause()); + System.exit(1); + } + }); + } else { + log.error("Failed to distinguish between Kubernetes and OpenShift", os.cause()); System.exit(1); } }); } - static CompositeFuture run(Vertx vertx, KubernetesClient client, Map env) { + static CompositeFuture run(Vertx vertx, KubernetesClient client, boolean isOpenShift, Map env) { ClusterControllerConfig config = ClusterControllerConfig.fromMap(env); ServiceOperations serviceOperations = new ServiceOperations(vertx, client); @@ -53,7 +67,6 @@ static CompositeFuture run(Vertx vertx, KubernetesClient client, Map isOnOpenShift(Vertx vertx, KubernetesClient client) { + URL kubernetesApi = client.getMasterUrl(); + Future fut = Future.future(); + + HttpClientOptions httpClientOptions = new HttpClientOptions(); + httpClientOptions.setDefaultHost(kubernetesApi.getHost()); + + if (kubernetesApi.getPort() == -1) { + httpClientOptions.setDefaultPort(kubernetesApi.getDefaultPort()); + } else { + httpClientOptions.setDefaultPort(kubernetesApi.getPort()); + } + + if (kubernetesApi.getProtocol().equals("https")) { + httpClientOptions.setSsl(true); + httpClientOptions.setTrustAll(true); + } + + HttpClient httpClient = vertx.createHttpClient(httpClientOptions); + + httpClient.getNow("/oapi", res -> { + if (res.statusCode() == HttpResponseStatus.OK.code()) { + log.debug("{} returned {}. We are on OpenShift.", res.request().absoluteURI(), res.statusCode()); + // We should be on OpenShift based on the /oapi result. We can now safely try isAdaptable() to be 100% sure. + Boolean isOpenShift = Boolean.TRUE.equals(client.isAdaptable(OpenShiftClient.class)); + fut.complete(isOpenShift); + } else { + log.debug("{} returned {}. We are not on OpenShift.", res.request().absoluteURI(), res.statusCode()); + fut.complete(Boolean.FALSE); + } + }); + + return fut; + } } diff --git a/cluster-controller/src/test/java/io/strimzi/controller/cluster/ClusterControllerTest.java b/cluster-controller/src/test/java/io/strimzi/controller/cluster/ClusterControllerTest.java index a0d74193bab..4c16a011ae9 100644 --- a/cluster-controller/src/test/java/io/strimzi/controller/cluster/ClusterControllerTest.java +++ b/cluster-controller/src/test/java/io/strimzi/controller/cluster/ClusterControllerTest.java @@ -88,7 +88,7 @@ private void startStop(TestContext context, String namespaces) { env.put(ClusterControllerConfig.STRIMZI_NAMESPACE, namespaces); env.put(ClusterControllerConfig.STRIMZI_CONFIGMAP_LABELS, STRIMZI_IO_KIND_CLUSTER); env.put(ClusterControllerConfig.STRIMZI_FULL_RECONCILIATION_INTERVAL_MS, "120000"); - Main.run(vertx, client, env).setHandler(ar -> { + Main.run(vertx, client, true, env).setHandler(ar -> { context.assertNull(ar.cause(), "Expected all verticles to start OK"); async.complete(); });