Skip to content

Commit f381736

Browse files
committed
merged main
2 parents 9d8dd17 + 10002a8 commit f381736

File tree

19 files changed

+127
-89
lines changed

19 files changed

+127
-89
lines changed

docs/modules/ROOT/pages/property-source-config/propertysource-reload.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
= `PropertySource` Reload
33

44
WARNING: This functionality has been deprecated in the 2020.0 release. Please see
5-
the xref:spring-cloud-kubernetes-configuration-watcher.adoc#spring-cloud-kubernetes-configuration-watcher[null] controller for an alternative way
6-
to achieve the same functionality.
5+
the xref:spring-cloud-kubernetes-configuration-watcher.adoc#spring-cloud-kubernetes-configuration-watcher[Spring Cloud Kubernetes Configuration Watcher]
6+
controller for an alternative way to achieve the same functionality.
77

88
Some applications may need to detect changes on external property sources and update their internal status to reflect the new configuration.
99
The reload feature of Spring Cloud Kubernetes is able to trigger an application reload when a related `ConfigMap` or

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"dependencies": {
33
"antora": "3.2.0-alpha.4",
44
"@antora/atlas-extension": "1.0.0-alpha.2",
5-
"@antora/collector-extension": "1.0.0-beta.3",
5+
"@antora/collector-extension": "1.0.0-beta.4",
66
"@asciidoctor/tabs": "1.0.0-beta.6",
77
"@springio/antora-extensions": "1.11.1",
88
"@springio/asciidoctor-extensions": "1.0.0-alpha.14"

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ public PropertySource<?> locate(Environment environment) {
8282
if (this.properties.enableApi()) {
8383
Set<NormalizedSource> sources = new LinkedHashSet<>(this.properties.determineSources(environment));
8484
LOG.debug("Config Map normalized sources : " + sources);
85-
sources.forEach(s -> composite
86-
.addFirstPropertySource(getMapPropertySource(s, env, properties.namespacedBatchRead())));
85+
sources.forEach(s -> {
86+
MapPropertySource propertySource = getMapPropertySource(s, env, properties.namespacedBatchRead());
87+
LOG.debug("Adding config map property source " + propertySource.getName());
88+
composite.addFirstPropertySource(propertySource);
89+
});
8790
}
8891

8992
addPropertySourcesFromPaths(environment, composite);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public final class ConfigUtils {
5959
|| sourceName.endsWith("-" + activeProfile + ".yaml")
6060
|| sourceName.endsWith("-" + activeProfile + ".properties");
6161

62-
private static final ApplicationListener<?> NO_OP = (e) -> { };
62+
private static final ApplicationListener<?> NO_OP = (e) -> {
63+
};
6364

6465
private ConfigUtils() {
6566
}
@@ -214,7 +215,7 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
214215
sourceNames.forEach(sourceName -> {
215216
StrippedSourceContainer stripped = hashByName.get(sourceName);
216217
if (stripped != null) {
217-
LOG.debug("Found source with name : '" + sourceName + " in namespace: '" + namespace + "'");
218+
LOG.debug("Found source with name : '" + sourceName + "' in namespace: '" + namespace + "'");
218219
foundSourceNames.add(sourceName);
219220
// see if data is a single yaml/properties file and if it needs decoding
220221
Map<String, String> rawData = stripped.data();
@@ -234,6 +235,9 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
234235
environment, includeDefaultProfileData));
235236
}
236237
}
238+
else {
239+
LOG.warn("sourceName : " + sourceName + " was requested, but not found in namespace : " + namespace);
240+
}
237241
});
238242

