From 48a4df29eb8d39d753bdf2141b9051745df203d4 Mon Sep 17 00:00:00 2001 From: ohjongsung Date: Thu, 11 Jun 2020 10:37:33 +0900 Subject: [PATCH 1/2] Configure topic creation with --topic.createEnabled --- README.md | 6 ++ .../kafdrop/controller/ClusterController.java | 8 ++- .../kafdrop/controller/TopicController.java | 8 ++- .../resources/templates/cluster-overview.ftl | 8 ++- src/main/resources/templates/topic-create.ftl | 66 ++++++++++--------- 5 files changed, 59 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index aca68661..18d0b387 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,12 @@ By default, you could delete a topic. If you don't want this feature, you could --topic.deleteEnabled=false ``` +By default, you could create a topic. If you don't want this feature, you could disable it with: + +``` +--topic.createEnabled=false +``` + ## Actuator Health and info endpoints are available at the following path: `/actuator` diff --git a/src/main/java/kafdrop/controller/ClusterController.java b/src/main/java/kafdrop/controller/ClusterController.java index 134a08ac..efb6b40b 100644 --- a/src/main/java/kafdrop/controller/ClusterController.java +++ b/src/main/java/kafdrop/controller/ClusterController.java @@ -23,6 +23,7 @@ import kafdrop.model.*; import kafdrop.service.*; import org.springframework.beans.factory.*; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.info.*; import org.springframework.http.*; import org.springframework.stereotype.*; @@ -41,13 +42,17 @@ public final class ClusterController { private final BuildProperties buildProperties; - public ClusterController(KafkaConfiguration kafkaConfiguration, KafkaMonitor kafkaMonitor, ObjectProvider buildInfoProvider) { + private final boolean topicCreateEnabled; + + public ClusterController(KafkaConfiguration kafkaConfiguration, KafkaMonitor kafkaMonitor, ObjectProvider buildInfoProvider, + @Value("${topic.createEnabled:true}") Boolean topicCreateEnabled) { this.kafkaConfiguration = kafkaConfiguration; this.kafkaMonitor = kafkaMonitor; this.buildProperties = buildInfoProvider.stream() .map(BuildInfo::getBuildProperties) .findAny() .orElseGet(ClusterController::blankBuildProperties); + this.topicCreateEnabled = topicCreateEnabled; } private static BuildProperties blankBuildProperties() { @@ -75,6 +80,7 @@ public String clusterInfo(Model model, model.addAttribute("missingBrokerIds", missingBrokerIds); model.addAttribute("topics", topics); model.addAttribute("clusterSummary", clusterSummary); + model.addAttribute("topicCreateEnabled", topicCreateEnabled); if (filter != null) { model.addAttribute("filter", filter); diff --git a/src/main/java/kafdrop/controller/TopicController.java b/src/main/java/kafdrop/controller/TopicController.java index b9719af8..806f0644 100644 --- a/src/main/java/kafdrop/controller/TopicController.java +++ b/src/main/java/kafdrop/controller/TopicController.java @@ -40,11 +40,13 @@ public final class TopicController { private static final Logger LOG = LoggerFactory.getLogger(TopicController.class); private final KafkaMonitor kafkaMonitor; private final boolean topicDeleteEnabled; + private final boolean topicCreateEnabled; public TopicController(KafkaMonitor kafkaMonitor, - @Value("${topic.deleteEnabled:true}") Boolean topicDeleteEnabled) { + @Value("${topic.deleteEnabled:true}") Boolean topicDeleteEnabled, @Value("${topic.createEnabled:true}") Boolean topicCreateEnabled) { this.kafkaMonitor = kafkaMonitor; this.topicDeleteEnabled = topicDeleteEnabled; + this.topicCreateEnabled = topicCreateEnabled; } @RequestMapping("/{name:.+}") @@ -81,6 +83,7 @@ public String deleteTopic(@PathVariable("name") String topicName, Model model) { */ @RequestMapping("/create") public String createTopicPage(Model model) { + model.addAttribute("topicCreateEnabled", topicCreateEnabled); model.addAttribute("brokersCount", kafkaMonitor.getBrokers().size()); return "topic-create"; } @@ -127,11 +130,12 @@ public String createTopicPage(Model model) { }) @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) public String createTopic(CreateTopicVO createTopicVO, Model model) { - try { + try { kafkaMonitor.createTopic(createTopicVO); } catch (Exception ex) { model.addAttribute("errorMessage", ex.getMessage()); } + model.addAttribute("topicCreateEnabled", topicCreateEnabled); model.addAttribute("brokersCount", kafkaMonitor.getBrokers().size()); model.addAttribute("topicName", createTopicVO.getName()); return "topic-create"; diff --git a/src/main/resources/templates/cluster-overview.ftl b/src/main/resources/templates/cluster-overview.ftl index 16a692ad..a84b4fbb 100644 --- a/src/main/resources/templates/cluster-overview.ftl +++ b/src/main/resources/templates/cluster-overview.ftl @@ -149,9 +149,11 @@ - - New - + <#if topicCreateEnabled> + + New + + diff --git a/src/main/resources/templates/topic-create.ftl b/src/main/resources/templates/topic-create.ftl index 91876768..07a3286c 100644 --- a/src/main/resources/templates/topic-create.ftl +++ b/src/main/resources/templates/topic-create.ftl @@ -22,40 +22,44 @@ <#setting number_format="0">
-

