Search in sources :

Example 51 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi-kafka-operator by strimzi.

the class TopicOperatorMockTest method testCreatedWithDefaultsInKube.

@Test
public void testCreatedWithDefaultsInKube(VertxTestContext context) throws InterruptedException, ExecutionException {
    int retention = 100_000_000;
    KafkaTopic kt = new KafkaTopicBuilder().withNewMetadata().withName("my-topic").withNamespace(NAMESPACE).addToLabels(Labels.STRIMZI_KIND_LABEL, "topic").endMetadata().withNewSpec().addToConfig("retention.bytes", retention).endSpec().build();
    testCreatedInKube(context, kt);
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) Checkpoint(io.vertx.junit5.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 52 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi-kafka-operator by strimzi.

the class TopicOperatorMockTest method testCreatedWithDifferentTopicNameInKube.

@Test
public void testCreatedWithDifferentTopicNameInKube(VertxTestContext context) throws InterruptedException, ExecutionException {
    int retention = 100_000_000;
    KafkaTopic kt = new KafkaTopicBuilder().withNewMetadata().withName("my-topic").withNamespace(NAMESPACE).addToLabels(Labels.STRIMZI_KIND_LABEL, "topic").endMetadata().withNewSpec().withTopicName(// different to metadata.name
    "DIFFERENT").withPartitions(1).withReplicas(1).addToConfig("retention.bytes", retention).endSpec().build();
    testCreatedInKube(context, kt);
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) Checkpoint(io.vertx.junit5.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 53 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi-kafka-operator by strimzi.

the class TopicOperatorReplicationIT method testKafkaTopicModifiedChangedReplication.

@Test
public void testKafkaTopicModifiedChangedReplication() throws Exception {
    // create the topicResource
    String topicName = "test-kafkatopic-modified-with-changed-replication";
    String resourceName = createTopic(topicName, asList(1));
    // now change the topicResource
    KafkaTopic changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get()).editOrNewSpec().withReplicas(2).endSpec().build();
    operation().inNamespace(NAMESPACE).withName(resourceName).patch(changedTopic);
    assertStatusNotReady(topicName, "Changing 'spec.replicas' is not supported. " + "This KafkaTopic's 'spec.replicas' should be reverted to 1 and then " + "the replication should be changed directly in Kafka.");
    // Now do the revert
    changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get()).editOrNewSpec().withReplicas(1).endSpec().build();
    operation().inNamespace(NAMESPACE).withName(resourceName).patch(changedTopic);
    assertStatusReady(topicName);
    File file = File.createTempFile(getClass().getSimpleName(), ".json");
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode root = new ObjectNode(mapper.getNodeFactory());
    root.put("version", 1).putArray("partitions").addObject().put("topic", topicName).put("partition", 0).putArray("replicas").add(0).add(1);
    mapper.writeValue(file, root);
    LOGGER.info("Creating 2nd replica: {}", mapper.writeValueAsString(root));
    // Now change it in Kafka
    String reassignmentOutput = doReassignmentCommand("--bootstrap-server", kafkaCluster.getBootstrapServers(), "--reassignment-json-file", file.getAbsolutePath(), "--execute");
    LOGGER.info(reassignmentOutput);
    LOGGER.info("Waiting for reassignment completion");
    waitFor(() -> {
        String output = doReassignmentCommand("--bootstrap-server", kafkaCluster.getBootstrapServers(), "--reassignment-json-file", file.getAbsolutePath(), "--verify");
        LOGGER.info(output);
        if (output.contains("Reassignment of partition test-kafkatopic-modified-with-changed-replication-0 is still in progress")) {
            return false;
        } else {
            assertThat("Reassignment is no longer in progress, but wasn't successful: " + output, output.contains("Reassignment of partition test-kafkatopic-modified-with-changed-replication-0 is complete"), is(true));
            return true;
        }
    }, "reassignment completion");
    // wait for reconciliation and that now replicas=2.
    waitFor(() -> {
        KafkaTopic kafkaTopic = Crds.topicOperation(kubeClient).inNamespace(NAMESPACE).withName(resourceName).get();
        LOGGER.info(kafkaTopic == null ? "Null topic" : kafkaTopic.toString());
        return kafkaTopic.getSpec().getReplicas() == 2;
    }, "KafkaTopic.spec.replicas=2");
    // And check that the status is ready
    assertStatusReady(topicName);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 54 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi-kafka-operator by strimzi.

the class MockK8s method createResource.

@Override
public Future<KafkaTopic> createResource(KafkaTopic topicResource) {
    Promise<KafkaTopic> handler = Promise.promise();
    AsyncResult<Void> response = createResponse.apply(new ResourceName(topicResource));
    if (response.succeeded()) {
        AsyncResult<KafkaTopic> old = byName.put(new ResourceName(topicResource), Future.succeededFuture(topicResource));
        if (old != null) {
            handler.handle(Future.failedFuture("resource already existed: " + topicResource.getMetadata().getName()));
            return handler.future();
        }
    }
    if (response.succeeded()) {
        handler.complete(new KafkaTopicBuilder(topicResource).editMetadata().withGeneration(1L).endMetadata().build());
    } else {
        handler.fail(response.cause());
    }
    return handler.future();
}
Also used : KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder)

Example 55 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi-kafka-operator by strimzi.

the class MockK8s method updateResourceStatus.

@Override
public Future<KafkaTopic> updateResourceStatus(Reconciliation reconciliation, KafkaTopic topicResource) {
    statuses.add(new KafkaTopicStatusBuilder(topicResource.getStatus()).build());
    Long generation = topicResource.getMetadata().getGeneration();
    return Future.succeededFuture(new KafkaTopicBuilder(topicResource).editMetadata().withGeneration(generation == null ? 1 : generation + 1).endMetadata().build());
}
Also used : KafkaTopicStatusBuilder(io.strimzi.api.kafka.model.status.KafkaTopicStatusBuilder) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder)

Aggregations

KafkaTopicBuilder (io.strimzi.api.kafka.model.KafkaTopicBuilder)56 KafkaTopic (io.strimzi.api.kafka.model.KafkaTopic)54 Test (org.junit.jupiter.api.Test)40 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)22 Checkpoint (io.vertx.junit5.Checkpoint)22 NewTopic (org.apache.kafka.clients.admin.NewTopic)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)2 Resource (io.fabric8.kubernetes.client.dsl.Resource)2 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)2 KafkaTopicList (io.strimzi.api.kafka.KafkaTopicList)2 KafkaTopicStatusBuilder (io.strimzi.api.kafka.model.status.KafkaTopicStatusBuilder)2 Vertx (io.vertx.core.Vertx)2 VertxExtension (io.vertx.junit5.VertxExtension)2 VertxTestContext (io.vertx.junit5.VertxTestContext)2 File (java.io.File)2