239243
return new MultipleSourcesContainer(foundSourceNames, data);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public final SourceData compute(Map<String, String> labels, ConfigUtils.Prefix p
3939
data = dataSupplier(labels);
4040

4141
// need this check because when there is no data, the name of the property
42-
// source
43-
// is using provided labels,
42+
// source is using provided labels,
4443
// unlike when the data is present: when we use secret names
4544
if (data.names().isEmpty()) {
4645
String names = labels.keySet()

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package org.springframework.cloud.kubernetes.commons.config;
1818

1919
import java.util.LinkedHashSet;
20-
import java.util.Map;
2120
import java.util.stream.Collectors;
2221

22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
2325
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException;
2426
import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR;
2527

@@ -31,6 +33,8 @@
3133
*/
3234
public abstract class NamedSourceData {
3335

36+
private static final Log LOG = LogFactory.getLog(NamedSourceData.class);
37+
3438
public final SourceData compute(String sourceName, ConfigUtils.Prefix prefix, String target, boolean profileSources,
3539
boolean failFast, String namespace, String[] activeProfiles) {
3640

@@ -51,7 +55,9 @@ public final SourceData compute(String sourceName, ConfigUtils.Prefix prefix, St
5155
data = dataSupplier(sourceNames);
5256

5357
if (data.names().isEmpty()) {
54-
return new SourceData(ConfigUtils.sourceName(target, sourceName, namespace), Map.of());
58+
String emptySourceName = ConfigUtils.sourceName(target, sourceName, namespace);
59+
LOG.debug("Will return empty source with name : " + emptySourceName);
60+
return SourceData.emptyRecord(emptySourceName);
5561
}
5662

5763
if (prefix != ConfigUtils.Prefix.DEFAULT) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.core.env.CompositePropertySource;
4343
import org.springframework.core.env.ConfigurableEnvironment;
4444
import org.springframework.core.env.Environment;
45+
import org.springframework.core.env.MapPropertySource;
4546
import org.springframework.core.env.PropertySource;
4647

4748
/**
@@ -87,8 +88,12 @@ public PropertySource<?> locate(Environment environment) {
8788
putPathConfig(composite);
8889

8990
if (this.properties.enableApi()) {
90-
uniqueSources.forEach(s -> composite.addPropertySource(
91-
getSecretsPropertySourceForSingleSecret(env, s, properties.namespacedBatchRead())));
91+
uniqueSources.forEach(s -> {
92+
MapPropertySource propertySource = getSecretsPropertySourceForSingleSecret(
93+
env, s, properties.namespacedBatchRead());
94+
LOG.debug("Adding secret property source " + propertySource.getName());
95+
composite.addFirstPropertySource(propertySource);
96+
});
9297
}
9398

9499
cache.discardSecrets();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.config;
1818

19-
import java.util.Collections;
2019
import java.util.Map;
2120

2221
/**
@@ -28,7 +27,7 @@
2827
public record SourceData(String sourceName, Map<String, Object> sourceData) {
2928

3029
public static SourceData emptyRecord(String sourceName) {
31-
return new SourceData(sourceName, Collections.emptyMap());
30+
return new SourceData(sourceName, Map.of());
3231
}
3332

3433
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class SourceDataEntriesProcessor extends MapPropertySource {
4848

4949
private static final Log LOG = LogFactory.getLog(SourceDataEntriesProcessor.class);
5050

51-
private static Predicate<String> ENDS_IN_EXTENSION = x -> x.endsWith(".yml") || x.endsWith(".yaml")
51+
private static final Predicate<String> ENDS_IN_EXTENSION = x -> x.endsWith(".yml") || x.endsWith(".yaml")
5252
|| x.endsWith(".properties");
5353

5454
public SourceDataEntriesProcessor(SourceData sourceData) {

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

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,31 @@ private ConfigReloadUtil() {
4444

4545
private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(ConfigReloadUtil.class));
4646

47-
public static boolean reload(String target, String eventSourceType, PropertySourceLocator locator,
47+
/**
48+
* used for the event based reloading.
49+
*/
50+
public static boolean reload(String target, String sourceAsString, PropertySourceLocator locator,
4851
ConfigurableEnvironment environment, Class<? extends MapPropertySource> existingSourcesType) {
49-
LOG.debug(() -> "onEvent " + target + ": " + eventSourceType);
52+
LOG.debug(() -> "onEvent " + target + ": " + sourceAsString);
53+
54+
return reload(locator, environment, existingSourcesType);
55+
}
56+
57+
/**
58+
* used for the poll based reloading.
59+
*/
60+
public static boolean reload(PropertySourceLocator locator, ConfigurableEnvironment environment,
61+
Class<? extends MapPropertySource> existingSourcesType) {
5062

51-
List<? extends MapPropertySource> sourceFromK8s = locateMapPropertySources(locator, environment);
5263
List<? extends MapPropertySource> existingSources = findPropertySources(existingSourcesType, environment);
5364

65+
if (existingSources.isEmpty()) {
66+
LOG.debug(() -> "no existingSources found, reload will not happen");
67+
return false;
68+
}
69+
70+
List<? extends MapPropertySource> sourceFromK8s = locateMapPropertySources(locator, environment);
71+
5472
boolean changed = changed(sourceFromK8s, existingSources);
5573
if (changed) {
5674
LOG.info("Detected change in config maps/secrets");
@@ -67,7 +85,9 @@ public static boolean reload(String target, String eventSourceType, PropertySour
6785
* @param <S> property source type
6886
* @param sourceClass class for which property sources will be found
6987
* @return finds all registered property sources of the given type
88+
* @deprecated this method will not be public in the next major release.
7089
*/
90+
@Deprecated(forRemoval = false)
7191
public static <S extends PropertySource<?>> List<S> findPropertySources(Class<S> sourceClass,
7292
ConfigurableEnvironment environment) {
7393
List<S> managedSources = new ArrayList<>();
@@ -141,25 +161,25 @@ else if (propertySource instanceof CompositePropertySource source) {
141161
return result;
142162
}
143163

144-
static boolean changed(List<? extends MapPropertySource> left, List<? extends MapPropertySource> right) {
145-
if (left.size() != right.size()) {
164+
static boolean changed(List<? extends MapPropertySource> k8sSources, List<? extends MapPropertySource> appSources) {
165+
if (k8sSources.size() != appSources.size()) {
146166
if (LOG.isDebugEnabled()) {
147-
LOG.debug("left size: " + left.size());
148-
left.forEach(item -> LOG.debug(item.toString()));
167+
LOG.debug("k8s property sources size: " + k8sSources.size());
168+
k8sSources.forEach(item -> LOG.debug(item.toString()));
149169

150-
LOG.debug("right size: " + right.size());
151-
right.forEach(item -> LOG.debug(item.toString()));
170+
LOG.debug("app property sources size size: " + appSources.size());
171+
appSources.forEach(item -> LOG.debug(item.toString()));
152172
}
153-
LOG.warn(() -> "The current number of ConfigMap PropertySources does not match "
173+
LOG.warn(() -> "The current number of PropertySources does not match "
154174
+ "the ones loaded from Kubernetes - No reload will take place");
155175
return false;
156176
}
157177

158-
for (int i = 0; i < left.size(); i++) {
159-
MapPropertySource leftPropertySource = left.get(i);
160-
MapPropertySource rightPropertySource = right.get(i);
161-
if (changed(leftPropertySource, rightPropertySource)) {
162-
LOG.debug(() -> "found change in : " + leftPropertySource);
178+
for (int i = 0; i < k8sSources.size(); i++) {
179+
MapPropertySource k8sSource = k8sSources.get(i);
180+
MapPropertySource appSource = appSources.get(i);
181+
if (changed(k8sSource, appSource)) {
182+
LOG.debug(() -> "found change in : " + k8sSource);
163183
return true;
164184
}
165185
}
@@ -169,20 +189,20 @@ static boolean changed(List<? extends MapPropertySource> left, List<? extends Ma
169189

170190
/**
171191
* Determines if two property sources are different.
172-
* @param left left map property sources
173-
* @param right right map property sources
192+
* @param k8sSource left map property sources
193+
* @param appSource right map property sources
174194
* @return {@code true} if source has changed
175195
*/
176-
static boolean changed(MapPropertySource left, MapPropertySource right) {
177-
if (left == right) {
196+
static boolean changed(MapPropertySource k8sSource, MapPropertySource appSource) {
197+
if (k8sSource == appSource) {
178198
return false;
179199
}
180-
if (left == null || right == null) {
200+
if (k8sSource == null || appSource == null) {
181201
return true;
182202
}
183-
Map<String, Object> leftMap = left.getSource();
184-
Map<String, Object> rightMap = right.getSource();
185-
return !Objects.equals(leftMap, rightMap);
203+
Map<String, Object> k8sMap = k8sSource.getSource();
204+
Map<String, Object> appMap = appSource.getSource();
205+
return !Objects.equals(k8sMap, appMap);
186206
}
187207

188208
}

0 commit comments

Comments
 (0)