From 141efd5c7589a741542caea81d4c15e7755e130f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Barto=C5=A1?=
Date: Mon, 30 Sep 2024 16:40:54 +0200
Subject: [PATCH] Container memory for Keycloak
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Martin Bartoš
---
.../devservices/keycloak/DevServicesConfig.java | 14 ++++++++++++--
.../keycloak/KeycloakDevServicesProcessor.java | 13 ++++++++++++-
2 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/DevServicesConfig.java b/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/DevServicesConfig.java
index ab18a1cad3c45..1b61a94d74a84 100644
--- a/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/DevServicesConfig.java
+++ b/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/DevServicesConfig.java
@@ -10,6 +10,7 @@
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
+import io.quarkus.runtime.configuration.MemorySize;
@ConfigGroup
public class DevServicesConfig {
@@ -230,6 +231,14 @@ public String getGrantType() {
@ConfigDocMapKey("environment-variable-name")
public Map containerEnv;
+ /**
+ * Memory limit for Keycloak container
+ *
+ * If not specified, 1G is the default memory limit.
+ */
+ @ConfigItem(defaultValue = "1G")
+ public MemorySize containerMemLimit;
+
@Override
public boolean equals(Object o) {
if (this == o)
@@ -247,11 +256,12 @@ public boolean equals(Object o) {
&& Objects.equals(users, that.users)
&& Objects.equals(javaOpts, that.javaOpts)
&& Objects.equals(roles, that.roles)
- && Objects.equals(containerEnv, that.containerEnv);
+ && Objects.equals(containerEnv, that.containerEnv)
+ && Objects.equals(containerMemLimit, that.containerMemLimit);
}
@Override
public int hashCode() {
- return Objects.hash(enabled, imageName, port, realmPath, realmName, users, roles, containerEnv);
+ return Objects.hash(enabled, imageName, port, realmPath, realmName, users, roles, containerEnv, containerMemLimit);
}
}
diff --git a/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/KeycloakDevServicesProcessor.java b/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/KeycloakDevServicesProcessor.java
index 9086c453f4e27..02aa287774398 100644
--- a/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/KeycloakDevServicesProcessor.java
+++ b/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/KeycloakDevServicesProcessor.java
@@ -65,6 +65,7 @@
import io.quarkus.oidc.runtime.devui.OidcDevServicesUtils;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.configuration.ConfigUtils;
+import io.quarkus.runtime.configuration.MemorySize;
import io.smallrye.mutiny.TimeoutException;
import io.smallrye.mutiny.Uni;
import io.vertx.core.Vertx;
@@ -375,6 +376,7 @@ private RunningDevService startContainer(DockerStatusBuildItem dockerStatusBuild
capturedDevServicesConfiguration.javaOpts,
capturedDevServicesConfiguration.startCommand,
capturedDevServicesConfiguration.showLogs,
+ capturedDevServicesConfiguration.containerMemLimit,
errors);
timeout.ifPresent(oidcContainer::withStartupTimeout);
@@ -447,12 +449,13 @@ private static class QuarkusOidcContainer extends GenericContainer realmReps = new LinkedList<>();
private final Optional startCommand;
private final boolean showLogs;
+ private final MemorySize containerMemLimit;
private final List errors;
public QuarkusOidcContainer(DockerImageName dockerImageName, OptionalInt fixedExposedPort, boolean useSharedNetwork,
List realmPaths, Map resources, String containerLabelValue,
boolean sharedContainer, Optional javaOpts, Optional startCommand, boolean showLogs,
- List errors) {
+ MemorySize containerMemLimit, List errors) {
super(dockerImageName);
this.useSharedNetwork = useSharedNetwork;
@@ -473,6 +476,7 @@ public QuarkusOidcContainer(DockerImageName dockerImageName, OptionalInt fixedEx
this.fixedExposedPort = fixedExposedPort;
this.startCommand = startCommand;
this.showLogs = showLogs;
+ this.containerMemLimit = containerMemLimit;
this.errors = errors;
super.setWaitStrategy(Wait.forLogMessage(".*Keycloak.*started.*", 1));
@@ -547,6 +551,13 @@ protected void configure() {
});
}
+ super.withCreateContainerCmdModifier((container) -> Optional.ofNullable(container.getHostConfig())
+ .ifPresent(hostConfig -> {
+ final var limit = containerMemLimit.asLongValue();
+ hostConfig.withMemory(limit);
+ LOG.debug("Set container limit (bytes): " + limit);
+ }));
+
LOG.infof("Using %s powered Keycloak distribution", keycloakX ? "Quarkus" : "WildFly");
}