Skip to content

Commit

Permalink
started work
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Nov 10, 2024
1 parent 5430511 commit 804458f
Show file tree
Hide file tree
Showing 7 changed files with 608 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException;
import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR;
import static org.springframework.cloud.kubernetes.commons.config.SourceData.EMPTY_SOURCE_NAME_ON_ERROR;

/**
* @author wind57
Expand All @@ -32,10 +36,12 @@
*/
public abstract class LabeledSourceData {

private static final Log LOG = LogFactory.getLog(LabeledSourceData.class);

public final SourceData compute(Map<String, String> labels, ConfigUtils.Prefix prefix, String target,
boolean profileSources, boolean failFast, String namespace, String[] activeProfiles) {

MultipleSourcesContainer data = MultipleSourcesContainer.empty();
MultipleSourcesContainer data;

try {
Set<String> profiles = Set.of();
Expand Down Expand Up @@ -73,7 +79,9 @@ public final SourceData compute(Map<String, String> labels, ConfigUtils.Prefix p
}
}
catch (Exception e) {
LOG.warn("failure in reading labeled sources");
onException(failFast, e);
return SourceData.emptyRecord(EMPTY_SOURCE_NAME_ON_ERROR);
}

String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException;
import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR;
import static org.springframework.cloud.kubernetes.commons.config.SourceData.EMPTY_SOURCE_NAME_ON_ERROR;

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

MultipleSourcesContainer data = MultipleSourcesContainer.empty();
MultipleSourcesContainer data;

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

}
catch (Exception e) {
LOG.warn("failure in reading named sources");
onException(failFast, e);
return SourceData.emptyRecord(EMPTY_SOURCE_NAME_ON_ERROR);
}

String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
public record SourceData(String sourceName, Map<String, Object> sourceData) {

/**
* source name that is generated when there is an error reading the underlying
* configmap(s) or secret(s).
*/
public static final String EMPTY_SOURCE_NAME_ON_ERROR = "source-generate-on-error";

public static SourceData emptyRecord(String sourceName) {
return new SourceData(sourceName, Map.of());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
import org.springframework.cloud.kubernetes.commons.config.SourceData;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
Expand Down Expand Up @@ -162,6 +163,12 @@ else if (propertySource instanceof CompositePropertySource source) {
}

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

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

if (k8sSources.size() != appSources.size()) {
if (LOG.isDebugEnabled()) {
LOG.debug("k8s property sources size: " + k8sSources.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
import org.springframework.cloud.kubernetes.commons.config.SourceData;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
Expand Down Expand Up @@ -150,6 +151,17 @@ public Object getProperty(String name) {
Assertions.assertEquals("from-inner-two-composite", result.get(3).getProperty(""));
}

@Test
void testEmptySourceNameOnError() {
Object value = new Object();
Map<String, Object> rightMap = new HashMap<>();
rightMap.put("key", value);
MapPropertySource left = new MapPropertySource(SourceData.EMPTY_SOURCE_NAME_ON_ERROR, Map.of());
MapPropertySource right = new MapPropertySource("right", rightMap);
boolean changed = ConfigReloadUtil.changed(List.of(left), List.of(right));
assertThat(changed).isFalse();
}

private static final class OneComposite extends CompositePropertySource {

private OneComposite() {
Expand Down
Loading

0 comments on commit 804458f

Please sign in to comment.