Skip to content

Commit

Permalink
Added endpoint for admins to check quotas of all namespaces (#216)
Browse files Browse the repository at this point in the history
* Started add an endpoint for admins to check quotas of all namespaces

* Fixed unit tests

* Improved code coverage

* Improved code coverage
  • Loading branch information
Loïc GREFFIER authored Nov 7, 2022
1 parent 6962d6f commit e01b14a
Show file tree
Hide file tree
Showing 17 changed files with 644 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
@Tag(name = "Namespaces")
@Controller("/api/namespaces")
public class NamespaceController extends NonNamespacedResourceController {

@Inject
NamespaceService namespaceService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.michelin.ns4kafka.models.Namespace;
import com.michelin.ns4kafka.models.quota.ResourceQuota;
import com.michelin.ns4kafka.models.quota.ResourceQuotaResponse;
import com.michelin.ns4kafka.security.ResourceBasedSecurityRule;
import com.michelin.ns4kafka.services.ResourceQuotaService;
import com.michelin.ns4kafka.utils.enums.ApplyStatus;
import com.michelin.ns4kafka.utils.exceptions.ResourceValidationException;
Expand All @@ -14,6 +15,7 @@
import io.micronaut.scheduling.annotation.ExecuteOn;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.validation.Valid;
import java.time.Instant;
Expand All @@ -25,12 +27,19 @@
@Controller(value = "/api/namespaces/{namespace}/resource-quotas")
@ExecuteOn(TaskExecutors.IO)
public class ResourceQuotaController extends NamespacedResourceController {
/**
* The resource quota service
*/
@Inject
ResourceQuotaService resourceQuotaService;

/**
* Get the sum of all quotas of all namespaces with all the current consumed resources
* @return A quota response
*/
@Get("/all")
@RolesAllowed(ResourceBasedSecurityRule.IS_ADMIN)
public ResourceQuotaResponse listAllNamespaces() {
return resourceQuotaService.getCurrentResourcesQuotasAllNamespaces();
}

/**
* Get all the quotas by namespace
* @param namespace The namespace
Expand All @@ -39,7 +48,8 @@ public class ResourceQuotaController extends NamespacedResourceController {
@Get
public List<ResourceQuotaResponse> list(String namespace) {
Namespace ns = getNamespace(namespace);
return List.of(resourceQuotaService.toResponse(ns, resourceQuotaService.findByNamespace(namespace)));
return List.of(resourceQuotaService.getCurrentResourcesQuotasByNamespace(ns,
resourceQuotaService.findByNamespace(namespace)));
}

/**
Expand All @@ -55,7 +65,7 @@ public Optional<ResourceQuotaResponse> get(String namespace, String quota) {
if (resourceQuota.isEmpty()) {
return Optional.empty();
}
return Optional.of(resourceQuotaService.toResponse(ns, resourceQuota));
return Optional.of(resourceQuotaService.getCurrentResourcesQuotasByNamespace(ns, resourceQuota));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@
import java.util.List;

public interface ConnectorRepository {
/**
* Find all connectors
* @return The list of connectors
*/
List<Connector> findAll();

/**
* Find all connectors by cluster
* @param cluster The cluster
* @return The list of connectors
*/
List<Connector> findAllForCluster(String cluster);

/**
* Create a given connector
* @param connector The connector to create
* @return The created connector
*/
Connector create(Connector connector);

/**
* Delete a given connector
* @param connector The connector to delete
*/
void delete(Connector connector);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import com.michelin.ns4kafka.models.quota.ResourceQuota;

import java.util.List;
import java.util.Optional;

public interface ResourceQuotaRepository {
/**
* Find all quotas of all namespaces
* @return The resource quotas
*/
List<ResourceQuota> findAll();

/**
* Get resource quota by namespace
* @param namespace The namespace used to research
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@
import java.util.List;

public interface TopicRepository {
/**
* Find all topics
* @return The list of topics
*/
List<Topic> findAll();

/***
*
* @param cluster the cluster id
* @return the list of all topics for this cluster as a KV Map with :<br>
* key : String : Topic Name<br>
* value : Topic : Topic data<br>
/**
* Find all topics by cluster
* @param cluster The cluster
* @return The list of topics
*/
List<Topic> findAllForCluster(String cluster);

/**
* Create a given topic
* @param topic The topic to create
* @return The created topic
*/
Topic create(Topic topic);

/**
* Delete a given topic
* @param topic The topic to delete
*/
void delete(Topic topic);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.kafka.clients.producer.Producer;

import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -34,16 +35,39 @@ void receive(ConsumerRecord<String, Connector> record) {
super.receive(record);
}

/**
* Create a given connector
* @param connector The connector to create
* @return The created connector
*/
@Override
public Connector create(Connector connector) {
return this.produce(getMessageKey(connector),connector);
}

/**
* Delete a given connector
* @param connector The connector to delete
*/
@Override
public void delete(Connector connector) {
this.produce(getMessageKey(connector),null);
}

/**
* Find all connectors
* @return The list of connectors
*/
@Override
public List<Connector> findAll() {
return new ArrayList<>(getKafkaStore().values());
}

/**
* Find all connectors by cluster
* @param cluster The cluster
* @return The list of connectors
*/
@Override
public List<Connector> findAllForCluster(String cluster) {
return getKafkaStore().values().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.apache.kafka.clients.producer.Producer;

import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Singleton
Expand All @@ -32,6 +34,15 @@ String getMessageKey(ResourceQuota message) {
return message.getMetadata().getNamespace();
}

/**
* Find all quotas of all namespaces
* @return The resource quotas
*/
@Override
public List<ResourceQuota> findAll() {
return new ArrayList<>(getKafkaStore().values());
}

/**
* Get resource quota by namespace
* @param namespace The namespace used to research
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.kafka.clients.producer.Producer;

import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -32,11 +33,20 @@ String getMessageKey(Topic topic) {
return topic.getMetadata().getCluster()+"/"+topic.getMetadata().getName();
}

/**
* Create a given topic
* @param topic The topic to create
* @return The created topic
*/
@Override
public Topic create(Topic topic) {
return this.produce(getMessageKey(topic), topic);
}

/**
* Delete a given topic
* @param topic The topic to delete
*/
@Override
public void delete(Topic topic) {
this.produce(getMessageKey(topic),null);
Expand All @@ -47,20 +57,25 @@ void receive(ConsumerRecord<String, Topic> record) {
super.receive(record);
}

//@Override
//public List<Topic> findAllForNamespace(Namespace namespace) {
// return getKafkaStore().values()
// .stream()
// .filter(topic -> topic.getMetadata().getCluster().equals(namespace.getCluster()))
// .collect(Collectors.toList());
//}
/**
* Find all topics
* @return The list of topics
*/
@Override
public List<Topic> findAll() {
return new ArrayList<>(getKafkaStore().values());
}

/**
* Find all topics by cluster
* @param cluster The cluster
* @return The list of topics
*/
@Override
public List<Topic> findAllForCluster(String cluster) {
return getKafkaStore().values()
.stream()
.filter(topic -> topic.getMetadata().getCluster().equals(cluster))
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public class ConnectorService {
@Inject
ConnectClusterService connectClusterService;


/**
* Find all connectors
* @return The list of connectors
*/
public List<Connector> findAll() {
return connectorRepository.findAll();
}

/**
* Find all connectors by given namespace
* @param namespace The namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,49 @@

@Singleton
public class NamespaceService {

@Inject
NamespaceRepository namespaceRepository;

@Inject
List<KafkaAsyncExecutorConfig> kafkaAsyncExecutorConfigList;

@Inject
TopicService topicService;

@Inject
RoleBindingService roleBindingService;

@Inject
AccessControlEntryService accessControlEntryService;

@Inject
ConnectorService connectorService;

/**
* Namespace validation in case of new namespace
*
* @param namespace
* @return
* Validate new namespace creation
* @param namespace The namespace to create
* @return A list of validation errors
*/
public List<String> validateCreation(Namespace namespace) {

List<String> validationErrors = new ArrayList<>();

if (kafkaAsyncExecutorConfigList.stream()
.noneMatch(config -> config.getName().equals(namespace.getMetadata().getCluster()))) {
validationErrors.add("Invalid value " + namespace.getMetadata().getCluster()
+ " for cluster: Cluster doesn't exist");
if (kafkaAsyncExecutorConfigList.stream().noneMatch(config -> config.getName().equals(namespace.getMetadata().getCluster()))) {
validationErrors.add("Invalid value " + namespace.getMetadata().getCluster() + " for cluster: Cluster doesn't exist");
}

if (namespaceRepository.findAllForCluster(namespace.getMetadata().getCluster()).stream()
.anyMatch(namespace1 -> namespace1.getSpec().getKafkaUser().equals(namespace.getSpec().getKafkaUser()))) {
validationErrors.add("Invalid value " + namespace.getSpec().getKafkaUser()
+ " for user: KafkaUser already exists");
validationErrors.add("Invalid value " + namespace.getSpec().getKafkaUser() + " for user: KafkaUser already exists");
}

return validationErrors;
}

/**
* Validate the Connect clusters of the namespace
* @param namespace The namespace
* @return A list of validation errors
*/
public List<String> validate(Namespace namespace) {
return namespace.getSpec().getConnectClusters()
.stream()
Expand All @@ -59,6 +65,12 @@ public List<String> validate(Namespace namespace) {
.collect(Collectors.toList());
}

/**
* Check if a given Connect cluster exists on a given Kafka cluster
* @param kafkaCluster The Kafka cluster
* @param connectCluster The Connect cluster
* @return true it does, false otherwise
*/
private boolean connectClusterExists(String kafkaCluster, String connectCluster) {
return kafkaAsyncExecutorConfigList.stream()
.anyMatch(kafkaAsyncExecutorConfig -> kafkaAsyncExecutorConfig.getName().equals(kafkaCluster) &&
Expand Down
Loading

0 comments on commit e01b14a

Please sign in to comment.