Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kafka Kraft Mode support via confluent-local image #623

Merged
merged 2 commits into from
Oct 11, 2024

Conversation

scprek
Copy link
Contributor

@scprek scprek commented May 17, 2024

Fixes: #622

I attempted to do a separate class, similar to Redis vs RedisCluster mode. That approach did not work. I'm guessing because they were both of AbstractTestContainersProvider<KafkaContainer> and caused the KafkaKraftTestResourceProvider to always be ignored?

In KafkaTestResourceProvider I made sure to provide an empty list of properties if in Kraft Mode.

    @Override
    public List<String> getResolvableProperties(Map<String, Collection<String>> propertyEntries, Map<String, Object> testResourcesConfig) {
        return isKraftMode(testResourcesConfig) ? List.of() : SUPPORTED_PROPERTIES_LIST;
    }

New class

/*
 * Copyright 2017-2021 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.testresources.kafka;

import io.micronaut.testresources.testcontainers.AbstractTestContainersProvider;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.micronaut.testresources.kafka.KafkaConfigurationSupport.isKraftMode;
import static io.micronaut.testresources.kafka.KafkaTestResourceProvider.*;


/**
 * A test resource provider which will spawn a Kafka test container with Kraft mode enabled.
 * see: <a href="https://docs.confluent.io/platform/current/kafka-metadata/kraft.html#kraft-overview">Kafka Kraft Mode</a>
 */
public class KafkaKraftTestResourceProvider extends AbstractTestContainersProvider<KafkaContainer> {


    @Override
    public List<String> getResolvableProperties(Map<String, Collection<String>> propertyEntries, Map<String, Object> testResourcesConfig) {
        return isKraftMode(testResourcesConfig) ? SUPPORTED_PROPERTIES_LIST : List.of();
    }

    @Override
    public String getDisplayName() {
        return DISPLAY_NAME;
    }

    @Override
    protected String getSimpleName() {
        return SIMPLE_NAME;
    }

    @Override
    protected String getDefaultImageName() {
        return DEFAULT_IMAGE;
    }

    /**
     * Starts Kafka container in Kraft mode.
     * See: <a href="https://java.testcontainers.org/modules/kafka/#using-kraft-mode">TestContainers Kafka Kraft Mode</a>
     */
    @Override
    protected KafkaContainer createContainer(DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) {
        return new KafkaContainer(imageName).withKraft();
    }

    @Override
    protected Optional<String> resolveProperty(String propertyName, KafkaContainer container) {
        return Optional.of(container.getBootstrapServers());
    }

    @Override
    protected boolean shouldAnswer(String propertyName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) {
        return KAFKA_BOOTSTRAP_SERVERS.equals(propertyName);
    }
}

@@ -56,7 +63,11 @@ protected String getDefaultImageName() {

@Override
protected KafkaContainer createContainer(DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) {
return new KafkaContainer(imageName);
if (isKraftMode(testResourcesConfig)) {
Copy link
Contributor Author

@scprek scprek May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I attempted to do a separate class that leveraged the statics (e.g. display, simple name) in this class since they were the same.

That approach did not work. I'm guessing because they were both of AbstractTestContainersProvider<KafkaContainer> and caused the KafkaKraftTestResourceProvider to always be ignored?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should take a slightly different approach to fix a discrepancy. The base image is different whether you use kraft mode or not. However, we also have the generic property to override the base image, so the imageName that you get may be different from what getDefaultImageName() returns. Therefore, I would add a check if the image name has been changed, and if so, keep it (but still enable kraft mode on that changed image if needed).

Otherwise, you lose the ability to override the version of the kraft image.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@melix how do I check if the image name has been changed? I see how it's done in AbstractTestContainersProvider, but I cannot use TestContainerMetadataSupport inside KafkaTestResourceProvider because

'io. micronaut. testresources. testcontainers. TestContainerMetadataSupport' is not public in 'io. micronaut. testresources. testcontainers'. Cannot be accessed from outside package

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have access to the imageName as a method parameter. I would compare it with getDefaultImageName()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops 😅

@graemerocher graemerocher requested a review from melix May 19, 2024 17:15
@scprek
Copy link
Contributor Author

scprek commented Jun 5, 2024

@melix sure you are busy, just checking in especially on the alternative approach issue I hit.

Copy link
Collaborator

@melix melix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @scprek , sorry I forgot about this PR.

@@ -56,7 +63,11 @@ protected String getDefaultImageName() {

@Override
protected KafkaContainer createContainer(DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfig) {
return new KafkaContainer(imageName);
if (isKraftMode(testResourcesConfig)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should take a slightly different approach to fix a discrepancy. The base image is different whether you use kraft mode or not. However, we also have the generic property to override the base image, so the imageName that you get may be different from what getDefaultImageName() returns. Therefore, I would add a check if the image name has been changed, and if so, keep it (but still enable kraft mode on that changed image if needed).

Otherwise, you lose the ability to override the version of the kraft image.

@melix
Copy link
Collaborator

melix commented Oct 11, 2024

Thanks for the PR update!

@scprek
Copy link
Contributor Author

scprek commented Oct 11, 2024

Sorry it took so long! Was in micronaut-openapi space for a while ha.

@melix melix changed the base branch from 2.6.x to 2.7.x October 11, 2024 16:24
@melix melix added this to the 2.7.0 milestone Oct 11, 2024
@melix melix merged commit 99e99f1 into micronaut-projects:2.7.x Oct 11, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Kafka Kraft Mode for use in Confluent-local Images
2 participants