From 319d9e78fbba767e0e47939e9e9061bc949ea4da Mon Sep 17 00:00:00 2001 From: Steven Hawkins Date: Fri, 8 Dec 2023 09:14:09 -0500 Subject: [PATCH] fix: use name to find the admin secret (#25362) closes: #25307 Signed-off-by: Steve Hawkins --- .../src/main/java/org/keycloak/operator/Utils.java | 14 ++++++++++++++ .../KeycloakAdminSecretDependentResource.java | 11 ++++++++++- .../KeycloakDiscoveryServiceDependentResource.java | 2 +- .../KeycloakServiceDependentResource.java | 12 +----------- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/operator/src/main/java/org/keycloak/operator/Utils.java b/operator/src/main/java/org/keycloak/operator/Utils.java index 28552e6bf6..50057f38db 100644 --- a/operator/src/main/java/org/keycloak/operator/Utils.java +++ b/operator/src/main/java/org/keycloak/operator/Utils.java @@ -19,6 +19,11 @@ package org.keycloak.operator; import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.client.KubernetesClient; +import io.javaoperatorsdk.operator.api.reconciler.Context; +import io.javaoperatorsdk.operator.processing.event.ResourceID; +import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; + +import org.keycloak.operator.crds.v2alpha1.deployment.Keycloak; import java.nio.charset.StandardCharsets; import java.time.ZoneOffset; @@ -27,6 +32,8 @@ import java.time.format.DateTimeFormatter; import java.util.Base64; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Optional; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -63,4 +70,11 @@ public final class Utils { return labels; } + public static Optional getByName(Class clazz, Function nameFunction, Keycloak primary, Context context) { + InformerEventSource ies = (InformerEventSource) context + .eventSourceRetriever().getResourceEventSourceFor(clazz); + + return ies.get(new ResourceID(nameFunction.apply(primary), primary.getMetadata().getNamespace())); + } + } diff --git a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakAdminSecretDependentResource.java b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakAdminSecretDependentResource.java index d5b18f092b..87ec2b04fd 100644 --- a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakAdminSecretDependentResource.java +++ b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakAdminSecretDependentResource.java @@ -4,6 +4,7 @@ import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; import io.javaoperatorsdk.operator.api.reconciler.Context; +import io.javaoperatorsdk.operator.api.reconciler.ResourceDiscriminator; import io.javaoperatorsdk.operator.api.reconciler.dependent.GarbageCollected; import io.javaoperatorsdk.operator.processing.dependent.Creator; import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; @@ -13,11 +14,19 @@ import org.keycloak.operator.Constants; import org.keycloak.operator.Utils; import org.keycloak.operator.crds.v2alpha1.deployment.Keycloak; +import java.util.Optional; import java.util.UUID; -@KubernetesDependent(labelSelector = Constants.DEFAULT_LABELS_AS_STRING) +@KubernetesDependent(labelSelector = Constants.DEFAULT_LABELS_AS_STRING, resourceDiscriminator = KeycloakAdminSecretDependentResource.NameResourceDiscriminator.class) public class KeycloakAdminSecretDependentResource extends KubernetesDependentResource implements Creator, GarbageCollected { + public static class NameResourceDiscriminator implements ResourceDiscriminator { + @Override + public Optional distinguish(Class resource, Keycloak primary, Context context) { + return Utils.getByName(Secret.class, KeycloakAdminSecretDependentResource::getName, primary, context); + } + } + public KeycloakAdminSecretDependentResource() { super(Secret.class); } diff --git a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDiscoveryServiceDependentResource.java b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDiscoveryServiceDependentResource.java index 9b8952b2f4..cf663036e8 100644 --- a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDiscoveryServiceDependentResource.java +++ b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDiscoveryServiceDependentResource.java @@ -37,7 +37,7 @@ public class KeycloakDiscoveryServiceDependentResource extends CRUDKubernetesDep public static class NameResourceDiscriminator implements ResourceDiscriminator { @Override public Optional distinguish(Class resource, Keycloak primary, Context context) { - return KeycloakServiceDependentResource.getService(KeycloakDiscoveryServiceDependentResource::getName, primary, context); + return Utils.getByName(Service.class, KeycloakDiscoveryServiceDependentResource::getName, primary, context); } } diff --git a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakServiceDependentResource.java b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakServiceDependentResource.java index 7eec56d714..c545f817fc 100644 --- a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakServiceDependentResource.java +++ b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakServiceDependentResource.java @@ -25,8 +25,6 @@ import io.javaoperatorsdk.operator.api.reconciler.Context; import io.javaoperatorsdk.operator.api.reconciler.ResourceDiscriminator; import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; -import io.javaoperatorsdk.operator.processing.event.ResourceID; -import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; import org.keycloak.operator.Constants; import org.keycloak.operator.Utils; @@ -34,7 +32,6 @@ import org.keycloak.operator.crds.v2alpha1.deployment.Keycloak; import org.keycloak.operator.crds.v2alpha1.deployment.spec.HttpSpec; import java.util.Optional; -import java.util.function.Function; import static org.keycloak.operator.crds.v2alpha1.CRDUtils.isTlsConfigured; @@ -44,17 +41,10 @@ public class KeycloakServiceDependentResource extends CRUDKubernetesDependentRes public static class NameResourceDiscriminator implements ResourceDiscriminator { @Override public Optional distinguish(Class resource, Keycloak primary, Context context) { - return getService(KeycloakServiceDependentResource::getServiceName, primary, context); + return Utils.getByName(Service.class, KeycloakServiceDependentResource::getServiceName, primary, context); } } - public static Optional getService(Function nameFunction, Keycloak primary, Context context) { - InformerEventSource ies = (InformerEventSource) context - .eventSourceRetriever().getResourceEventSourceFor(Service.class); - - return ies.get(new ResourceID(nameFunction.apply(primary), primary.getMetadata().getNamespace())); - } - public KeycloakServiceDependentResource() { super(Service.class); }