fix: use name to find the admin secret (#25362)

closes: #25307

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
This commit is contained in:
Steven Hawkins 2023-12-08 09:14:09 -05:00 committed by GitHub
parent a8cff72ed0
commit 319d9e78fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 13 deletions

View file

@ -19,6 +19,11 @@ package org.keycloak.operator;
import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient; 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.nio.charset.StandardCharsets;
import java.time.ZoneOffset; import java.time.ZoneOffset;
@ -27,6 +32,8 @@ import java.time.format.DateTimeFormatter;
import java.util.Base64; import java.util.Base64;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -63,4 +70,11 @@ public final class Utils {
return labels; return labels;
} }
public static <T extends HasMetadata> Optional<T> getByName(Class<T> clazz, Function<Keycloak, String> nameFunction, Keycloak primary, Context<Keycloak> context) {
InformerEventSource<T, Keycloak> ies = (InformerEventSource<T, Keycloak>) context
.eventSourceRetriever().getResourceEventSourceFor(clazz);
return ies.get(new ResourceID(nameFunction.apply(primary), primary.getMetadata().getNamespace()));
}
} }

View file

@ -4,6 +4,7 @@ import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.api.model.SecretBuilder;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
import io.javaoperatorsdk.operator.api.reconciler.Context; 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.api.reconciler.dependent.GarbageCollected;
import io.javaoperatorsdk.operator.processing.dependent.Creator; import io.javaoperatorsdk.operator.processing.dependent.Creator;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; 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.Utils;
import org.keycloak.operator.crds.v2alpha1.deployment.Keycloak; import org.keycloak.operator.crds.v2alpha1.deployment.Keycloak;
import java.util.Optional;
import java.util.UUID; 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<Secret, Keycloak> implements Creator<Secret, Keycloak>, GarbageCollected<Keycloak> { public class KeycloakAdminSecretDependentResource extends KubernetesDependentResource<Secret, Keycloak> implements Creator<Secret, Keycloak>, GarbageCollected<Keycloak> {
public static class NameResourceDiscriminator implements ResourceDiscriminator<Secret, Keycloak> {
@Override
public Optional<Secret> distinguish(Class<Secret> resource, Keycloak primary, Context<Keycloak> context) {
return Utils.getByName(Secret.class, KeycloakAdminSecretDependentResource::getName, primary, context);
}
}
public KeycloakAdminSecretDependentResource() { public KeycloakAdminSecretDependentResource() {
super(Secret.class); super(Secret.class);
} }

View file

@ -37,7 +37,7 @@ public class KeycloakDiscoveryServiceDependentResource extends CRUDKubernetesDep
public static class NameResourceDiscriminator implements ResourceDiscriminator<Service, Keycloak> { public static class NameResourceDiscriminator implements ResourceDiscriminator<Service, Keycloak> {
@Override @Override
public Optional<Service> distinguish(Class<Service> resource, Keycloak primary, Context<Keycloak> context) { public Optional<Service> distinguish(Class<Service> resource, Keycloak primary, Context<Keycloak> context) {
return KeycloakServiceDependentResource.getService(KeycloakDiscoveryServiceDependentResource::getName, primary, context); return Utils.getByName(Service.class, KeycloakDiscoveryServiceDependentResource::getName, primary, context);
} }
} }

View file

@ -25,8 +25,6 @@ import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.ResourceDiscriminator; import io.javaoperatorsdk.operator.api.reconciler.ResourceDiscriminator;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; 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.Constants;
import org.keycloak.operator.Utils; 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 org.keycloak.operator.crds.v2alpha1.deployment.spec.HttpSpec;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
import static org.keycloak.operator.crds.v2alpha1.CRDUtils.isTlsConfigured; import static org.keycloak.operator.crds.v2alpha1.CRDUtils.isTlsConfigured;
@ -44,17 +41,10 @@ public class KeycloakServiceDependentResource extends CRUDKubernetesDependentRes
public static class NameResourceDiscriminator implements ResourceDiscriminator<Service, Keycloak> { public static class NameResourceDiscriminator implements ResourceDiscriminator<Service, Keycloak> {
@Override @Override
public Optional<Service> distinguish(Class<Service> resource, Keycloak primary, Context<Keycloak> context) { public Optional<Service> distinguish(Class<Service> resource, Keycloak primary, Context<Keycloak> context) {
return getService(KeycloakServiceDependentResource::getServiceName, primary, context); return Utils.getByName(Service.class, KeycloakServiceDependentResource::getServiceName, primary, context);
} }
} }
public static Optional<Service> getService(Function<Keycloak, String> nameFunction, Keycloak primary, Context<Keycloak> context) {
InformerEventSource<Service, Keycloak> ies = (InformerEventSource<Service, Keycloak>) context
.eventSourceRetriever().getResourceEventSourceFor(Service.class);
return ies.get(new ResourceID(nameFunction.apply(primary), primary.getMetadata().getNamespace()));
}
public KeycloakServiceDependentResource() { public KeycloakServiceDependentResource() {
super(Service.class); super(Service.class);
} }