-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BC-6618 automatically delete forgotten namespaces
- Loading branch information
Showing
8 changed files
with
199 additions
and
10 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
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
47 changes: 47 additions & 0 deletions
47
src/main/java/de/svs/scheduling/DeleteDeactivatedNamespaces.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.svs.scheduling; | ||
|
||
import de.svs.Namespace; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.ScheduledExecution; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.jboss.logging.Logger; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
|
||
@ApplicationScoped | ||
public class DeleteDeactivatedNamespaces { | ||
|
||
private static final Logger logger = Logger.getLogger(DeleteDeactivatedNamespaces.class); | ||
private final KubernetesClient kubernetesClient; | ||
|
||
@ConfigProperty(name = "namespace.deletion.afterDaysOfInactivity", defaultValue = "30") | ||
int afterDaysOfInactivity; | ||
|
||
public DeleteDeactivatedNamespaces(KubernetesClient kubernetesClient) { | ||
this.kubernetesClient = kubernetesClient; | ||
} | ||
|
||
@Scheduled(cron = "{namespace.deletion.cron}", concurrentExecution = Scheduled.ConcurrentExecution.SKIP) | ||
void deleteDeactivatedNamespaces(ScheduledExecution execution) { | ||
Instant thirtyDaysAgo = Instant.now().minus(afterDaysOfInactivity, ChronoUnit.DAYS); | ||
List<Namespace> namespacesToDelete = Namespace.findByActivatedUntilOlderThan(thirtyDaysAgo); | ||
logger.info("found namespaces to delete: " + namespacesToDelete.stream().map(ns -> ns.name).toList()); | ||
for (Namespace namespace : namespacesToDelete) { | ||
logger.info("deleting namespace: " + namespace.name); | ||
io.fabric8.kubernetes.api.model.Namespace k8sNamespace = kubernetesClient.namespaces().withName(namespace.name).get(); | ||
if (k8sNamespace != null) { | ||
kubernetesClient.namespaces().withName(namespace.name).delete(); | ||
namespace.delete(); | ||
logger.info("deleted namespace in k8s and db: " + namespace.name); | ||
} else { | ||
namespace.delete(); | ||
logger.info("namespace " + namespace.name + " not found, deleted it only from db"); | ||
} | ||
} | ||
logger.info("finished deleting namespaces"); | ||
} | ||
} |
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,3 +1,5 @@ | ||
quarkus.mongodb.connection-string=mongodb://localhost:27017/keda | ||
baseDomain=.brb.dbildungscloud.dev | ||
quarkus.mongodb.database=keda | ||
namespace.deletion.cron=30 * * * * ? | ||
# to set a kubeconfig you have to set the kubeconfig variable, yes lowercase |
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
90 changes: 90 additions & 0 deletions
90
src/test/java/de/svs/scheduling/DeleteDeactivatedNamespacesTest.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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package de.svs.scheduling; | ||
|
||
import de.svs.Namespace; | ||
import de.svs.QuarkusMongoDbTestResource; | ||
import io.fabric8.kubernetes.api.model.NamespaceBuilder; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.quarkus.scheduler.ScheduledExecution; | ||
import io.quarkus.scheduler.Trigger; | ||
import io.quarkus.test.common.WithTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.kubernetes.client.WithKubernetesTestServer; | ||
import jakarta.inject.Inject; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Instant; | ||
|
||
import static java.time.temporal.ChronoUnit.DAYS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@WithKubernetesTestServer | ||
@QuarkusTest | ||
@WithTestResource(QuarkusMongoDbTestResource.ContainerResource.class) | ||
class DeleteDeactivatedNamespacesTest { | ||
|
||
@Inject | ||
DeleteDeactivatedNamespaces deleteDeactivatedNamespaces; | ||
|
||
@Inject | ||
KubernetesClient k8sClient; | ||
|
||
@Test | ||
void deleteDeactivatedNamespaces() { | ||
Instant instantToBeDeleted = Instant.now().minus(300, DAYS); | ||
Instant justNow = Instant.now(); | ||
|
||
Namespace namespaceThatDoesNotExistInK8s = persistNamespace("namespaceThatDoesNotExistInK8s", instantToBeDeleted); | ||
Namespace namespaceThatExistInK8s = persistNamespace("namespaceThatExistInK8s", instantToBeDeleted); | ||
Namespace freshNamespace = persistNamespace("freshNamespace", justNow); | ||
|
||
createK8sNamespace(namespaceThatExistInK8s); | ||
createK8sNamespace(freshNamespace); | ||
|
||
deleteDeactivatedNamespaces.deleteDeactivatedNamespaces(scheduledExecution()); | ||
|
||
assertThat(Namespace.findByIdOptional(namespaceThatDoesNotExistInK8s.id)).isEmpty(); | ||
assertThat(Namespace.findByIdOptional(namespaceThatExistInK8s.id)).isEmpty(); | ||
assertThat(Namespace.findByIdOptional(freshNamespace.id)).isNotEmpty(); | ||
|
||
assertThat(k8sClient.namespaces().withName(namespaceThatExistInK8s.name).get()).isNull(); | ||
assertThat(k8sClient.namespaces().withName(freshNamespace.name).get()).isNotNull(); | ||
|
||
} | ||
|
||
private void createK8sNamespace(Namespace namespaceThatExistInK8s) { | ||
io.fabric8.kubernetes.api.model.Namespace k8sNamespace = new NamespaceBuilder() | ||
.withNewMetadata() | ||
.withName(namespaceThatExistInK8s.name) | ||
.and() | ||
.build(); | ||
|
||
k8sClient.resource(k8sNamespace).create(); | ||
} | ||
|
||
private static @NotNull ScheduledExecution scheduledExecution() { | ||
return new ScheduledExecution() { | ||
@Override | ||
public Trigger getTrigger() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Instant getFireTime() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Instant getScheduledFireTime() { | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
private static Namespace persistNamespace(String name, Instant activatedUntil) { | ||
Namespace namespace = Namespace.create(name, activatedUntil); | ||
|
||
namespace.persist(); | ||
return namespace; | ||
} | ||
} |
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,3 @@ | ||
quarkus.mongodb.database=keda-test | ||
quarkus.mongodb.database=keda-test | ||
# basically never | ||
namespace.deletion.cron=0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010 |