Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ConfigMapList's resourceVersion for watching (w/ PropertySource) #300

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +29,7 @@
* @since 1.0.0
*/
@Introspected
public class ConfigMapList {
public class ConfigMapList extends KubernetesObject {

private List<ConfigMap> items;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.KUBERNETES_CONFIG_MAP_LIST_NAME;
import static io.micronaut.kubernetes.util.KubernetesUtils.computePodLabelSelector;

/**
Expand Down Expand Up @@ -109,8 +109,8 @@ 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))
.filter(propertySource -> propertySource.getName().equals(KUBERNETES_CONFIG_MAP_LIST_NAME))
.map(propertySource -> propertySource.get(KubernetesConfigurationClient.CONFIG_MAP_LIST_RESOURCE_VERSION))
.map(o -> Long.parseLong(o.toString()))
.max(Long::compareTo)
.orElse(0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import static io.micronaut.kubernetes.client.v1.secrets.Secret.OPAQUE_SECRET_TYPE;
import static io.micronaut.kubernetes.util.KubernetesUtils.computePodLabelSelector;
import static java.util.Collections.singletonMap;

/**
* A {@link ConfigurationClient} implementation that provides {@link PropertySource}s read from Kubernetes ConfigMap's.
Expand All @@ -59,7 +60,9 @@
@BootstrapContextCompatible
public class KubernetesConfigurationClient implements ConfigurationClient {

public static final String CONFIG_MAP_LIST_RESOURCE_VERSION = "configMapListResourceVersion";
public static final String CONFIG_MAP_RESOURCE_VERSION = "configMapResourceVersion";
public static final String KUBERNETES_CONFIG_MAP_LIST_NAME = "Kubernetes ConfigMapList";
public static final String KUBERNETES_CONFIG_MAP_NAME_SUFFIX = " (Kubernetes ConfigMap)";
public static final String KUBERNETES_SECRET_NAME_SUFFIX = " (Kubernetes Secret)";

Expand Down Expand Up @@ -154,15 +157,30 @@ private Flowable<PropertySource> getPropertySourcesFromConfigMaps() {
LOG.debug("Found {} config maps. Applying includes/excludes filters (if any)", configMapList.getItems().size());
}
})
.flatMapIterable(ConfigMapList::getItems)
.filter(includesFilter)
.filter(excludesFilter)
.doOnNext(configMap -> {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding config map with name {}", configMap.getMetadata().getName());
}
})
.map(KubernetesUtils::configMapAsPropertySource);
.flatMap(configMapList -> Flowable.just(configMapListAsPropertySource(configMapList))
.mergeWith(Flowable.fromIterable(configMapList.getItems())
.filter(includesFilter)
.filter(excludesFilter)
.doOnNext(configMap -> {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding config map with name {}", configMap.getMetadata().getName());
}
})
.map(KubernetesUtils::configMapAsPropertySource)));
}

/**
* Converts a {@link ConfigMapList} into a {@link PropertySource}.
*
* @param configMapList the ConfigMapList
* @return A PropertySource
*/
private static PropertySource configMapListAsPropertySource(ConfigMapList configMapList) {
String resourceVersion = configMapList.getMetadata().getResourceVersion();
if (LOG.isDebugEnabled()) {
LOG.debug("Adding config map list with version {}", resourceVersion);
}
return PropertySource.of(KUBERNETES_CONFIG_MAP_LIST_NAME, singletonMap(CONFIG_MAP_LIST_RESOURCE_VERSION, resourceVersion), EnvironmentPropertySource.POSITION + 100);
}

private Flowable<PropertySource> getPropertySourcesFromSecrets() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class KubernetesConfigurationClientLabelsSpec extends KubernetesSpecification {
def propertySources = Flowable.fromPublisher(configurationClient.getPropertySources(applicationContext.environment)).blockingIterable()

then:
propertySources.size() == 0
propertySources.size() == 1
propertySources.first().name == KubernetesConfigurationClient.KUBERNETES_CONFIG_MAP_LIST_NAME
}

void "it can filter secrets by labels"() {
Expand Down