Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public PropertySource<?> locate(Environment environment) {
}

@Override
protected MapPropertySource getMapPropertySource(NormalizedSource source, ConfigurableEnvironment environment,
protected MapPropertySource getPropertySource(ConfigurableEnvironment environment, NormalizedSource source,
ReadType readType) {

String normalizedNamespace = source.namespace().orElse(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2013-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.apache.commons.logging.LogFactory;

import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.log.LogAccessor;

/**
* @author wind57
*/
abstract class CommonPropertySourceLocator implements PropertySourceLocator {
Copy link
Contributor Author

@wind57 wind57 Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do the same code in SecretsPropertySourceLocator::locate and ConfigMapPropertySourceLocator::locate. As such, I created a common class that these two extend.


private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(CommonPropertySourceLocator.class));

protected final SourceConfigProperties properties;

private final SourceType sourceType;

CommonPropertySourceLocator(SourceConfigProperties properties, SourceType sourceType) {
this.properties = properties;
this.sourceType = sourceType;
}

protected abstract MapPropertySource getPropertySource(ConfigurableEnvironment environment,
NormalizedSource normalizedSource, ReadType readType);

@Override
public PropertySource<?> locate(Environment environment) {

if (environment instanceof ConfigurableEnvironment env) {

List<NormalizedSource> sources = properties.determineSources(sourceType, environment);
Set<NormalizedSource> uniqueSources = new LinkedHashSet<>(sources);
LOG.debug(sourceType.name() + " normalized sources : " + uniqueSources);
CompositePropertySource composite = new CompositePropertySource(
"composite-" + sourceType.name().toLowerCase(Locale.ROOT));

uniqueSources.forEach(secretSource -> {
MapPropertySource propertySource = getPropertySource(env, secretSource, properties.readType());

if ("true".equals(propertySource.getProperty(Constants.ERROR_PROPERTY))) {
LOG.warn(() -> "Failed to load source: " + secretSource);
}
else {
LOG.debug("Adding " + sourceType.name().toLowerCase(Locale.ROOT) + " property source "
+ propertySource.getName());
composite.addFirstPropertySource(propertySource);
}
});

return composite;
}
return null;
}

@Override
public Collection<PropertySource<?>> locateCollection(Environment environment) {
return PropertySourceLocator.super.locateCollection(environment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public ConfigDataRetryableConfigMapPropertySourceLocator(
}

@Override
protected MapPropertySource getMapPropertySource(NormalizedSource normalizedSource,
ConfigurableEnvironment environment, ReadType readType) {
return configMapPropertySourceLocator.getMapPropertySource(normalizedSource, environment, readType);
protected MapPropertySource getPropertySource(ConfigurableEnvironment environment,
NormalizedSource normalizedSource, ReadType readType) {
return configMapPropertySourceLocator.getPropertySource(environment, normalizedSource, readType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.retry.support.RetryTemplate;

Expand Down Expand Up @@ -56,7 +57,7 @@ public Collection<PropertySource<?>> locateCollection(Environment environment) {
}

@Override
protected SecretsPropertySource getPropertySource(ConfigurableEnvironment environment,
protected MapPropertySource getPropertySource(ConfigurableEnvironment environment,
NormalizedSource normalizedSource, ReadType readType) {
return this.secretsPropertySourceLocator.getPropertySource(environment, normalizedSource, readType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,17 @@

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

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

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

import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

/**
* A {@link PropertySourceLocator} that uses config maps.
* A PropertySourceLocator that uses config maps.
*
* @author Ioannis Canellos
* @author Michael Moudatsos
* @author Isik Erhan
*/
public abstract class ConfigMapPropertySourceLocator implements PropertySourceLocator {

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

protected final ConfigMapConfigProperties properties;

public ConfigMapPropertySourceLocator(ConfigMapConfigProperties properties) {
this.properties = properties;
}

protected abstract MapPropertySource getMapPropertySource(NormalizedSource normalizedSource,
ConfigurableEnvironment environment, ReadType readType);

@Override
public PropertySource<?> locate(Environment environment) {
if (environment instanceof ConfigurableEnvironment env) {

CompositePropertySource composite = new CompositePropertySource("composite-configmap");
Set<NormalizedSource> sources = new LinkedHashSet<>(this.properties.determineSources(true, environment));
LOG.debug("Config Map normalized sources : " + sources);
sources.forEach(configMapSource -> {
MapPropertySource propertySource = getMapPropertySource(configMapSource, env, properties.readType());
if ("true".equals(propertySource.getProperty(Constants.ERROR_PROPERTY))) {
LOG.warn("Failed to load source: " + configMapSource);
}
else {
LOG.debug("Adding config map property source " + propertySource.getName());
composite.addFirstPropertySource(propertySource);
}
});

return composite;
}
return null;
}
public abstract class ConfigMapPropertySourceLocator extends CommonPropertySourceLocator {

@Override
public Collection<PropertySource<?>> locateCollection(Environment environment) {
return PropertySourceLocator.super.locateCollection(environment);
public ConfigMapPropertySourceLocator(ConfigMapConfigProperties configMapConfigProperties) {
super(configMapConfigProperties, SourceType.CONFIGMAP);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,7 @@

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

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

/**
* Kubernetes {@link PropertySourceLocator} for secrets.
Expand All @@ -39,48 +26,10 @@
* @author wind57
* @author Isik Erhan
*/
public abstract class SecretsPropertySourceLocator implements PropertySourceLocator {

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

protected final SecretsConfigProperties properties;
public abstract class SecretsPropertySourceLocator extends CommonPropertySourceLocator {

public SecretsPropertySourceLocator(SecretsConfigProperties properties) {
this.properties = properties;
}

protected abstract SecretsPropertySource getPropertySource(ConfigurableEnvironment environment,
NormalizedSource normalizedSource, ReadType readType);

@Override
public PropertySource<?> locate(Environment environment) {
if (environment instanceof ConfigurableEnvironment env) {

List<NormalizedSource> sources = this.properties.determineSources(false, environment);
Set<NormalizedSource> uniqueSources = new HashSet<>(sources);
LOG.debug("Secrets normalized sources : " + sources);
CompositePropertySource composite = new CompositePropertySource("composite-secrets");

uniqueSources.forEach(secretSource -> {
MapPropertySource propertySource = getPropertySource(env, secretSource, properties.readType());

if ("true".equals(propertySource.getProperty(Constants.ERROR_PROPERTY))) {
LOG.warn("Failed to load source: " + secretSource);
}
else {
LOG.debug("Adding secret property source " + propertySource.getName());
composite.addFirstPropertySource(propertySource);
}
});

return composite;
}
return null;
}

@Override
public Collection<PropertySource<?>> locateCollection(Environment environment) {
return PropertySourceLocator.super.locateCollection(environment);
super(properties, SourceType.SECRET);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,12 @@ public ReadType readType() {
return readType;
}

protected final List<NormalizedSource> determineSources(boolean configMap, Environment environment) {
protected final List<NormalizedSource> determineSources(SourceType sourceType, Environment environment) {
if (sources().isEmpty()) {
List<NormalizedSource> result = new ArrayList<>(2);
String configurationTarget = configMap ? "ConfigMap" : "Secret";
String name = getApplicationName(environment, name(), configurationTarget);
String name = getApplicationName(environment, name(), sourceType.name());
NormalizedSource normalizedSource;
if (configMap) {
if (sourceType == SourceType.CONFIGMAP) {
normalizedSource = new NamedConfigMapNormalizedSource(name, namespace(), failFast(),
includeProfileSpecificSources());
}
Expand All @@ -128,7 +127,7 @@ protected final List<NormalizedSource> determineSources(boolean configMap, Envir

if (!labels().isEmpty()) {
NormalizedSource labeledSource;
if (configMap) {
if (sourceType == SourceType.CONFIGMAP) {
labeledSource = new LabeledConfigMapNormalizedSource(namespace(), labels(), failFast(),
ConfigUtils.Prefix.DEFAULT, false);
}
Expand All @@ -142,7 +141,7 @@ protected final List<NormalizedSource> determineSources(boolean configMap, Envir
}

return sources().stream()
.flatMap(s -> s.normalize(configMap, name(), namespace(), labels(), includeProfileSpecificSources(),
.flatMap(s -> s.normalize(sourceType, name(), namespace(), labels(), includeProfileSpecificSources(),
failFast(), useNameAsPrefix(), environment))
.toList();
}
Expand All @@ -161,7 +160,7 @@ protected final List<NormalizedSource> determineSources(boolean configMap, Envir
public record Source(String name, String namespace, @DefaultValue Map<String, String> labels, String explicitPrefix,
Boolean useNameAsPrefix, Boolean includeProfileSpecificSources) {

Stream<NormalizedSource> normalize(boolean configMap, String defaultName, String defaultNamespace,
Stream<NormalizedSource> normalize(SourceType sourceType, String defaultName, String defaultNamespace,
Map<String, String> defaultLabels, boolean defaultIncludeProfileSpecificSources, boolean failFast,
boolean defaultUseNameAsPrefix, Environment environment) {

Expand All @@ -171,16 +170,15 @@ Stream<NormalizedSource> normalize(boolean configMap, String defaultName, String
String normalizedNamespace = hasLength(namespace) ? namespace : defaultNamespace;
Map<String, String> normalizedLabels = labels.isEmpty() ? defaultLabels : labels;

String configurationTarget = configMap ? "ConfigMap" : "Secret";
String sourceName = getApplicationName(environment, normalizedName, configurationTarget);
String sourceName = getApplicationName(environment, normalizedName, sourceType.name());

Prefix prefix = findPrefix(explicitPrefix, useNameAsPrefix, defaultUseNameAsPrefix, normalizedName);

boolean includeProfileSpecificSources = ConfigUtils.includeProfileSpecificSources(
defaultIncludeProfileSpecificSources, this.includeProfileSpecificSources);

NormalizedSource namedSource;
if (configMap) {
if (sourceType == SourceType.CONFIGMAP) {
namedSource = new NamedConfigMapNormalizedSource(sourceName, normalizedNamespace, failFast, prefix,
includeProfileSpecificSources);
}
Expand All @@ -192,7 +190,7 @@ Stream<NormalizedSource> normalize(boolean configMap, String defaultName, String

if (!normalizedLabels.isEmpty()) {
NormalizedSource labeledSource;
if (configMap) {
if (sourceType == SourceType.CONFIGMAP) {
labeledSource = new LabeledConfigMapNormalizedSource(normalizedNamespace, labels, failFast, prefix,
includeProfileSpecificSources);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2013-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

public enum SourceType {

/**
* ConfigMap as the source type.
*/
CONFIGMAP,

/**
* Secret as the source type.
*/
SECRET

}
Loading
Loading