Skip to content

Commit 002c3fd

Browse files
authored
fix (#1334)
1 parent 88ebe79 commit 002c3fd

File tree

4 files changed

+144
-8
lines changed

4 files changed

+144
-8
lines changed

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ private MapPropertySource getMapPropertySourceForSingleConfigMap(ConfigurableEnv
9797

9898
private void addPropertySourcesFromPaths(Environment environment, CompositePropertySource composite) {
9999
Set<String> uniquePaths = new LinkedHashSet<>(properties.getPaths());
100+
LOG.debug("paths property sources : " + uniquePaths);
100101
uniquePaths.stream().map(Paths::get).filter(p -> {
101102
boolean exists = Files.exists(p);
102103
if (!exists) {
@@ -137,7 +138,8 @@ private void addPropertySourceIfNeeded(Function<String, Map<String, Object>> con
137138
LOG.warn("Property source: " + name + "will be ignored because no properties could be found");
138139
}
139140
else {
140-
composite.addFirstPropertySource(new MapPropertySource(name, map));
141+
LOG.debug("will add file-based property source : " + name);
142+
composite.addFirstPropertySource(new MountConfigMapPropertySource(name, map));
141143
}
142144
}
143145

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.commons.config;
18+
19+
import java.util.Map;
20+
21+
import org.springframework.core.env.MapPropertySource;
22+
23+
public final class MountConfigMapPropertySource extends MapPropertySource {
24+
25+
public MountConfigMapPropertySource(String name, Map<String, Object> source) {
26+
super(name, source);
27+
}
28+
29+
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigurationChangeDetector.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
3030
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
31+
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
3132
import org.springframework.core.env.CompositePropertySource;
3233
import org.springframework.core.env.ConfigurableEnvironment;
3334
import org.springframework.core.env.Environment;
@@ -82,8 +83,6 @@ public boolean changed(MapPropertySource left, MapPropertySource right) {
8283

8384
public boolean changed(List<? extends MapPropertySource> left, List<? extends MapPropertySource> right) {
8485
if (left.size() != right.size()) {
85-
this.log.warn("The current number of ConfigMap PropertySources does not match "
86-
+ "the ones loaded from the Kubernetes - No reload will take place");
8786

8887
if (log.isDebugEnabled()) {
8988
this.log.debug(String.format("source 1: %d", left.size()));
@@ -92,12 +91,19 @@ public boolean changed(List<? extends MapPropertySource> left, List<? extends Ma
9291
this.log.debug(String.format("source 2: %d", right.size()));
9392
right.forEach(item -> log.debug(item));
9493
}
94+
this.log.warn("The current number of ConfigMap PropertySources does not match "
95+
+ "the ones loaded from the Kubernetes - No reload will take place");
9596
return false;
9697
}
9798

9899
for (int i = 0; i < left.size(); i++) {
99100
if (changed(left.get(i), right.get(i))) {
100-
return true;
101+
MapPropertySource leftPropertySource = left.get(i);
102+
MapPropertySource rightPropertySource = right.get(i);
103+
if (changed(leftPropertySource, rightPropertySource)) {
104+
this.log.debug("found change in : " + leftPropertySource);
105+
return true;
106+
}
101107
}
102108
}
103109
return false;
@@ -131,8 +137,8 @@ public <S extends PropertySource<?>> List<S> findPropertySources(Class<S> source
131137

132138
LinkedList<PropertySource<?>> sources = toLinkedList(this.environment.getPropertySources());
133139
this.log.debug("findPropertySources");
134-
this.log.debug(String.format("environment: %s", this.environment));
135-
this.log.debug(String.format("environment sources: %s", sources));
140+
this.log.debug(String.format("environment from findPropertySources : %s", this.environment));
141+
this.log.debug(String.format("environment sources from findPropertySources : %s", sources));
136142

137143
while (!sources.isEmpty()) {
138144
PropertySource<?> source = sources.pop();
@@ -143,14 +149,24 @@ public <S extends PropertySource<?>> List<S> findPropertySources(Class<S> source
143149
else if (sourceClass.isInstance(source)) {
144150
managedSources.add(sourceClass.cast(source));
145151
}
152+
else if (source instanceof MountConfigMapPropertySource) {
153+
// we know that the type is correct here
154+
managedSources.add((S) source);
155+
}
146156
else if (source instanceof BootstrapPropertySource) {
147157
PropertySource<?> propertySource = ((BootstrapPropertySource<?>) source).getDelegate();
148158
if (sourceClass.isInstance(propertySource)) {
149159
sources.add(propertySource);
150160
}
161+
else if (propertySource instanceof MountConfigMapPropertySource) {
162+
// we know that the type is correct here
163+
managedSources.add((S) propertySource);
164+
}
151165
}
152166
}
153167

168+
this.log.debug("findPropertySources : " + managedSources.stream().map(PropertySource::getName)
169+
.collect(Collectors.toList()));
154170
return managedSources;
155171
}
156172

@@ -188,8 +204,8 @@ else if (propertySource instanceof CompositePropertySource) {
188204
}
189205

190206
this.log.debug("locateMapPropertySources");
191-
this.log.debug(String.format("environment: %s", environment));
192-
this.log.debug(String.format("sources: %s", result));
207+
this.log.debug(String.format("environment from locateMapPropertySources : %s", environment));
208+
this.log.debug(String.format("sources from locateMapPropertySources : %s", result));
193209

194210
return result;
195211
}

spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload/ConfigurationChangeDetectorTest.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,27 @@
1616

1717
package org.springframework.cloud.kubernetes.fabric8.config.reload;
1818

19+
import java.util.Collection;
1920
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
2324

25+
import org.junit.jupiter.api.Assertions;
2426
import org.junit.jupiter.api.Test;
2527

28+
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
29+
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
2630
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
2731
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationChangeDetector;
2832
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy;
33+
import org.springframework.core.env.CompositePropertySource;
2934
import org.springframework.core.env.ConfigurableEnvironment;
35+
import org.springframework.core.env.EnumerablePropertySource;
3036
import org.springframework.core.env.MapPropertySource;
37+
import org.springframework.core.env.MutablePropertySources;
38+
import org.springframework.core.env.PropertySource;
39+
import org.springframework.mock.env.MockEnvironment;
3140

3241
import static org.assertj.core.api.Assertions.assertThat;
3342

@@ -119,6 +128,34 @@ public void testChangedListSameSizesEqual() {
119128
assertThat(changed).isTrue();
120129
}
121130

131+
@Test
132+
void testFindPropertySources() {
133+
MockEnvironment environment = new MockEnvironment();
134+
MutablePropertySources propertySources = environment.getPropertySources();
135+
propertySources.addFirst(new OneComposite());
136+
propertySources.addFirst(new PlainPropertySource("plain"));
137+
propertySources.addFirst(new OneBootstrap(new EnumerablePropertySource<String>("enumerable") {
138+
@Override
139+
public String[] getPropertyNames() {
140+
return new String[0];
141+
}
142+
143+
@Override
144+
public Object getProperty(String name) {
145+
return null;
146+
}
147+
}));
148+
propertySources.addFirst(new MountConfigMapPropertySource("mounted", Collections.singletonMap("a", "b")));
149+
150+
ConfigurationChangeDetectorStub local = new ConfigurationChangeDetectorStub(environment, null, null);
151+
List<? extends PropertySource> result = local.findPropertySources(PlainPropertySource.class);
152+
Assertions.assertEquals(4, result.size());
153+
Assertions.assertEquals("b", result.get(0).getProperty("a"));
154+
Assertions.assertEquals("plain", result.get(1).getProperty(""));
155+
Assertions.assertEquals("from-bootstrap", result.get(2).getProperty(""));
156+
Assertions.assertEquals("from-inner-two-composite", result.get(3).getProperty(""));
157+
}
158+
122159
/**
123160
* only needed to test some protected methods it defines
124161
*/
@@ -131,4 +168,56 @@ private ConfigurationChangeDetectorStub(ConfigurableEnvironment environment, Con
131168

132169
}
133170

171+
private static final class OneComposite extends CompositePropertySource {
172+
173+
private OneComposite() {
174+
super("one");
175+
}
176+
177+
@Override
178+
public Collection<PropertySource<?>> getPropertySources() {
179+
return Collections.singletonList(new TwoComposite());
180+
}
181+
182+
}
183+
184+
private static final class TwoComposite extends CompositePropertySource {
185+
186+
private TwoComposite() {
187+
super("two");
188+
}
189+
190+
@Override
191+
public Collection<PropertySource<?>> getPropertySources() {
192+
return Collections.singletonList(new PlainPropertySource("from-inner-two-composite"));
193+
}
194+
195+
}
196+
197+
private static final class PlainPropertySource extends PropertySource<String> {
198+
199+
private PlainPropertySource(String name) {
200+
super(name);
201+
}
202+
203+
@Override
204+
public Object getProperty(String name) {
205+
return this.name;
206+
}
207+
208+
}
209+
210+
private static final class OneBootstrap extends BootstrapPropertySource<String> {
211+
212+
private OneBootstrap(EnumerablePropertySource<String> delegate) {
213+
super(delegate);
214+
}
215+
216+
@Override
217+
public PropertySource<String> getDelegate() {
218+
return new PlainPropertySource("from-bootstrap");
219+
}
220+
221+
}
222+
134223
}

0 commit comments

Comments
 (0)