Skip to content

Commit

Permalink
Implement workaround hack to avoid RBAC errors (#293)
Browse files Browse the repository at this point in the history
* Implement workaround hack to avoid RABC errors when kubernetes client asks if it is on OpenShift - Closes #286

* Remove hardcoded HTTP 200 and replace it with HttpResponseStatus class

* Move the log messages to debug level
  • Loading branch information
scholzj authored Mar 6, 2018
1 parent 671a0e4 commit 1827fbe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<String, String> env) {
static CompositeFuture run(Vertx vertx, KubernetesClient client, boolean isOpenShift, Map<String, String> env) {
ClusterControllerConfig config = ClusterControllerConfig.fromMap(env);

ServiceOperations serviceOperations = new ServiceOperations(vertx, client);
Expand All @@ -53,7 +67,6 @@ static CompositeFuture run(Vertx vertx, KubernetesClient client, Map<String, Str
PodOperations podOperations = new PodOperations(vertx, client);
EndpointOperations endpointOperations = new EndpointOperations(vertx, client);

boolean isOpenShift = Boolean.TRUE.equals(client.isAdaptable(OpenShiftClient.class));
KafkaClusterOperations kafkaClusterOperations = new KafkaClusterOperations(vertx, isOpenShift, config.getOperationTimeoutMs(), configMapOperations, serviceOperations, statefulSetOperations, pvcOperations, podOperations, endpointOperations, deploymentOperations);
KafkaConnectClusterOperations kafkaConnectClusterOperations = new KafkaConnectClusterOperations(vertx, isOpenShift, configMapOperations, deploymentOperations, serviceOperations);

Expand Down Expand Up @@ -94,4 +107,39 @@ static CompositeFuture run(Vertx vertx, KubernetesClient client, Map<String, Str
}
return CompositeFuture.join(futures);
}

static Future<Boolean> isOnOpenShift(Vertx vertx, KubernetesClient client) {
URL kubernetesApi = client.getMasterUrl();
Future<Boolean> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 1827fbe

Please sign in to comment.