Skip to content

Commit

Permalink
Keycloak container consumes too much memory in devmode
Browse files Browse the repository at this point in the history
Fixes quarkusio#41813

Signed-off-by: Martin Bartoš <[email protected]>
(cherry picked from commit 4c49feb)
  • Loading branch information
mabartos authored and gsmet committed Oct 18, 2024
1 parent 8eceb18 commit f373db1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -230,6 +231,14 @@ public String getGrantType() {
@ConfigDocMapKey("environment-variable-name")
public Map<String, String> containerEnv;

/**
* Memory limit for Keycloak container
* </p>
* If not specified, 750MiB is the default memory limit.
*/
@ConfigItem(defaultValue = "750M")
public MemorySize containerMemoryLimit;

@Override
public boolean equals(Object o) {
if (this == o)
Expand All @@ -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(containerMemoryLimit, that.containerMemoryLimit);
}

@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, containerMemoryLimit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -375,6 +376,7 @@ private RunningDevService startContainer(DockerStatusBuildItem dockerStatusBuild
capturedDevServicesConfiguration.javaOpts,
capturedDevServicesConfiguration.startCommand,
capturedDevServicesConfiguration.showLogs,
capturedDevServicesConfiguration.containerMemoryLimit,
errors);

timeout.ifPresent(oidcContainer::withStartupTimeout);
Expand Down Expand Up @@ -447,12 +449,13 @@ private static class QuarkusOidcContainer extends GenericContainer<QuarkusOidcCo
private List<RealmRepresentation> realmReps = new LinkedList<>();
private final Optional<String> startCommand;
private final boolean showLogs;
private final MemorySize containerMemoryLimit;
private final List<String> errors;

public QuarkusOidcContainer(DockerImageName dockerImageName, OptionalInt fixedExposedPort, boolean useSharedNetwork,
List<String> realmPaths, Map<String, String> resources, String containerLabelValue,
boolean sharedContainer, Optional<String> javaOpts, Optional<String> startCommand, boolean showLogs,
List<String> errors) {
MemorySize containerMemoryLimit, List<String> errors) {
super(dockerImageName);

this.useSharedNetwork = useSharedNetwork;
Expand All @@ -473,6 +476,7 @@ public QuarkusOidcContainer(DockerImageName dockerImageName, OptionalInt fixedEx
this.fixedExposedPort = fixedExposedPort;
this.startCommand = startCommand;
this.showLogs = showLogs;
this.containerMemoryLimit = containerMemoryLimit;
this.errors = errors;

super.setWaitStrategy(Wait.forLogMessage(".*Keycloak.*started.*", 1));
Expand Down Expand Up @@ -547,6 +551,13 @@ protected void configure() {
});
}

super.withCreateContainerCmdModifier((container) -> Optional.ofNullable(container.getHostConfig())
.ifPresent(hostConfig -> {
final var limit = containerMemoryLimit.asLongValue();
hostConfig.withMemory(limit);
LOG.debug("Set container memory limit (bytes): " + limit);
}));

LOG.infof("Using %s powered Keycloak distribution", keycloakX ? "Quarkus" : "WildFly");
}

Expand Down

0 comments on commit f373db1

Please sign in to comment.