Skip to content

Commit cd715b9

Browse files
committed
review comments
1 parent 372e228 commit cd715b9

File tree

6 files changed

+19
-18
lines changed

6 files changed

+19
-18
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public final class Constants {
6363
*/
6464
public static final String RELOAD_MODE = "spring.cloud.kubernetes.reload.mode";
6565

66+
/**
67+
* property set to true when there was an error reading config maps or secrets, when
68+
* generating a property source.
69+
*/
70+
public static final String ERROR_PROPERTY = "spring.cloud.k8s.error.reading.property.source";
71+
6672
private Constants() {
6773
}
6874

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import org.apache.commons.logging.LogFactory;
2626

2727
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException;
28+
import static org.springframework.cloud.kubernetes.commons.config.Constants.ERROR_PROPERTY;
2829
import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR;
29-
import static org.springframework.cloud.kubernetes.commons.config.SourceData.EMPTY_SOURCE_NAME_ON_ERROR;
3030

3131
/**
3232
* @author wind57
@@ -41,7 +41,7 @@ public abstract class LabeledSourceData {
4141
public final SourceData compute(Map<String, String> labels, ConfigUtils.Prefix prefix, String target,
4242
boolean profileSources, boolean failFast, String namespace, String[] activeProfiles) {
4343

44-
MultipleSourcesContainer data;
44+
MultipleSourcesContainer data = MultipleSourcesContainer.empty();
4545

4646
try {
4747
Set<String> profiles = Set.of();
@@ -81,7 +81,7 @@ public final SourceData compute(Map<String, String> labels, ConfigUtils.Prefix p
8181
catch (Exception e) {
8282
LOG.warn("failure in reading labeled sources");
8383
onException(failFast, e);
84-
return SourceData.emptyRecord(EMPTY_SOURCE_NAME_ON_ERROR);
84+
data = new MultipleSourcesContainer(data.names(), Map.of(ERROR_PROPERTY, "true"));
8585
}
8686

8787
String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
package org.springframework.cloud.kubernetes.commons.config;
1818

1919
import java.util.LinkedHashSet;
20+
import java.util.Map;
2021
import java.util.stream.Collectors;
2122

2223
import org.apache.commons.logging.Log;
2324
import org.apache.commons.logging.LogFactory;
2425

2526
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException;
27+
import static org.springframework.cloud.kubernetes.commons.config.Constants.ERROR_PROPERTY;
2628
import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR;
27-
import static org.springframework.cloud.kubernetes.commons.config.SourceData.EMPTY_SOURCE_NAME_ON_ERROR;
2829

2930
/**
3031
* @author wind57
@@ -43,7 +44,7 @@ public final SourceData compute(String sourceName, ConfigUtils.Prefix prefix, St
4344
// first comes non-profile based source
4445
sourceNames.add(sourceName);
4546

46-
MultipleSourcesContainer data;
47+
MultipleSourcesContainer data = MultipleSourcesContainer.empty();
4748

4849
try {
4950
if (profileSources) {
@@ -72,7 +73,7 @@ public final SourceData compute(String sourceName, ConfigUtils.Prefix prefix, St
7273
catch (Exception e) {
7374
LOG.warn("failure in reading named sources");
7475
onException(failFast, e);
75-
return SourceData.emptyRecord(EMPTY_SOURCE_NAME_ON_ERROR);
76+
data = new MultipleSourcesContainer(data.names(), Map.of(ERROR_PROPERTY, "true"));
7677
}
7778

7879
String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@
2626
*/
2727
public record SourceData(String sourceName, Map<String, Object> sourceData) {
2828

29-
/**
30-
* source name that is generated when there is an error reading the underlying
31-
* configmap(s) or secret(s).
32-
*/
33-
public static final String EMPTY_SOURCE_NAME_ON_ERROR = "source-generate-on-error";
34-
3529
public static SourceData emptyRecord(String sourceName) {
3630
return new SourceData(sourceName, Map.of());
3731
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
2828
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
2929
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
30-
import org.springframework.cloud.kubernetes.commons.config.SourceData;
3130
import org.springframework.core.env.CompositePropertySource;
3231
import org.springframework.core.env.ConfigurableEnvironment;
3332
import org.springframework.core.env.MapPropertySource;
3433
import org.springframework.core.env.PropertySource;
3534
import org.springframework.core.log.LogAccessor;
3635

36+
import static org.springframework.cloud.kubernetes.commons.config.Constants.ERROR_PROPERTY;
37+
3738
/**
3839
* @author wind57
3940
*/
@@ -164,7 +165,7 @@ else if (propertySource instanceof CompositePropertySource source) {
164165

165166
static boolean changed(List<? extends MapPropertySource> k8sSources, List<? extends MapPropertySource> appSources) {
166167

167-
if (k8sSources.stream().anyMatch(source -> source.getName().equals(SourceData.EMPTY_SOURCE_NAME_ON_ERROR))) {
168+
if (k8sSources.stream().anyMatch(source -> "true".equals(source.getProperty(ERROR_PROPERTY)))) {
168169
LOG.info(() -> "there was an error while reading config maps/secrets, no reload will happen");
169170
return false;
170171
}

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

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

2828
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
29+
import org.springframework.cloud.kubernetes.commons.config.Constants;
2930
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
30-
import org.springframework.cloud.kubernetes.commons.config.SourceData;
3131
import org.springframework.core.env.CompositePropertySource;
3232
import org.springframework.core.env.EnumerablePropertySource;
3333
import org.springframework.core.env.MapPropertySource;
@@ -154,9 +154,8 @@ public Object getProperty(String name) {
154154
@Test
155155
void testEmptySourceNameOnError() {
156156
Object value = new Object();
157-
Map<String, Object> rightMap = new HashMap<>();
158-
rightMap.put("key", value);
159-
MapPropertySource left = new MapPropertySource(SourceData.EMPTY_SOURCE_NAME_ON_ERROR, Map.of());
157+
Map<String, Object> rightMap = Map.of("key", value);
158+
MapPropertySource left = new MapPropertySource("on-error", Map.of(Constants.ERROR_PROPERTY, "true"));
160159
MapPropertySource right = new MapPropertySource("right", rightMap);
161160
boolean changed = ConfigReloadUtil.changed(List.of(left), List.of(right));
162161
assertThat(changed).isFalse();

0 commit comments

Comments
 (0)