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

fix: check for presence of KafkaTopic status in managed topic lookup #1267

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ Optional<KafkaTopic> getManagedTopic(String topicName) {
.flatMap(kafkaMeta -> Optional.ofNullable(managedTopics.get(kafkaMeta.getNamespace()))
.map(clustersInNamespace -> clustersInNamespace.get(kafkaMeta.getName()))
.map(topicsInCluster -> topicsInCluster.get(topicName))
// Do not consider topics without a status set by Strimzi as managed
.filter(topic -> Objects.nonNull(topic.getStatus()))
.filter(this::isManaged));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,35 +730,53 @@ void testListTopicsWithManagedTopic() {
String topic1 = "t1-" + UUID.randomUUID().toString();
String topic2 = "t2-" + UUID.randomUUID().toString();
String topic3 = "t3-" + UUID.randomUUID().toString();
topicUtils.createTopics(clusterId1, List.of(topic1), 1);
topicUtils.createTopics(clusterId1, List.of(topic2), 1);
topicUtils.createTopics(clusterId1, List.of(topic3), 1);

client.resource(new KafkaTopicBuilder()
.withNewMetadata()
.withName(topic1)
.withNamespace("default")
.withLabels(Map.of("strimzi.io/cluster", clusterName1))
.endMetadata()
.withNewSpec()
.withTopicName(topic1)
.withPartitions(1)
.endSpec()
.build())
.create();
String topic4 = "t4-" + UUID.randomUUID().toString();
Map<String, String> topics = topicUtils.createTopics(clusterId1, List.of(topic1, topic2, topic3, topic4), 1);

client.resource(new KafkaTopicBuilder()
.withNewMetadata()
.withName(topic3)
.withNamespace("default")
.withLabels(Map.of("strimzi.io/cluster", clusterName1))
.endMetadata()
.withNewSpec()
// topicName (optional) is not set
.withPartitions(1)
.endSpec()
.build())
.create();
utils.apply(client, new KafkaTopicBuilder()
.withNewMetadata()
.withName(topic1)
.withNamespace("default")
.withLabels(Map.of("strimzi.io/cluster", clusterName1))
.endMetadata()
.withNewSpec()
.withTopicName(topic1)
.withPartitions(1)
.endSpec()
.withNewStatus()
.withTopicName(topic1)
.withTopicId(topics.get(topic1))
.endStatus()
.build());

utils.apply(client, new KafkaTopicBuilder()
.withNewMetadata()
.withName(topic3)
.withNamespace("default")
.withLabels(Map.of("strimzi.io/cluster", clusterName1))
.endMetadata()
.withNewSpec()
// topicName (optional) is not set
.withPartitions(1)
.endSpec()
.withNewStatus()
.withTopicName(topic3)
.withTopicId(topics.get(topic3))
.endStatus()
.build());

utils.apply(client, new KafkaTopicBuilder()
.withNewMetadata()
.withName(topic4)
.withNamespace("default")
.withLabels(Map.of("strimzi.io/cluster", clusterName1))
.endMetadata()
.withNewSpec()
.withTopicName(topic4)
.withPartitions(1)
.endSpec()
// No status
.build());

// Wait for the managed topic list to include the topic
await().atMost(10, TimeUnit.SECONDS)
Expand All @@ -770,21 +788,21 @@ void testListTopicsWithManagedTopic() {
whenRequesting(req -> req.get("", clusterId1))
.assertThat()
.statusCode(is(Status.OK.getStatusCode()))
.body("data.size()", is(3))
.body("data.size()", is(4))
.body("data.find { it.attributes.name == '%s' }.meta.managed".formatted(topic1), is(true))
.body("data.find { it.attributes.name == '%s' }.meta.managed".formatted(topic2), is(false))
.body("data.find { it.attributes.name == '%s' }.meta.managed".formatted(topic3), is(true));
.body("data.find { it.attributes.name == '%s' }.meta.managed".formatted(topic3), is(true))
.body("data.find { it.attributes.name == '%s' }.meta.managed".formatted(topic4), is(false));
}

@Test
void testListTopicsWithManagedTopicMissingCluster() {
String topic1 = "t1-" + UUID.randomUUID().toString();
String topic2 = "t2-" + UUID.randomUUID().toString();
topicUtils.createTopics(clusterId1, List.of(topic1), 1);
topicUtils.createTopics(clusterId1, List.of(topic2), 1);
Map<String, String> topics = topicUtils.createTopics(clusterId1, List.of(topic1, topic2), 1);

KafkaTopic topicCR = new KafkaTopicBuilder()
.withNewMetadata()
KafkaTopic topicCR = utils.apply(client, new KafkaTopicBuilder()
.withNewMetadata()
.withName(topic1)
.withNamespace("default")
.withLabels(Map.of("strimzi.io/cluster", clusterName1))
Expand All @@ -793,9 +811,11 @@ void testListTopicsWithManagedTopicMissingCluster() {
.withTopicName(topic1)
.withPartitions(1)
.endSpec()
.build();

client.resource(topicCR).create();
.withNewStatus()
.withTopicName(topic1)
.withTopicId(topics.get(topic1))
.endStatus()
.build());

// Wait for the managed topic list to include the topic
await().atMost(10, TimeUnit.SECONDS)
Expand Down Expand Up @@ -838,11 +858,10 @@ void testListTopicsWithManagedTopicMissingCluster() {
void testListTopicsWithManagedTopicBecomingUnmanaged() {
String topic1 = "t1-" + UUID.randomUUID().toString();
String topic2 = "t2-" + UUID.randomUUID().toString();
topicUtils.createTopics(clusterId1, List.of(topic1), 1);
topicUtils.createTopics(clusterId1, List.of(topic2), 1);
Map<String, String> topics = topicUtils.createTopics(clusterId1, List.of(topic1, topic2), 1);

KafkaTopic topicCR = new KafkaTopicBuilder()
.withNewMetadata()
KafkaTopic topicCR = utils.apply(client, new KafkaTopicBuilder()
.withNewMetadata()
.withName(topic1)
.withNamespace("default")
.withLabels(Map.of("strimzi.io/cluster", clusterName1))
Expand All @@ -851,9 +870,11 @@ void testListTopicsWithManagedTopicBecomingUnmanaged() {
.withTopicName(topic1)
.withPartitions(1)
.endSpec()
.build();

client.resource(topicCR).create();
.withNewStatus()
.withTopicName(topic1)
.withTopicId(topics.get(topic1))
.endStatus()
.build());

// Wait for the managed topic list to include the topic
await().atMost(10, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.strimzi.api.kafka.model.kafka.KafkaBuilder;
import io.strimzi.api.kafka.model.kafka.listener.KafkaListenerAuthentication;
import io.strimzi.api.kafka.model.kafka.listener.KafkaListenerType;
import io.strimzi.api.kafka.model.topic.KafkaTopic;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -123,7 +124,14 @@ public Kafka buildKafkaResource(String name, String id, URI bootstrapServers, Ka
}

public <S, T, C extends CustomResource<S, T>> C apply(KubernetesClient client, C resource) {
client.resource(resource).serverSideApply();
C response = client.resource(resource).serverSideApply();
/*
* Skip status patch for KafkaTopic before Strimzi 0.45.
* https://github.com/strimzi/strimzi-kafka-operator/issues/10905
*/
if (response instanceof KafkaTopic) {
return response;
}
return client.resource(resource).patchStatus();
}

Expand Down