use of io.fabric8.kubernetes.api.Controller in project strimzi by strimzi.
the class Main method main.
public static void main(String[] args) {
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);
}
});
}
use of io.fabric8.kubernetes.api.Controller in project strimzi by strimzi.
the class TopicController method fromAssembly.
/**
* Create a Topic Controller from the deployed Deployment resource
*
* @param namespace Kubernetes/OpenShift namespace where cluster resources are going to be created
* @param cluster overall cluster name
* @param dep the deployment from which to recover the topic controller state
* @return Topic Controller instance, null if the corresponding Deployment doesn't exist
*/
public static TopicController fromAssembly(String namespace, String cluster, Deployment dep) {
TopicController topicController = null;
if (dep != null) {
topicController = new TopicController(namespace, cluster, Labels.fromResource(dep));
topicController.setReplicas(dep.getSpec().getReplicas());
Container container = dep.getSpec().getTemplate().getSpec().getContainers().get(0);
topicController.setImage(container.getImage());
topicController.setHealthCheckInitialDelay(container.getReadinessProbe().getInitialDelaySeconds());
topicController.setHealthCheckTimeout(container.getReadinessProbe().getTimeoutSeconds());
Map<String, String> vars = containerEnvVars(container);
topicController.setKafkaBootstrapServers(vars.getOrDefault(KEY_KAFKA_BOOTSTRAP_SERVERS, defaultBootstrapServers(cluster)));
topicController.setZookeeperConnect(vars.getOrDefault(KEY_ZOOKEEPER_CONNECT, defaultZookeeperConnect(cluster)));
topicController.setWatchedNamespace(vars.getOrDefault(KEY_WATCHED_NAMESPACE, namespace));
topicController.setReconciliationIntervalMs(vars.getOrDefault(KEY_FULL_RECONCILIATION_INTERVAL_MS, DEFAULT_FULL_RECONCILIATION_INTERVAL_MS));
topicController.setZookeeperSessionTimeoutMs(vars.getOrDefault(KEY_ZOOKEEPER_SESSION_TIMEOUT_MS, DEFAULT_ZOOKEEPER_SESSION_TIMEOUT_MS));
topicController.setTopicConfigMapLabels(vars.getOrDefault(KEY_CONFIGMAP_LABELS, defaultTopicConfigMapLabels(cluster)));
topicController.setTopicMetadataMaxAttempts(Integer.parseInt(vars.getOrDefault(KEY_TOPIC_METADATA_MAX_ATTEMPTS, String.valueOf(DEFAULT_TOPIC_METADATA_MAX_ATTEMPTS))));
}
return topicController;
}
use of io.fabric8.kubernetes.api.Controller in project strimzi by strimzi.
the class ControllerIT method testReconcile.
@Test
public void testReconcile(TestContext context) {
String topicName = "test-reconcile";
Topic topic = new Topic.Builder(topicName, 1, (short) 1, emptyMap()).build();
ConfigMap cm = TopicSerialization.toConfigMap(topic, cmPredicate);
String configMapName = cm.getMetadata().getName();
kubeClient.configMaps().inNamespace(NAMESPACE).create(cm);
// Wait for the configmap to be created
waitFor(context, () -> {
ConfigMap createdCm = kubeClient.configMaps().inNamespace(NAMESPACE).withName(configMapName).get();
LOGGER.info("Polled configmap {} waiting for creation", configMapName);
// modify configmap
if (createdCm != null) {
Map<String, String> data = new HashMap<>(createdCm.getData());
data.put("partitions", "2");
createdCm.setData(data);
kubeClient.configMaps().inNamespace(NAMESPACE).withName(configMapName).patch(createdCm);
}
return createdCm != null;
}, timeout, "Expected the configmap to have been created by now");
// trigger an immediate reconcile, while topic controller is dealing with configmap modification
session.reconcileTopics("periodic");
// Wait for the topic to be created
waitFor(context, () -> {
try {
adminClient.describeTopics(singletonList(topicName)).values().get(topicName).get();
return true;
} catch (ExecutionException e) {
if (e.getCause() instanceof UnknownTopicOrPartitionException) {
return false;
} else {
throw new RuntimeException(e);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}, timeout, "Expected topic to be created by now");
}
use of io.fabric8.kubernetes.api.Controller in project strimzi by strimzi.
the class ControllerTest method configMapAdded.
/**
* Trigger {@link Controller#onConfigMapAdded(ConfigMap, Handler)}
* and have the Kafka and TopicStore respond with the given exceptions.
*/
private Controller configMapAdded(TestContext context, Exception createException, Exception storeException) {
mockKafka.setCreateTopicResponse(topicName.toString(), createException);
mockKafka.setTopicMetadataResponse(topicName, null, null);
mockTopicStore.setCreateTopicResponse(topicName, storeException);
ConfigMap cm = new ConfigMapBuilder().withNewMetadata().withName(topicName.toString()).withLabels(cmPredicate.labels()).endMetadata().withData(map(TopicSerialization.CM_KEY_PARTITIONS, "10", TopicSerialization.CM_KEY_REPLICAS, "2")).build();
Async async = context.async();
controller.onConfigMapAdded(cm, ar -> {
if (createException != null || storeException != null) {
assertFailed(context, ar);
Class<? extends Exception> expectedExceptionType;
if (createException != null) {
expectedExceptionType = createException.getClass();
} else {
expectedExceptionType = storeException.getClass();
}
context.assertEquals(expectedExceptionType, ar.cause().getClass(), ar.cause().getMessage());
TopicName topicName = TopicSerialization.fromConfigMap(cm).getTopicName();
if (createException != null) {
mockKafka.assertNotExists(context, topicName);
} else {
mockKafka.assertExists(context, topicName);
}
mockTopicStore.assertNotExists(context, topicName);
// TODO mockK8s.assertContainsEvent(context, e -> "Error".equals(e.getKind()));
} else {
assertSucceeded(context, ar);
Topic expectedTopic = TopicSerialization.fromConfigMap(cm);
mockKafka.assertContains(context, expectedTopic);
mockTopicStore.assertContains(context, expectedTopic);
mockK8s.assertNoEvents(context);
}
async.complete();
});
return controller;
}
use of io.fabric8.kubernetes.api.Controller in project strimzi by strimzi.
the class ControllerTest method testReconcile_withCm_withKafka_withPrivate_3WayMerge.
/**
* Test reconciliation when a cm has been changed both in kafka and in k8s while the controller was down, and
* a 3 way merge is needed.
*/
@Test
public void testReconcile_withCm_withKafka_withPrivate_3WayMerge(TestContext context) {
Topic kubeTopic = new Topic.Builder(topicName, mapName, 10, (short) 2, map("cleanup.policy", "bar")).build();
Topic kafkaTopic = new Topic.Builder(topicName, mapName, 12, (short) 2, map("cleanup.policy", "baz")).build();
Topic privateTopic = new Topic.Builder(topicName, mapName, 10, (short) 2, map("cleanup.policy", "baz")).build();
Topic resultTopic = new Topic.Builder(topicName, mapName, 12, (short) 2, map("cleanup.policy", "bar")).build();
Async async0 = context.async(3);
mockKafka.setCreateTopicResponse(topicName -> Future.succeededFuture());
mockKafka.createTopic(kafkaTopic, ar -> async0.countDown());
mockKafka.setUpdateTopicResponse(topicName -> Future.succeededFuture());
ConfigMap cm = TopicSerialization.toConfigMap(kubeTopic, cmPredicate);
mockK8s.setCreateResponse(topicName.asMapName(), null);
mockK8s.createConfigMap(cm, ar -> async0.countDown());
mockK8s.setModifyResponse(topicName.asMapName(), null);
mockTopicStore.setCreateTopicResponse(topicName, null);
mockTopicStore.create(privateTopic, ar -> async0.countDown());
async0.await();
Async async = context.async(3);
controller.reconcile(cm, kubeTopic, kafkaTopic, privateTopic, reconcileResult -> {
assertSucceeded(context, reconcileResult);
mockK8s.assertNoEvents(context);
mockTopicStore.read(topicName, readResult -> {
assertSucceeded(context, readResult);
context.assertEquals(resultTopic, readResult.result());
async.countDown();
});
mockK8s.getFromName(topicName.asMapName(), readResult -> {
assertSucceeded(context, readResult);
context.assertEquals(resultTopic, TopicSerialization.fromConfigMap(readResult.result()));
async.countDown();
});
context.assertEquals(resultTopic, mockKafka.getTopicState(topicName));
async.countDown();
});
}
Aggregations