Search in sources :

Example 41 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi 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 42 with KafkaTopicBuilder

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

the class MockK8s method updateResource.

@Override
public Future<KafkaTopic> updateResource(KafkaTopic topicResource) {
    Promise<KafkaTopic> handler = Promise.promise();
    AsyncResult<Void> response = modifyResponse.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 does not exist, cannot be updated: " + topicResource.getMetadata().getName()));
            return handler.future();
        }
    }
    if (response.succeeded()) {
        Long generation = topicResource.getMetadata().getGeneration();
        handler.complete(new KafkaTopicBuilder(topicResource).editMetadata().withGeneration(generation != null ? generation + 1 : 1).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 43 with KafkaTopicBuilder

use of io.strimzi.api.kafka.model.KafkaTopicBuilder in project strimzi 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)

Example 44 with KafkaTopicBuilder

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

the class TopicOperatorTest method testOnKafkaTopicAdded_invalidResource.

/**
 * Test what happens when a non-topic KafkaTopic gets created in kubernetes
 */
@Test
public void testOnKafkaTopicAdded_invalidResource(VertxTestContext context) {
    KafkaTopic kafkaTopic = new KafkaTopicBuilder().withMetadata(new ObjectMetaBuilder().withName("invalid").withLabels(labels.labels()).build()).withNewSpec().withReplicas(1).withPartitions(1).withConfig(singletonMap(null, null)).endSpec().build();
    String errorMessage = "KafkaTopic's spec.config has invalid entry: The key 'null' of the topic config is invalid: The value corresponding to the key must have a string, number or boolean value but the value was null";
    mockK8s.setGetFromNameResponse(new ResourceName(kafkaTopic), Future.succeededFuture(kafkaTopic));
    LogContext logContext = LogContext.kubeWatch(Watcher.Action.ADDED, kafkaTopic);
    Checkpoint async = context.checkpoint();
    topicOperator.onResourceEvent(logContext, kafkaTopic, ADDED).onComplete(ar -> {
        assertFailed(context, ar);
        context.verify(() -> assertThat(ar.cause(), instanceOf(InvalidTopicException.class)));
        context.verify(() -> assertThat(ar.cause().getMessage(), is(errorMessage)));
        mockKafka.assertEmpty(context);
        mockTopicStore.assertEmpty(context);
        assertNotReadyStatus(context, new InvalidTopicException(null, ar.cause().getMessage()));
        context.verify(() -> {
            MeterRegistry registry = metrics.meterRegistry();
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations").tag("kind", "KafkaTopic").counter().count(), is(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.successful").tag("kind", "KafkaTopic").counter().count(), is(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.failed").tag("kind", "KafkaTopic").counter().count(), is(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", "KafkaTopic").timer().count(), is(0L));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "reconciliations.duration").tag("kind", "KafkaTopic").timer().totalTime(TimeUnit.MILLISECONDS), is(0.0));
            assertThat(registry.get(TopicOperator.METRICS_PREFIX + "resource.state").tag("kind", "KafkaTopic").tag("name", "invalid").tag("resource-namespace", "default-namespace").tag("reason", errorMessage).gauge().value(), is(0.0));
        });
        async.flag();
    });
}
Also used : Checkpoint(io.vertx.junit5.Checkpoint) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 45 with KafkaTopicBuilder

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

the class TopicOperatorTest method testOnKafkaTopicAdded_ignorable.

/**
 * Test what happens when a non-topic KafkaTopic gets created in kubernetes
 */
@Test
public void testOnKafkaTopicAdded_ignorable(VertxTestContext context) {
    KafkaTopic kafkaTopic = new KafkaTopicBuilder().withMetadata(new ObjectMetaBuilder().withName("non-topic").build()).build();
    Checkpoint async = context.checkpoint();
    K8sTopicWatcher w = new K8sTopicWatcher(topicOperator, Future.succeededFuture(), () -> {
    });
    w.eventReceived(ADDED, kafkaTopic);
    mockKafka.assertEmpty(context);
    mockTopicStore.assertEmpty(context);
    async.flag();
}
Also used : Checkpoint(io.vertx.junit5.Checkpoint) KafkaTopic(io.strimzi.api.kafka.model.KafkaTopic) KafkaTopicBuilder(io.strimzi.api.kafka.model.KafkaTopicBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Test(org.junit.jupiter.api.Test)

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