Skip to content

Commit

Permalink
Container memory for Keycloak
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Bartoš <[email protected]>
  • Loading branch information
mabartos committed Sep 30, 2024
1 parent a98a3f9 commit 141efd5
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, 1G is the default memory limit.
*/
@ConfigItem(defaultValue = "1G")
public MemorySize containerMemLimit;

@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(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);
}
}
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.containerMemLimit,
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 containerMemLimit;
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 containerMemLimit, 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.containerMemLimit = containerMemLimit;
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 = containerMemLimit.asLongValue();
hostConfig.withMemory(limit);
LOG.debug("Set container limit (bytes): " + limit);
}));

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

Expand Down

0 comments on commit 141efd5

Please sign in to comment.