Topic creation

+

Topic creation <#if !topicCreateEnabled> disabled

Back -
-
- - - - - - - - - - - - - - - -
Topic name
Number of partitions
Replication factor
- -
-
- <#if errorMessage??> -

Error creating topic ${topicName}: ${errorMessage}

- <#elseif topicName??> -

Successfully created topic ${topicName}

- -
-
+ + <#if topicCreateEnabled> +
+
+ + + + + + + + + + + + + + + +
Topic name
Number of partitions
Replication factor
+ + +
+
+ <#if errorMessage??> +

Error creating topic ${topicName}: ${errorMessage}

+ <#elseif topicName??> +

Successfully created topic ${topicName}

+ +
+
+
<@template.footer/> From 036510bab60f0a3ec46bfe6332cd3c65f2beedba Mon Sep 17 00:00:00 2001 From: ohjongsung Date: Thu, 11 Jun 2020 13:19:58 +0900 Subject: [PATCH 2/2] --topic.createEnabled flag is also checked in the controller code --- .../kafdrop/controller/TopicController.java | 18 +++++++++++------- src/main/resources/templates/topic-create.ftl | 10 +++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/kafdrop/controller/TopicController.java b/src/main/java/kafdrop/controller/TopicController.java index 806f0644..8867cdd6 100644 --- a/src/main/java/kafdrop/controller/TopicController.java +++ b/src/main/java/kafdrop/controller/TopicController.java @@ -130,14 +130,18 @@ public String createTopicPage(Model model) { }) @RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) public String createTopic(CreateTopicVO createTopicVO, Model model) { + model.addAttribute("topicCreateEnabled", topicCreateEnabled); + model.addAttribute("topicName", createTopicVO.getName()); + if (!topicCreateEnabled) { + model.addAttribute("errorMessage", "Not configured to be created."); + return createTopicPage(model); + } try { kafkaMonitor.createTopic(createTopicVO); - } catch (Exception ex) { - model.addAttribute("errorMessage", ex.getMessage()); - } - model.addAttribute("topicCreateEnabled", topicCreateEnabled); - model.addAttribute("brokersCount", kafkaMonitor.getBrokers().size()); - model.addAttribute("topicName", createTopicVO.getName()); - return "topic-create"; + } catch (Exception ex) { + model.addAttribute("errorMessage", ex.getMessage()); + } + model.addAttribute("brokersCount", kafkaMonitor.getBrokers().size()); + return "topic-create"; } } diff --git a/src/main/resources/templates/topic-create.ftl b/src/main/resources/templates/topic-create.ftl index 07a3286c..3f6b44e1 100644 --- a/src/main/resources/templates/topic-create.ftl +++ b/src/main/resources/templates/topic-create.ftl @@ -52,14 +52,14 @@

- <#if errorMessage??> -

Error creating topic ${topicName}: ${errorMessage}

- <#elseif topicName??> -

Successfully created topic ${topicName}

- + <#if errorMessage??> +

Error creating topic ${topicName}: ${errorMessage}

+ <#elseif topicName??> +

Successfully created topic ${topicName}

+ <@template.footer/>