-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
161 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
rootProject.name="kafka-ns" | ||
rootProject.name="ns4kafka" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/com/michelin/ns4kafka/controllers/AdminController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,97 @@ | ||
package com.michelin.ns4kafka.controllers; | ||
|
||
import com.michelin.ns4kafka.models.AccessControlEntry; | ||
import com.michelin.ns4kafka.models.Namespace; | ||
import com.michelin.ns4kafka.repositories.AccessControlEntryRepository; | ||
import com.michelin.ns4kafka.repositories.NamespaceRepository; | ||
import com.michelin.ns4kafka.services.KafkaAsyncExecutorConfig; | ||
import com.michelin.ns4kafka.validation.ResourceValidationException; | ||
import io.micronaut.core.annotation.Introspected; | ||
import io.micronaut.http.annotation.Body; | ||
import io.micronaut.http.annotation.Controller; | ||
import io.micronaut.http.annotation.Post; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import javax.annotation.security.RolesAllowed; | ||
import javax.inject.Inject; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotBlank; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Controller("/api/admin") | ||
public class AdminController { | ||
|
||
@Inject | ||
NamespaceRepository namespaceRepository; | ||
@Inject | ||
AccessControlEntryRepository accessControlEntryRepository; | ||
@Inject | ||
List<KafkaAsyncExecutorConfig> kafkaAsyncExecutorConfigList; | ||
|
||
@RolesAllowed("isAdmin()") | ||
@Post("/namespace") | ||
public Namespace createNamespace(@Valid @Body NamespaceCreationRequest namespaceCreationRequest){ | ||
|
||
// Validation steps: | ||
// - namespace must not already exist | ||
// - cluster must exist | ||
// - kafkaUser must not exist within the namespaces linked to this cluster | ||
// - prefix ? prefix overlap ? "seb" currently exists and we try to create "se" or "seb_a" | ||
// current new check | ||
// seb seb_a new.startswith(current) | ||
// seb se current.startswith(new) | ||
List<String> validationErrors = new ArrayList<>(); | ||
if(namespaceRepository.findByName(namespaceCreationRequest.getName()).isPresent()) { | ||
validationErrors.add("Namespace already exist"); | ||
} | ||
|
||
if(kafkaAsyncExecutorConfigList.stream() | ||
.noneMatch(config -> config.getName().equals(namespaceCreationRequest.getCluster()))) { | ||
validationErrors.add("Cluster doesn't exist"); | ||
} | ||
if(namespaceRepository.findAllForCluster(namespaceCreationRequest.getCluster()).stream() | ||
.anyMatch(namespace -> namespace.getDefaulKafkatUser().equals(namespaceCreationRequest.getKafkaUser()))){ | ||
validationErrors.add("KafkaUser already exist"); | ||
} | ||
List<AccessControlEntry> prefixInUse = accessControlEntryRepository.findAllForCluster(namespaceCreationRequest.getCluster()).stream() | ||
.filter(ace -> ace.getSpec().getResourcePatternType() == AccessControlEntry.ResourcePatternType.PREFIXED) | ||
.filter(ace -> ace.getSpec().getResourceType() == AccessControlEntry.ResourceType.TOPIC) | ||
.filter(ace -> ace.getSpec().getResource().startsWith(namespaceCreationRequest.getPrefix()) | ||
|| namespaceCreationRequest.getPrefix().startsWith(ace.getSpec().getResource())) | ||
.collect(Collectors.toList()); | ||
if(prefixInUse.size()>0) { | ||
validationErrors.add(String.format("Prefix overlaps with namespace %s: [%s]" | ||
, prefixInUse.get(0).getSpec().getGrantedTo() | ||
, prefixInUse.get(0).getSpec().getResource())); | ||
} | ||
|
||
|
||
if(validationErrors.size()>0){ | ||
throw new ResourceValidationException(validationErrors); | ||
} | ||
//TODO this | ||
Namespace toCreate = Namespace.builder().build(); | ||
return toCreate; | ||
} | ||
|
||
|
||
@Introspected | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public static class NamespaceCreationRequest{ | ||
@NotBlank | ||
String name; | ||
@NotBlank | ||
String cluster; | ||
@NotBlank | ||
String kafkaUser; | ||
@NotBlank | ||
String prefix; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 2 additions & 7 deletions
9
src/main/java/com/michelin/ns4kafka/services/KafkaAsyncExecutorScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,26 @@ | ||
package com.michelin.ns4kafka.services; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.inject.qualifiers.Qualifiers; | ||
import io.micronaut.scheduling.annotation.Scheduled; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
|
||
@Singleton | ||
public class KafkaAsyncExecutorScheduler { | ||
private static final Logger LOG = LoggerFactory.getLogger(KafkaAsyncExecutorScheduler.class); | ||
|
||
@Inject | ||
ApplicationContext applicationContext; | ||
@Inject | ||
List<ConnectRestService> connectRestServices; | ||
@Inject | ||
List<KafkaAsyncExecutor> kafkaAsyncExecutors; | ||
|
||
//TODO urgent : start the schedulder only when Application is started (ServerStartupEvent) | ||
@Scheduled(initialDelay = "12s", fixedDelay = "20s") | ||
void schedule(){ | ||
|
||
//TODO sequential forEach with exception handling (to let next clusters sync) | ||
kafkaAsyncExecutors.forEach(KafkaAsyncExecutor::run); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters