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

Handle wildcard parameter in namespace deletion API #442

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
import jakarta.validation.Valid;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Controller to manage the namespaces.
Expand Down Expand Up @@ -120,8 +124,10 @@ public HttpResponse<Namespace> apply(@Valid @Body Namespace namespace,
* @param namespace The namespace
* @param dryrun Is dry run mode or not ?
* @return An HTTP response
* @deprecated use bulkDelete instead.
*/
@Delete("/{namespace}{?dryrun}")
@Deprecated(since = "1.13.0")
public HttpResponse<Void> delete(String namespace, @QueryValue(defaultValue = "false") boolean dryrun) {
Optional<Namespace> optionalNamespace = namespaceService.findByName(namespace);
if (optionalNamespace.isEmpty()) {
Expand All @@ -141,17 +147,69 @@ public HttpResponse<Void> delete(String namespace, @QueryValue(defaultValue = "f
return HttpResponse.noContent();
}

var namespaceToDelete = optionalNamespace.get();
performDeletion(optionalNamespace.get());
return HttpResponse.noContent();
}

sendEventLog(
namespaceToDelete,
ApplyStatus.deleted,
namespaceToDelete.getSpec(),
null,
EMPTY_STRING
/**
* Delete namespaces.
*
* @param dryrun Is dry run mode or not ?
* @param name The name parameter
* @return An HTTP response
*/
@Delete
adriencalime marked this conversation as resolved.
Show resolved Hide resolved
public HttpResponse<Void> bulkDelete(@QueryValue(defaultValue = "*") String name,
@QueryValue(defaultValue = "false") boolean dryrun) {
List<Namespace> namespaces = namespaceService.findByWildcardName(name);
if (namespaces.isEmpty()) {
return HttpResponse.notFound();
}

Map<String, List<String>> validationErrors = new HashMap<>();

namespaces.forEach(namespace ->
adriencalime marked this conversation as resolved.
Show resolved Hide resolved
validationErrors.put(
namespace.getMetadata().getName(),
namespaceService.findAllResourcesByNamespace(namespace)
.stream()
.map(FormatErrorUtils::invalidNamespaceDeleteOperation)
.toList())
);

namespaceService.delete(optionalNamespace.get());
if (validationErrors.values().stream()
.anyMatch(list -> !list.isEmpty())) {
throw new ResourceValidationException(
NAMESPACE,
validationErrors.keySet().stream()
.map(Object::toString)
.collect(Collectors.joining("/")),
validationErrors.values().stream()
.flatMap(Collection::stream)
.toList());
}

if (dryrun) {
return HttpResponse.noContent();
}

namespaces.forEach(this::performDeletion);
return HttpResponse.noContent();
}

/**
* Perform the deletion of the namespace and send an event log.
*
* @param namespace The namespace to delete
*/
private void performDeletion(Namespace namespace) {
loicgreffier marked this conversation as resolved.
Show resolved Hide resolved
sendEventLog(
namespace,
ApplyStatus.deleted,
namespace.getSpec(),
null,
EMPTY_STRING
);
namespaceService.delete(namespace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ void shouldUpdateNamespaceInDryRunMode() {
}

@Test
@SuppressWarnings("deprecation")
void shouldDeleteNamespace() {
Namespace existing = Namespace.builder()
.metadata(Metadata.builder()
Expand All @@ -317,6 +318,7 @@ void shouldDeleteNamespace() {
}

@Test
@SuppressWarnings("deprecation")
void shouldDeleteNamespaceInDryRunMode() {
Namespace existing = Namespace.builder()
.metadata(Metadata.builder()
Expand All @@ -340,6 +342,7 @@ void shouldDeleteNamespaceInDryRunMode() {
}

@Test
@SuppressWarnings("deprecation")
void shouldNotDeleteNamespaceWhenNotFound() {
when(namespaceService.findByName("namespace"))
.thenReturn(Optional.empty());
Expand All @@ -351,6 +354,7 @@ void shouldNotDeleteNamespaceWhenNotFound() {
}

@Test
@SuppressWarnings("deprecation")
void shouldNotDeleteNamespaceWhenResourcesAreStillLinkedWithIt() {
Namespace existing = Namespace.builder()
.metadata(Metadata.builder()
Expand All @@ -371,4 +375,113 @@ void shouldNotDeleteNamespaceWhenResourcesAreStillLinkedWithIt() {
() -> namespaceController.delete("namespace", false));
verify(namespaceService, never()).delete(any());
}

@Test
void shouldDeleteNamespaces() {
Namespace existing1 = Namespace.builder()
.metadata(Metadata.builder()
.name("namespace1")
.cluster("local")
.build())
.spec(Namespace.NamespaceSpec.builder()
.kafkaUser("user")
.build())
.build();
Namespace existing2 = Namespace.builder()
.metadata(Metadata.builder()
.name("namespace2")
.cluster("local")
.build())
.spec(Namespace.NamespaceSpec.builder()
.kafkaUser("user")
.build())
.build();

when(namespaceService.findByWildcardName("namespace*"))
.thenReturn(List.of(existing1, existing2));
when(namespaceService.findAllResourcesByNamespace(existing1))
.thenReturn(List.of());
when(namespaceService.findAllResourcesByNamespace(existing2))
.thenReturn(List.of());
when(securityService.username())
.thenReturn(Optional.of("test-user"));
when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN))
.thenReturn(false);

doNothing().when(applicationEventPublisher).publishEvent(any());
var result = namespaceController.bulkDelete("namespace*", false);
assertEquals(HttpResponse.noContent().getStatus(), result.getStatus());
}

@Test
void shouldDeleteNamespacesInDryRunMode() {
Namespace existing1 = Namespace.builder()
.metadata(Metadata.builder()
.name("namespace1")
.cluster("local")
.build())
.spec(Namespace.NamespaceSpec.builder()
.kafkaUser("user")
.build())
.build();
Namespace existing2 = Namespace.builder()
.metadata(Metadata.builder()
.name("namespace2")
.cluster("local")
.build())
.spec(Namespace.NamespaceSpec.builder()
.kafkaUser("user")
.build())
.build();

when(namespaceService.findByWildcardName("namespace*"))
.thenReturn(List.of(existing1, existing2));
when(namespaceService.findAllResourcesByNamespace(existing1))
.thenReturn(List.of());
when(namespaceService.findAllResourcesByNamespace(existing2))
.thenReturn(List.of());

var result = namespaceController.bulkDelete("namespace*", true);
verify(namespaceService, never()).delete(any());
assertEquals(HttpResponse.noContent().getStatus(), result.getStatus());
}

@Test
void shouldNotDeleteNamespacesWhenResourcesAreStillLinkedWithIt() {
Namespace existing1 = Namespace.builder()
.metadata(Metadata.builder()
.name("namespace1")
.cluster("local")
.build())
.spec(Namespace.NamespaceSpec.builder()
.kafkaUser("user")
.build())
.build();
Namespace existing2 = Namespace.builder()
.metadata(Metadata.builder()
.name("namespace2")
.cluster("local")
.build())
.spec(Namespace.NamespaceSpec.builder()
.kafkaUser("user")
.build())
.build();

when(namespaceService.findByWildcardName("namespace*"))
.thenReturn(List.of(existing1, existing2));
when(namespaceService.findAllResourcesByNamespace(existing1))
.thenReturn(List.of("Topic/topic1"));
when(namespaceService.findAllResourcesByNamespace(existing2))
.thenReturn(List.of());

assertThrows(ResourceValidationException.class,
() -> namespaceController.bulkDelete("namespace*", false));
verify(namespaceService, never()).delete(any());
}

@Test
void shouldNotDeleteNamespacesWhenPatternMatchesNothing() {
var result = namespaceController.bulkDelete("namespace*", false);
adriencalime marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(HttpResponse.notFound().getStatus(), result.getStatus());
}
}