Skip to content

Commit 680441f

Browse files
committed
Merge branch 'fix_secrets_reload_issue' into drop_unused_k8s_client_it_methods
2 parents d3e9637 + a07196d commit 680441f

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ else if (source instanceof MountConfigMapPropertySource mountConfigMapPropertySo
111111
// we know that the type is correct here
112112
managedSources.add((S) mountConfigMapPropertySource);
113113
}
114+
else if (source instanceof SecretsPropertySource secretsPropertySource) {
115+
// we know that the type is correct here
116+
managedSources.add((S) secretsPropertySource);
117+
}
114118
else if (source instanceof BootstrapPropertySource<?> bootstrapPropertySource) {
115119
PropertySource<?> propertySource = bootstrapPropertySource.getDelegate();
116120
LOG.debug(() -> "bootstrap delegate class : " + propertySource.getClass());
@@ -184,8 +188,6 @@ static boolean changed(List<? extends MapPropertySource> k8sSources, List<? exte
184188
for (int i = 0; i < k8sSources.size(); i++) {
185189
MapPropertySource k8sSource = k8sSources.get(i);
186190
MapPropertySource appSource = appSources.get(i);
187-
System.out.println("k8sSource (abc): " + k8sSource.getSource());
188-
System.out.println("appSource (abc): " + appSource.getSource());
189191
if (changed(k8sSource, appSource)) {
190192
LOG.debug(() -> "found change in : " + k8sSource);
191193
return true;

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
2929
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
30+
import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySource;
31+
import org.springframework.cloud.kubernetes.commons.config.SourceData;
3032
import org.springframework.core.env.CompositePropertySource;
3133
import org.springframework.core.env.EnumerablePropertySource;
3234
import org.springframework.core.env.MapPropertySource;
@@ -127,8 +129,8 @@ void testFindPropertySources() {
127129
MockEnvironment environment = new MockEnvironment();
128130
MutablePropertySources propertySources = environment.getPropertySources();
129131
propertySources.addFirst(new OneComposite());
130-
propertySources.addFirst(new PlainPropertySource("plain"));
131-
propertySources.addFirst(new OneBootstrap(new EnumerablePropertySource<>("enumerable") {
132+
propertySources.addFirst(new PlainPropertySource<>("plain"));
133+
propertySources.addFirst(new OneBootstrap<>(new EnumerablePropertySource<>("enumerable") {
132134
@Override
133135
public String[] getPropertyNames() {
134136
return new String[0];
@@ -143,11 +145,35 @@ public Object getProperty(String name) {
143145

144146
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class,
145147
environment);
146-
Assertions.assertEquals(4, result.size());
148+
Assertions.assertEquals(3, result.size());
147149
Assertions.assertEquals("b", result.get(0).getProperty("a"));
148150
Assertions.assertEquals("plain", result.get(1).getProperty(""));
149-
Assertions.assertEquals("from-bootstrap", result.get(2).getProperty(""));
150-
Assertions.assertEquals("from-inner-two-composite", result.get(3).getProperty(""));
151+
Assertions.assertEquals("from-inner-two-composite", result.get(2).getProperty(""));
152+
}
153+
154+
@Test
155+
void testSecretsPropertySource() {
156+
MockEnvironment environment = new MockEnvironment();
157+
MutablePropertySources propertySources = environment.getPropertySources();
158+
propertySources.addFirst(new SecretsPropertySource(new SourceData("secret", Map.of("a", "b"))));
159+
160+
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class,
161+
environment);
162+
assertThat(result.size()).isEqualTo(1);
163+
assertThat(result.get(0).getProperty("a")).isEqualTo("b");
164+
}
165+
166+
@Test
167+
void testBootstrapSecretsPropertySource() {
168+
MockEnvironment environment = new MockEnvironment();
169+
MutablePropertySources propertySources = environment.getPropertySources();
170+
propertySources
171+
.addFirst(new OneBootstrap<>(new SecretsPropertySource(new SourceData("secret", Map.of("a", "b")))));
172+
173+
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class,
174+
environment);
175+
assertThat(result.size()).isEqualTo(1);
176+
assertThat(result.get(0).getProperty("a")).isEqualTo("b");
151177
}
152178

153179
private static final class OneComposite extends CompositePropertySource {
@@ -171,12 +197,12 @@ private TwoComposite() {
171197

172198
@Override
173199
public Collection<PropertySource<?>> getPropertySources() {
174-
return List.of(new PlainPropertySource("from-inner-two-composite"));
200+
return List.of(new PlainPropertySource<>("from-inner-two-composite"));
175201
}
176202

177203
}
178204

179-
private static final class PlainPropertySource extends PropertySource<String> {
205+
private static final class PlainPropertySource<T> extends PropertySource<T> {
180206

181207
private PlainPropertySource(String name) {
182208
super(name);
@@ -189,15 +215,18 @@ public Object getProperty(String name) {
189215

190216
}
191217

192-
private static final class OneBootstrap extends BootstrapPropertySource<String> {
218+
private static final class OneBootstrap<T> extends BootstrapPropertySource<T> {
219+
220+
private final EnumerablePropertySource<T> delegate;
193221

194-
private OneBootstrap(EnumerablePropertySource<String> delegate) {
222+
private OneBootstrap(EnumerablePropertySource<T> delegate) {
195223
super(delegate);
224+
this.delegate = delegate;
196225
}
197226

198227
@Override
199-
public PropertySource<String> getDelegate() {
200-
return new PlainPropertySource("from-bootstrap");
228+
public PropertySource<T> getDelegate() {
229+
return delegate;
201230
}
202231

203232
}

0 commit comments

Comments
 (0)