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

Configure topic creation with --topic.createEnabled #148

Merged
merged 2 commits into from
Jun 11, 2020
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/kafdrop/controller/ClusterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -41,13 +42,17 @@ public final class ClusterController {

private final BuildProperties buildProperties;

public ClusterController(KafkaConfiguration kafkaConfiguration, KafkaMonitor kafkaMonitor, ObjectProvider<BuildInfo> buildInfoProvider) {
private final boolean topicCreateEnabled;

public ClusterController(KafkaConfiguration kafkaConfiguration, KafkaMonitor kafkaMonitor, ObjectProvider<BuildInfo> 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() {
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/kafdrop/controller/TopicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:.+}")
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -127,13 +130,18 @@ public String createTopicPage(Model model) {
})
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public String createTopic(CreateTopicVO createTopicVO, Model model) {
try {
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("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";
}
}
8 changes: 5 additions & 3 deletions src/main/resources/templates/cluster-overview.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@
</#list>
</tbody>
</table>
<a class="btn btn-outline-light" href="<@spring.url '/topic/create'/>">
<i class="fa fa-plus"></i> New
</a>
<#if topicCreateEnabled>
<a class="btn btn-outline-light" href="<@spring.url '/topic/create'/>">
<i class="fa fa-plus"></i> New
</a>
</#if>
</div>
</div>

Expand Down
66 changes: 35 additions & 31 deletions src/main/resources/templates/topic-create.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,44 @@

<#setting number_format="0">
<div>
<h2>Topic creation</h2>
<h2>Topic creation <#if !topicCreateEnabled> disabled </#if></h2>
<a class="btn btn-outline-light" href="<@spring.url '/'/>">
Back
</a>
<div id="create-form">
<form action="<@spring.url '/topic'/>" method="POST">
<table class="table table-bordered" style="width: 40%; margin-top: 20px">
<tbody>
<tr>
<td>Topic name</td>
<td align="center"><input type="text" name="name" required></td>
</tr>
<tr>
<td>Number of partitions</td>
<td align="center"><input type="number" name="partitionsNumber" value="1" required></td>
</tr>
<tr>
<td>Replication factor</td>
<td align="center"><input type="number" name="replicationFactor" value="${brokersCount}" required></td>
</tr>
</tbody>
</table>
<button class="btn btn-success" type="submit">
<i class="fa fa-plus"></i> Create
</button>
<br>
<br>
<#if errorMessage??>
<p>Error creating topic ${topicName}: ${errorMessage}</p>
<#elseif topicName??>
<p>Successfully created topic <a href="<@spring.url '/topic/${topicName}'/>">${topicName}</a> </p>
</#if>
</form>
</div>

<#if topicCreateEnabled>
<div id="create-form">
<form action="<@spring.url '/topic'/>" method="POST">
<table class="table table-bordered" style="width: 40%; margin-top: 20px">
<tbody>
<tr>
<td>Topic name</td>
<td align="center"><input type="text" name="name" required></td>
</tr>
<tr>
<td>Number of partitions</td>
<td align="center"><input type="number" name="partitionsNumber" value="1" required></td>
</tr>
<tr>
<td>Replication factor</td>
<td align="center"><input type="number" name="replicationFactor" value="${brokersCount}" required></td>
</tr>
</tbody>
</table>

<button class="btn btn-success" type="submit">
<i class="fa fa-plus"></i> Create
</button>
<br>
<br>
</form>
</div>
</#if>
<#if errorMessage??>
<p>Error creating topic ${topicName}: ${errorMessage}</p>
<#elseif topicName??>
<p>Successfully created topic <a href="<@spring.url '/topic/${topicName}'/>">${topicName}</a> </p>
</#if>
</div>

<@template.footer/>
Expand Down