Search in sources :

Example 1 with MirrorCreationData

use of com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData in project quick by bakdata.

the class TopicRegistryInitializer method onStartUp.

/**
 * Ensures the topic registry itself and its topic are created.
 */
@EventListener
@Async
public void onStartUp(final StartupEvent event) {
    // the topic registry is a basically just a mirror application
    // therefore, it needs its own kafka topic
    final Properties properties = new Properties();
    properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, this.kafkaConfig.getBootstrapServer());
    boolean registryExists = false;
    try (final AdminClient admin = AdminClient.create(properties)) {
        final NewTopic immutableTopic = this.topicRegistryConfig.toNewKafkaTopic();
        admin.createTopics(List.of(immutableTopic));
    } catch (final TopicExistsException ignored) {
        log.info("Internal registry topic already exists");
        registryExists = true;
    } catch (final KafkaException e) {
        throw new InternalErrorException("Kafka could not be reached: " + e.getMessage());
    }
    // register the avro schema of the topic data class with the schema registry
    try {
        final String subject = VALUE.asSubject(this.topicRegistryConfig.getTopicName());
        final Schema topicDataSchema = AvroTopicData.getClassSchema();
        this.schemaRegistryClient.register(subject, topicDataSchema);
    } catch (final IOException | RestClientException exception) {
        if (!registryExists) {
            throw new InternalErrorException("Could not register schema for internal topic registry topic");
        }
    }
    final MirrorCreationData topicRegistryCreationData = new MirrorCreationData(this.topicRegistryConfig.getServiceName(), this.topicRegistryConfig.getTopicName(), 1, // null means we use the default tag
    null, null);
    // create topic-registry mirror
    // no retention time
    this.mirrorService.createInternalMirror(topicRegistryCreationData).subscribe(() -> log.info("Deployed internal topic-registry service"), e -> log.info("Could not deploy internal topic-registry service: {}", e.getMessage())).dispose();
}
Also used : MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) Async(io.micronaut.scheduling.annotation.Async) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) KafkaException(org.apache.kafka.common.KafkaException) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) AdminClient(org.apache.kafka.clients.admin.AdminClient) KafkaConfig(com.bakdata.quick.common.config.KafkaConfig) EventListener(io.micronaut.runtime.event.annotation.EventListener) InternalErrorException(com.bakdata.quick.common.exception.InternalErrorException) AvroTopicData(com.bakdata.quick.common.api.model.AvroTopicData) MirrorService(com.bakdata.quick.manager.mirror.MirrorService) Requires(io.micronaut.context.annotation.Requires) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) TopicRegistryConfig(com.bakdata.quick.common.config.TopicRegistryConfig) Properties(java.util.Properties) Schema(org.apache.avro.Schema) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) IOException(java.io.IOException) VALUE(com.bakdata.quick.common.api.model.KeyValueEnum.VALUE) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) StartupEvent(io.micronaut.context.event.StartupEvent) CachedSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient) Schema(org.apache.avro.Schema) InternalErrorException(com.bakdata.quick.common.exception.InternalErrorException) IOException(java.io.IOException) Properties(java.util.Properties) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) NewTopic(org.apache.kafka.clients.admin.NewTopic) KafkaException(org.apache.kafka.common.KafkaException) AdminClient(org.apache.kafka.clients.admin.AdminClient) Async(io.micronaut.scheduling.annotation.Async) EventListener(io.micronaut.runtime.event.annotation.EventListener)

Example 2 with MirrorCreationData

use of com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData in project quick by bakdata.

the class MirrorControllerTest method shouldCreateMirrorWithQueryValues.

@Test
void shouldCreateMirrorWithQueryValues() {
    when(this.service.createMirror(any())).thenReturn(Completable.complete());
    final MirrorCreationData mirrorCreationData = new MirrorCreationData(NAME, NAME, REPLICAS, TAG, null);
    this.httpClient.toBlocking().exchange(POST("topic/mirror", mirrorCreationData));
    verify(this.service).createMirror(mirrorCreationData);
}
Also used : MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 3 with MirrorCreationData

use of com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData in project quick by bakdata.

the class KubernetesMirrorServiceTest method shouldCreateDeployment.

