Skip to content

Commit b71ab6b

Browse files
committed
fix-2148: reload issue
Signed-off-by: wind57 <eugen.rabii@gmail.com>
1 parent 638784e commit b71ab6b

File tree

5 files changed

+138
-13
lines changed

5 files changed

+138
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2013-present 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 class ConfigMapPropertySource extends MapPropertySource {
24+
25+
public ConfigMapPropertySource(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/MountConfigMapPropertySource.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
import java.util.Map;
2020

21-
import org.springframework.core.env.MapPropertySource;
22-
2321
/**
2422
* @author wind57
2523
*/
26-
public final class MountConfigMapPropertySource extends MapPropertySource {
24+
public final class MountConfigMapPropertySource extends ConfigMapPropertySource {
2725

2826
public MountConfigMapPropertySource(String name, Map<String, Object> source) {
2927
super(name, source);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.apache.commons.logging.LogFactory;
3131

3232
import org.springframework.core.env.Environment;
33-
import org.springframework.core.env.MapPropertySource;
3433

3534
import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.KEY_VALUE_TO_PROPERTIES;
3635
import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.PROPERTIES_TO_MAP;
@@ -44,7 +43,7 @@
4443
* @author Ali Shahbour
4544
* @author Michael Moudatsos
4645
*/
47-
public class SourceDataEntriesProcessor extends MapPropertySource {
46+
public class SourceDataEntriesProcessor extends ConfigMapPropertySource {
4847

4948
private static final Log LOG = LogFactory.getLog(SourceDataEntriesProcessor.class);
5049

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626

2727
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
2828
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
29+
import org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySource;
2930
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
3031
import org.springframework.cloud.kubernetes.commons.config.MountSecretPropertySource;
32+
import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySource;
3133
import org.springframework.core.env.CompositePropertySource;
3234
import org.springframework.core.env.ConfigurableEnvironment;
3335
import org.springframework.core.env.MapPropertySource;
@@ -108,11 +110,13 @@ public static <S extends PropertySource<?>> List<S> findPropertySources(Class<S>
108110
else if (sourceClass.isInstance(source)) {
109111
managedSources.add(sourceClass.cast(source));
110112
}
111-
else if (source instanceof MountConfigMapPropertySource mountConfigMapPropertySource) {
113+
else if (source instanceof MountConfigMapPropertySource mountConfigMapPropertySource
114+
&& ConfigMapPropertySource.class.isAssignableFrom(sourceClass)) {
112115
// we know that the type is correct here
113116
managedSources.add((S) mountConfigMapPropertySource);
114117
}
115-
else if (source instanceof MountSecretPropertySource mountSecretPropertySource) {
118+
else if (source instanceof MountSecretPropertySource mountSecretPropertySource
119+
&& SecretsPropertySource.class.isAssignableFrom(sourceClass)) {
116120
// we know that the type is correct here
117121
managedSources.add((S) mountSecretPropertySource);
118122
}

spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtilTests.java

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import org.junit.jupiter.api.Test;
2727

2828
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
29+
import org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySource;
2930
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
3031
import org.springframework.cloud.kubernetes.commons.config.MountSecretPropertySource;
32+
import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySource;
3133
import org.springframework.cloud.kubernetes.commons.config.SourceData;
3234
import org.springframework.core.env.CompositePropertySource;
3335
import org.springframework.core.env.EnumerablePropertySource;
@@ -125,6 +127,7 @@ void testChangedListSameSizesEqual() {
125127
}
126128

127129
@Test
130+
@SuppressWarnings({ "rawtypes", "deprecation" })
128131
void testFindPropertySources() {
129132
MockEnvironment environment = new MockEnvironment();
130133
MutablePropertySources propertySources = environment.getPropertySources();
@@ -141,31 +144,100 @@ public Object getProperty(String name) {
141144
return null;
142145
}
143146
}));
144-
propertySources.addFirst(new MountConfigMapPropertySource("mounted", Map.of("a", "b")));
147+
propertySources.addFirst(new MountConfigMapPropertySource("mounted", Map.of("aa", "bb")));
145148

146149
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class,
147150
environment);
148151

149-
Assertions.assertThat(result.size()).isEqualTo(3);
150-
Assertions.assertThat(result.get(0).getProperty("a")).isEqualTo("b");
151-
Assertions.assertThat(result.get(1).getProperty("")).isEqualTo("plain");
152-
Assertions.assertThat(result.get(2).getProperty("")).isEqualTo("from-inner-two-composite");
153-
152+
Assertions.assertThat(result.size()).isEqualTo(2);
153+
Assertions.assertThat(result.get(0).getProperty("a")).isEqualTo("plain");
154+
Assertions.assertThat(result.get(1).getProperty("")).isEqualTo("from-inner-two-composite");
154155
}
155156

157+
/**
158+
* <pre>
159+
* - in environment we have one MountSecretPropertySource
160+
* - we search for a type that extends SecretPropertySource (SecretsTypePropertySource)
161+
*
162+
* - we pick up MountSecretPropertySource
163+
* </pre>
164+
*/
156165
@Test
166+
@SuppressWarnings({ "rawtypes", "deprecation" })
157167
void testSecretsPropertySource() {
158168
MockEnvironment environment = new MockEnvironment();
159169
MutablePropertySources propertySources = environment.getPropertySources();
160170
propertySources.addFirst(new MountSecretPropertySource(new SourceData("secret", Map.of("a", "b"))));
161171

172+
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(SecretsTypePropertySource.class,
173+
environment);
174+
assertThat(result.size()).isEqualTo(1);
175+
assertThat(result.get(0).getProperty("a")).isEqualTo("b");
176+
}
177+
178+
/**
179+
* <pre>
180+
* - in environment we have one MountSecretPropertySource
181+
* - we search for a type that does not extend SecretPropertySource (PlainPropertySource)
182+
*
183+
* - we don't pick up MountSecretPropertySource
184+
* </pre>
185+
*/
186+
@Test
187+
@SuppressWarnings({ "rawtypes", "deprecation" })
188+
void testSecretsPropertySourceNotTaken() {
189+
MockEnvironment environment = new MockEnvironment();
190+
MutablePropertySources propertySources = environment.getPropertySources();
191+
propertySources.addFirst(new MountSecretPropertySource(new SourceData("secret", Map.of("a", "b"))));
192+
162193
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class,
163194
environment);
195+
assertThat(result.size()).isEqualTo(0);
196+
}
197+
198+
/**
199+
* <pre>
200+
* - in environment we have one MountConfigMapPropertySource
201+
* - we search for a type that extends SecretPropertySource (ConfigMapTypePropertySource)
202+
*
203+
* - we pick up MountConfigMapPropertySource
204+
* </pre>
205+
*/
206+
@Test
207+
@SuppressWarnings({ "rawtypes", "deprecation" })
208+
void testConfigMapPropertySource() {
209+
MockEnvironment environment = new MockEnvironment();
210+
MutablePropertySources propertySources = environment.getPropertySources();
211+
propertySources.addFirst(new MountConfigMapPropertySource("secret", Map.of("a", "b")));
212+
213+
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(ConfigMapTypePropertySource.class,
214+
environment);
164215
assertThat(result.size()).isEqualTo(1);
165216
assertThat(result.get(0).getProperty("a")).isEqualTo("b");
166217
}
167218

219+
/**
220+
* <pre>
221+
* - in environment we have one MountConfigMapPropertySource
222+
* - we search for a type that does not extend ConfigMapPropertySource (PlainPropertySource)
223+
*
224+
* - we don't pick up MountConfigMapPropertySource
225+
* </pre>
226+
*/
168227
@Test
228+
@SuppressWarnings({ "rawtypes", "deprecation" })
229+
void testConfigMapPropertySourceNotTaken() {
230+
MockEnvironment environment = new MockEnvironment();
231+
MutablePropertySources propertySources = environment.getPropertySources();
232+
propertySources.addFirst(new MountSecretPropertySource(new SourceData("secret", Map.of("a", "b"))));
233+
234+
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class,
235+
environment);
236+
assertThat(result.size()).isEqualTo(0);
237+
}
238+
239+
@Test
240+
@SuppressWarnings({ "rawtypes", "deprecation" })
169241
void testBootstrapSecretsPropertySource() {
170242
MockEnvironment environment = new MockEnvironment();
171243
MutablePropertySources propertySources = environment.getPropertySources();
@@ -217,6 +289,29 @@ public Object getProperty(String name) {
217289

218290
}
219291

292+
/**
293+
* simulates Fabric8SecretsPropertySource or KubernetesClientSecretsPropertySource.
294+
*/
295+
private static final class SecretsTypePropertySource<T> extends SecretsPropertySource {
296+
297+
SecretsTypePropertySource(SourceData sourceData) {
298+
super(sourceData);
299+
}
300+
301+
}
302+
303+
/**
304+
* simulates Fabric8ConfigMapPropertySource or
305+
* KubernetesClientConfigMapPropertySource.
306+
*/
307+
private static final class ConfigMapTypePropertySource<T> extends ConfigMapPropertySource {
308+
309+
ConfigMapTypePropertySource(String name, Map<String, Object> source) {
310+
super(name, source);
311+
}
312+
313+
}
314+
220315
private static final class OneBootstrap<T> extends BootstrapPropertySource<T> {
221316

222317
private final EnumerablePropertySource<T> delegate;

0 commit comments

Comments
 (0)