Search in sources :

Example 41 with Controller

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);
        }
    });
}
Also used : DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Vertx(io.vertx.core.Vertx)

Example 42 with Controller

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;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container)

Example 43 with Controller

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");
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) HashMap(java.util.HashMap) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) NewTopic(org.apache.kafka.clients.admin.NewTopic) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 44 with Controller

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;
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Async(io.vertx.ext.unit.Async)

Example 45 with 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();
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)16 Controller (io.fabric8.kubernetes.api.Controller)13 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)12 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)12 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)9 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)8 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)8 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)8 Async (io.vertx.ext.unit.Async)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)7 Service (io.fabric8.kubernetes.api.model.Service)6 File (java.io.File)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)5 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)5 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4