From d5547ef53a4ed872199812146623db70ebe9c0ad Mon Sep 17 00:00:00 2001 From: "Schmidt, Sascha (sasschmidt)" Date: Fri, 14 Jun 2024 16:45:39 +0200 Subject: [PATCH] fix(operator): Scale statefulset to 0 to prepare for update When performing a keycloak update, the operator is supposed to make sure that potential database migrations are run with only one pod active. This change makes the operator scale down the stateful set to zero pods in preparation for the update. The next reconciliation loop will scale the stateful set back up and change the image, making sure migrations are being run on the first pod that is brought up. This also makes sure that the rollover works even if the infinispan versions are incompatible. (ref: #30449) Signed-off-by: Schmidt, Sascha (sasschmidt) --- .../controllers/KeycloakDeploymentDependentResource.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeploymentDependentResource.java b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeploymentDependentResource.java index 0afaadb7545b..1987ee14125e 100644 --- a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeploymentDependentResource.java +++ b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeploymentDependentResource.java @@ -461,13 +461,13 @@ public void migrateDeployment(StatefulSet previousDeployment, StatefulSet reconc var reconciledContainer = reconciledDeployment.getSpec().getTemplate().getSpec().getContainers().get(0); if (!previousContainer.getImage().equals(reconciledContainer.getImage()) - && previousDeployment.getStatus().getReplicas() > 1) { + && previousDeployment.getStatus().getReplicas() > 0) { // TODO Check if migration is really needed (e.g. based on actual KC version); https://github.com/keycloak/keycloak/issues/10441 Log.info("Detected changed Keycloak image, assuming Keycloak upgrade. Scaling down the deployment to one instance to perform a safe database migration"); Log.infof("original image: %s; new image: %s", previousContainer.getImage(), reconciledContainer.getImage()); reconciledContainer.setImage(previousContainer.getImage()); - reconciledDeployment.getSpec().setReplicas(1); + reconciledDeployment.getSpec().setReplicas(0); reconciledDeployment.getMetadata().getAnnotations().put(Constants.KEYCLOAK_MIGRATING_ANNOTATION, Boolean.TRUE.toString()); }