@Test
void shouldCreateDeployment() {
    final MirrorCreationData mirrorCreationData = new MirrorCreationData(TOPIC_NAME, TOPIC_NAME, 1, null, null);
    this.createMirror(mirrorCreationData);
    final List<Deployment> items = this.getDeployments();
    assertThat(items).isNotNull().hasSize(1);
    final Deployment deployment = items.get(0);
    assertThat(deployment.getMetadata()).hasFieldOrPropertyWithValue("name", DEPLOYMENT_NAME);
}
Also used : Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) Test(org.junit.jupiter.api.Test)

Example 4 with MirrorCreationData

use of com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData in project quick by bakdata.

the class KubernetesMirrorServiceTest method shouldCreateDeletionJob.

@Test
void shouldCreateDeletionJob() {
    final MirrorCreationData mirrorCreationData = new MirrorCreationData(TOPIC_NAME, TOPIC_NAME, 1, null, null);
    this.createMirror(mirrorCreationData);
    assertThat(this.getServices()).isNotNull().hasSize(1);
    final Completable deleteRequest = this.mirrorService.deleteMirror(TOPIC_NAME);
    Optional.ofNullable(deleteRequest.blockingGet()).ifPresent(Assertions::fail);
    assertThat(this.client.batch().v1().jobs().list().getItems()).isNotNull().hasSize(1).first().satisfies(job -> assertThat(job.getMetadata().getName()).isEqualTo(DEPLOYMENT_NAME + "-deletion")).extracting(job -> job.getSpec().getTemplate().getSpec().getContainers(), InstanceOfAssertFactories.list(Container.class)).hasSize(1).first().satisfies(container -> assertThat(container.getArgs()).contains("--input-topics=" + TOPIC_NAME));
}
Also used : MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) BeforeEach(org.junit.jupiter.api.BeforeEach) ResourcePrefix(com.bakdata.quick.manager.k8s.resource.QuickResources.ResourcePrefix) Container(io.fabric8.kubernetes.api.model.Container) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Completable(io.reactivex.Completable) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) KubernetesResources(com.bakdata.quick.manager.k8s.KubernetesResources) BadArgumentException(com.bakdata.quick.common.exception.BadArgumentException) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) MirrorResourceLoader(com.bakdata.quick.manager.mirror.resources.MirrorResourceLoader) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Completable(io.reactivex.Completable) Assertions(org.junit.jupiter.api.Assertions) MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) Test(org.junit.jupiter.api.Test)

Example 5 with MirrorCreationData

use of com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData in project quick by bakdata.

the class KubernetesMirrorServiceTest method shouldDeleteDeployment.

@Test
void shouldDeleteDeployment() {
    final MirrorCreationData mirrorCreationData = new MirrorCreationData(TOPIC_NAME, TOPIC_NAME, 1, null, null);
    this.createMirror(mirrorCreationData);
    assertThat(this.getDeployments()).isNotNull().hasSize(1);
    final Completable deleteRequest = this.mirrorService.deleteMirror(TOPIC_NAME);
    Optional.ofNullable(deleteRequest.blockingGet()).ifPresent(Assertions::fail);
    assertThat(this.getDeployments()).isNullOrEmpty();
}
Also used : Completable(io.reactivex.Completable) Assertions(org.junit.jupiter.api.Assertions) MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) Test(org.junit.jupiter.api.Test)

Aggregations

MirrorCreationData (com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData)23 Test (org.junit.jupiter.api.Test)21 KubernetesTest (com.bakdata.quick.manager.k8s.KubernetesTest)17 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)7 KubernetesResources (com.bakdata.quick.manager.k8s.KubernetesResources)5 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)5 MirrorService (com.bakdata.quick.manager.mirror.MirrorService)4 MirrorResourceLoader (com.bakdata.quick.manager.mirror.resources.MirrorResourceLoader)4 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)4 Service (io.fabric8.kubernetes.api.model.Service)4 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)4 Completable (io.reactivex.Completable)4 DeploymentConfig (com.bakdata.quick.manager.config.DeploymentConfig)3 KubernetesMirrorService (com.bakdata.quick.manager.mirror.KubernetesMirrorService)3 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)3 DeploymentSpec (io.fabric8.kubernetes.api.model.apps.DeploymentSpec)3 Duration (java.time.Duration)3 Assertions (org.junit.jupiter.api.Assertions)3 KafkaConfig (com.bakdata.quick.common.config.KafkaConfig)2