diff --git a/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/client/v1/configmaps/ConfigMapList.java b/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/client/v1/configmaps/ConfigMapList.java index 9f4785e83..aef56a497 100644 --- a/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/client/v1/configmaps/ConfigMapList.java +++ b/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/client/v1/configmaps/ConfigMapList.java @@ -16,6 +16,7 @@ package io.micronaut.kubernetes.client.v1.configmaps; import io.micronaut.core.annotation.Introspected; +import io.micronaut.kubernetes.client.v1.KubernetesObject; import java.util.Collections; import java.util.List; @@ -28,7 +29,7 @@ * @since 1.0.0 */ @Introspected -public class ConfigMapList { +public class ConfigMapList extends KubernetesObject { private List items; diff --git a/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/configuration/KubernetesConfigMapWatcher.java b/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/configuration/KubernetesConfigMapWatcher.java index 10225071e..37ed013d7 100644 --- a/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/configuration/KubernetesConfigMapWatcher.java +++ b/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/configuration/KubernetesConfigMapWatcher.java @@ -39,7 +39,7 @@ import java.util.Map; import java.util.concurrent.ExecutorService; -import static io.micronaut.kubernetes.configuration.KubernetesConfigurationClient.KUBERNETES_CONFIG_MAP_NAME_SUFFIX; +import static io.micronaut.kubernetes.configuration.KubernetesConfigurationClient.getLastConfigMapListResourceVersion; import static io.micronaut.kubernetes.util.KubernetesUtils.computePodLabelSelector; /** @@ -85,7 +85,7 @@ public KubernetesConfigMapWatcher(Environment environment, KubernetesClient clie @SuppressWarnings("ResultOfMethodCallIgnored") @Override public void onApplicationEvent(ServiceReadyEvent event) { - long lastResourceVersion = computeLastResourceVersion(); + long lastResourceVersion = getLastConfigMapListResourceVersion(); Map labels = configuration.getConfigMaps().getLabels(); Flowable singleLabelSelector = computePodLabelSelector(client, configuration.getConfigMaps().getPodLabels(), configuration.getNamespace(), labels); @@ -105,23 +105,6 @@ public void onApplicationEvent(ServiceReadyEvent event) { .subscribe(this::processEvent); } - private long computeLastResourceVersion() { - long lastResourceVersion = environment - .getPropertySources() - .stream() - .filter(propertySource -> propertySource.getName().endsWith(KUBERNETES_CONFIG_MAP_NAME_SUFFIX)) - .map(propertySource -> propertySource.get(KubernetesConfigurationClient.CONFIG_MAP_RESOURCE_VERSION)) - .map(o -> Long.parseLong(o.toString())) - .max(Long::compareTo) - .orElse(0L); - - if (LOG.isDebugEnabled()) { - LOG.debug("Latest resourceVersion is: {}", lastResourceVersion); - } - - return lastResourceVersion; - } - private void processEvent(ConfigMapWatchEvent event) { switch (event.getType()) { case ADDED: diff --git a/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/configuration/KubernetesConfigurationClient.java b/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/configuration/KubernetesConfigurationClient.java index 109c46dd8..d8fd304bc 100644 --- a/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/configuration/KubernetesConfigurationClient.java +++ b/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/configuration/KubernetesConfigurationClient.java @@ -42,6 +42,7 @@ import java.nio.file.Paths; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; import static io.micronaut.kubernetes.client.v1.secrets.Secret.OPAQUE_SECRET_TYPE; import static io.micronaut.kubernetes.util.KubernetesUtils.computePodLabelSelector; @@ -66,6 +67,7 @@ public class KubernetesConfigurationClient implements ConfigurationClient { private static final Logger LOG = LoggerFactory.getLogger(KubernetesConfigurationClient.class); private static Map propertySources = new ConcurrentHashMap<>(); + private static AtomicLong lastConfigMapListResourceVersion = new AtomicLong(); private final KubernetesClient client; private final KubernetesConfiguration configuration; @@ -140,6 +142,13 @@ static Map getPropertySourceCache() { return propertySources; } + /** + * @return the last config map list resource version. + */ + static long getLastConfigMapListResourceVersion() { + return lastConfigMapListResourceVersion.get(); + } + private Flowable getPropertySourcesFromConfigMaps() { Predicate includesFilter = KubernetesUtils.getIncludesFilter(configuration.getConfigMaps().getIncludes()); Predicate excludesFilter = KubernetesUtils.getExcludesFilter(configuration.getConfigMaps().getExcludes()); @@ -150,9 +159,11 @@ private Flowable getPropertySourcesFromConfigMaps() { .doOnError(throwable -> LOG.error("Error while trying to list all Kubernetes ConfigMaps in the namespace [" + configuration.getNamespace() + "]", throwable)) .onErrorReturn(throwable -> new ConfigMapList()) .doOnNext(configMapList -> { + final String resourceVersion = configMapList.getMetadata().getResourceVersion(); if (LOG.isDebugEnabled()) { - LOG.debug("Found {} config maps. Applying includes/excludes filters (if any)", configMapList.getItems().size()); + LOG.debug("Found {} config maps in list version {}. Applying includes/excludes filters (if any)", configMapList.getItems().size(), resourceVersion); } + lastConfigMapListResourceVersion.set(Long.parseLong(resourceVersion)); }) .flatMapIterable(ConfigMapList::getItems) .filter(includesFilter)