Skip to content

Commit 804458f

Browse files
committed
started work
1 parent 5430511 commit 804458f

File tree

7 files changed

+608
-2
lines changed

7 files changed

+608
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
import java.util.Set;
2222
import java.util.stream.Collectors;
2323

24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
26+
2427
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException;
2528
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;
2630

2731
/**
2832
* @author wind57
@@ -32,10 +36,12 @@
3236
*/
3337
public abstract class LabeledSourceData {
3438

39+
private static final Log LOG = LogFactory.getLog(LabeledSourceData.class);
40+
3541
public final SourceData compute(Map<String, String> labels, ConfigUtils.Prefix prefix, String target,
3642
boolean profileSources, boolean failFast, String namespace, String[] activeProfiles) {
3743

38-
MultipleSourcesContainer data = MultipleSourcesContainer.empty();
44+
MultipleSourcesContainer data;
3945

4046
try {
4147
Set<String> profiles = Set.of();
@@ -73,7 +79,9 @@ public final SourceData compute(Map<String, String> labels, ConfigUtils.Prefix p
7379
}
7480
}
7581
catch (Exception e) {
82+
LOG.warn("failure in reading labeled sources");
7683
onException(failFast, e);
84+
return SourceData.emptyRecord(EMPTY_SOURCE_NAME_ON_ERROR);
7785
}
7886

7987
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException;
2626
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;
2728

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

45-
MultipleSourcesContainer data = MultipleSourcesContainer.empty();
46+
MultipleSourcesContainer data;
4647

4748
try {
4849
if (profileSources) {
@@ -69,7 +70,9 @@ public final SourceData compute(String sourceName, ConfigUtils.Prefix prefix, St
6970

7071
}
7172
catch (Exception e) {
73+
LOG.warn("failure in reading named sources");
7274
onException(failFast, e);
75+
return SourceData.emptyRecord(EMPTY_SOURCE_NAME_ON_ERROR);
7376
}
7477

7578
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
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+
2935
public static SourceData emptyRecord(String sourceName) {
3036
return new SourceData(sourceName, Map.of());
3137
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
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;
3031
import org.springframework.core.env.CompositePropertySource;
3132
import org.springframework.core.env.ConfigurableEnvironment;
3233
import org.springframework.core.env.MapPropertySource;
@@ -162,6 +163,12 @@ else if (propertySource instanceof CompositePropertySource source) {
162163
}
163164

164165
static boolean changed(List<? extends MapPropertySource> k8sSources, List<? extends MapPropertySource> appSources) {
166+
167+
if (k8sSources.stream().anyMatch(source -> source.getName().equals(SourceData.EMPTY_SOURCE_NAME_ON_ERROR))) {
168+
LOG.info(() -> "there was an error while reading config maps/secrets, no reload will happen");
169+
return false;
170+
}
171+
165172
if (k8sSources.size() != appSources.size()) {
166173
if (LOG.isDebugEnabled()) {
167174
LOG.debug("k8s property sources size: " + k8sSources.size());

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
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.SourceData;
3031
import org.springframework.core.env.CompositePropertySource;
3132
import org.springframework.core.env.EnumerablePropertySource;
3233
import org.springframework.core.env.MapPropertySource;
@@ -150,6 +151,17 @@ public Object getProperty(String name) {
150151
Assertions.assertEquals("from-inner-two-composite", result.get(3).getProperty(""));
151152
}
152153

154+
@Test
155+
void testEmptySourceNameOnError() {
156+
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());
160+
MapPropertySource right = new MapPropertySource("right", rightMap);
161+
boolean changed = ConfigReloadUtil.changed(List.of(left), List.of(right));
162+
assertThat(changed).isFalse();
163+
}
164+
153165
private static final class OneComposite extends CompositePropertySource {
154166

155167
private OneComposite() {

0 commit comments

Comments
 (0)