From 12e7b37595010aca87e227cca33a89a579dfdf7e Mon Sep 17 00:00:00 2001 From: Michael Edgar Date: Thu, 11 Apr 2024 08:26:10 -0400 Subject: [PATCH] Default name of TopicName to metadata.name when spec.topicName absent Signed-off-by: Michael Edgar --- .../console/api/support/InformerFactory.java | 11 ++++++++-- .../console/api/TopicsResourceIT.java | 20 +++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/com/github/eyefloaters/console/api/support/InformerFactory.java b/api/src/main/java/com/github/eyefloaters/console/api/support/InformerFactory.java index 53a75c358..bd2338856 100644 --- a/api/src/main/java/com/github/eyefloaters/console/api/support/InformerFactory.java +++ b/api/src/main/java/com/github/eyefloaters/console/api/support/InformerFactory.java @@ -19,6 +19,7 @@ import io.fabric8.kubernetes.client.informers.SharedIndexInformer; import io.strimzi.api.kafka.model.kafka.Kafka; import io.strimzi.api.kafka.model.topic.KafkaTopic; +import io.strimzi.api.kafka.model.topic.KafkaTopicSpec; @ApplicationScoped public class InformerFactory { @@ -58,7 +59,7 @@ void onStartup(@Observes Startup event) { topicInformer.addEventHandler(new ResourceEventHandler() { @Override public void onAdd(KafkaTopic topic) { - topicMap(topic).ifPresent(map -> map.put(topic.getSpec().getTopicName(), topic)); + topicMap(topic).ifPresent(map -> map.put(topicName(topic), topic)); } @Override @@ -69,7 +70,13 @@ public void onUpdate(KafkaTopic oldTopic, KafkaTopic topic) { @Override public void onDelete(KafkaTopic topic, boolean deletedFinalStateUnknown) { - topicMap(topic).ifPresent(map -> map.remove(topic.getSpec().getTopicName())); + topicMap(topic).ifPresent(map -> map.remove(topicName(topic))); + } + + private static String topicName(KafkaTopic topic) { + return Optional.ofNullable(topic.getSpec()) + .map(KafkaTopicSpec::getTopicName) + .orElseGet(() -> topic.getMetadata().getName()); } Optional> topicMap(KafkaTopic topic) { diff --git a/api/src/test/java/com/github/eyefloaters/console/api/TopicsResourceIT.java b/api/src/test/java/com/github/eyefloaters/console/api/TopicsResourceIT.java index d7ccac223..d1e0e61c7 100644 --- a/api/src/test/java/com/github/eyefloaters/console/api/TopicsResourceIT.java +++ b/api/src/test/java/com/github/eyefloaters/console/api/TopicsResourceIT.java @@ -706,8 +706,10 @@ void testListTopicsWithNumPartitions() { 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() @@ -722,6 +724,19 @@ void testListTopicsWithManagedTopic() { .build()) .create(); + 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(); + // Wait for the managed topic list to include the topic await().atMost(10, TimeUnit.SECONDS) .until(() -> Optional.ofNullable(managedTopics.get("default")) @@ -732,9 +747,10 @@ void testListTopicsWithManagedTopic() { whenRequesting(req -> req.get("", clusterId1)) .assertThat() .statusCode(is(Status.OK.getStatusCode())) - .body("data.size()", is(2)) + .body("data.size()", is(3)) .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(topic2), is(false)) + .body("data.find { it.attributes.name == '%s' }.meta.managed".formatted(topic3), is(true)); } @Test