From 851ecb43fc6b24fdf4a77f61545380d9b63f964d Mon Sep 17 00:00:00 2001 From: Andre Nascimento RH <90404766+andre-nascimento-rh@users.noreply.github.com> Date: Tue, 9 May 2023 19:33:12 +0200 Subject: [PATCH] Stops the recreation of managed Keycloak Statefulset Pods when Keycloak Operator restarts occasionally. (#20187) --- .../main/java/org/keycloak/operator/Constants.java | 14 +++++++++----- .../operator/controllers/KeycloakDeployment.java | 10 ++++------ .../integration/KeycloakDeploymentTest.java | 12 ++++++++++-- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/operator/src/main/java/org/keycloak/operator/Constants.java b/operator/src/main/java/org/keycloak/operator/Constants.java index 3de7e1fb30..c8d38a899e 100644 --- a/operator/src/main/java/org/keycloak/operator/Constants.java +++ b/operator/src/main/java/org/keycloak/operator/Constants.java @@ -16,6 +16,9 @@ */ package org.keycloak.operator; +import org.keycloak.operator.crds.v2alpha1.deployment.ValueOrSecret; + +import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -39,13 +42,14 @@ public final class Constants { .map(e -> e.getKey() + "=" + e.getValue()) .collect(Collectors.joining(",")); - public static final Map DEFAULT_DIST_CONFIG = Map.of( - "health-enabled","true", - "cache", "ispn", - "cache-stack", "kubernetes", - "proxy", "passthrough" + public static final List DEFAULT_DIST_CONFIG_LIST = List.of( + new ValueOrSecret("health-enabled", "true"), + new ValueOrSecret("cache", "ispn"), + new ValueOrSecret("cache-stack", "kubernetes"), + new ValueOrSecret("proxy", "passthrough") ); + public static final Integer KEYCLOAK_HTTP_PORT = 8080; public static final Integer KEYCLOAK_HTTPS_PORT = 8443; public static final String KEYCLOAK_SERVICE_PROTOCOL = "TCP"; diff --git a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeployment.java b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeployment.java index 0e81227ff3..0fa0cdf4e4 100644 --- a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeployment.java +++ b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeployment.java @@ -429,19 +429,17 @@ public class KeycloakDeployment extends OperatorManagedResource implements Statu private List getEnvVars() { // default config values - List serverConfig = Constants.DEFAULT_DIST_CONFIG.entrySet().stream() - .map(e -> new ValueOrSecret(e.getKey(), e.getValue())) - .collect(Collectors.toList()); + List serverConfigsList = new ArrayList<>(Constants.DEFAULT_DIST_CONFIG_LIST); // merge with the CR; the values in CR take precedence if (keycloakCR.getSpec().getAdditionalOptions() != null) { - serverConfig.removeAll(keycloakCR.getSpec().getAdditionalOptions()); - serverConfig.addAll(keycloakCR.getSpec().getAdditionalOptions()); + serverConfigsList.removeAll(keycloakCR.getSpec().getAdditionalOptions()); + serverConfigsList.addAll(keycloakCR.getSpec().getAdditionalOptions()); } // set env vars serverConfigSecretsNames = new HashSet<>(); - List envVars = serverConfig.stream() + List envVars = serverConfigsList.stream() .map(v -> { var envBuilder = new EnvVarBuilder().withName(KeycloakDistConfigurator.getKeycloakOptionEnvVarName(v.getName())); var secret = v.getSecret(); diff --git a/operator/src/test/java/org/keycloak/operator/testsuite/integration/KeycloakDeploymentTest.java b/operator/src/test/java/org/keycloak/operator/testsuite/integration/KeycloakDeploymentTest.java index b9031c6b22..12618404ef 100644 --- a/operator/src/test/java/org/keycloak/operator/testsuite/integration/KeycloakDeploymentTest.java +++ b/operator/src/test/java/org/keycloak/operator/testsuite/integration/KeycloakDeploymentTest.java @@ -145,11 +145,19 @@ public class KeycloakDeploymentTest extends BaseOperatorTest { deployKeycloak(k8sclient, defaultKCDeploy, false); assertThat( - Constants.DEFAULT_DIST_CONFIG.get(valueSecretHealthProp.getName()) + Constants.DEFAULT_DIST_CONFIG_LIST.stream() + .filter(oneValueOrSecret -> oneValueOrSecret.getName().equalsIgnoreCase(valueSecretHealthProp.getName())) + .findFirst() + .get() + .getValue() ).isEqualTo("true"); // just a sanity check default values did not change assertThat( - Constants.DEFAULT_DIST_CONFIG.get(valueSecretProxyProp.getName()) + Constants.DEFAULT_DIST_CONFIG_LIST.stream() + .filter(oneValueOrSecret -> oneValueOrSecret.getName().equalsIgnoreCase(valueSecretProxyProp.getName())) + .findFirst() + .get() + .getValue() ).isEqualTo("passthrough"); // just a sanity check default values did not change Awaitility.await()