Skip to content

Commit

Permalink
Merge pull request #15252 from shawkins/master
Browse files Browse the repository at this point in the history
Provide unique information in secret/configmap ConfigSource
  • Loading branch information
geoand authored Feb 23, 2021
2 parents 24d70ee + 86a606e commit af867d5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.jboss.logging.Logger;

import io.fabric8.kubernetes.api.model.ObjectMeta;

abstract class AbstractKubernetesConfigSourceUtil {

private static final Logger log = Logger.getLogger(AbstractKubernetesConfigSourceUtil.class);
Expand Down Expand Up @@ -36,8 +38,14 @@ abstract ConfigSource createPropertiesConfigSource(String kubernetesConfigSource
* All the {@code ConfigSource} objects use the same ordinal which is higher than the ordinal
* of normal configuration files, but lower than that of environment variables
*/
List<ConfigSource> toConfigSources(String kubernetesConfigSourceName, Map<String, String> kubernetesConfigSourceDataMap,
List<ConfigSource> toConfigSources(ObjectMeta metadata, Map<String, String> kubernetesConfigSourceDataMap,
int ordinalOffset) {
/*
* use a name that uniquely identifies the secret/configmap - which can be used an application
* to fully report its startup state or even respond to secret/configmap changes
*/
String kubernetesConfigSourceName = metadata.getNamespace() + "/" + metadata.getName() + "/" + metadata.getUid() + "/"
+ metadata.getResourceVersion();
if (log.isDebugEnabled()) {
log.debug("Attempting to convert data in " + getType() + " '" + kubernetesConfigSourceName
+ "' to a list of ConfigSource objects");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private List<ConfigSource> getConfigMapConfigSources(List<String> configMapNames
logMissingOrFail(configMapName, namespace, "ConfigMap", config.failOnMissingConfig);
} else {
result.addAll(
configMapConfigSourceUtil.toConfigSources(configMap.getMetadata().getName(), configMap.getData(),
configMapConfigSourceUtil.toConfigSources(configMap.getMetadata(), configMap.getData(),
i));
if (log.isDebugEnabled()) {
log.debug("Done reading ConfigMap " + configMap.getMetadata().getName());
Expand Down Expand Up @@ -107,7 +107,7 @@ private List<ConfigSource> getSecretConfigSources(List<String> secretNames) {
if (secret == null) {
logMissingOrFail(secretName, namespace, "Secret", config.failOnMissingConfig);
} else {
result.addAll(secretConfigSourceUtil.toConfigSources(secret.getMetadata().getName(), secret.getData(), i));
result.addAll(secretConfigSourceUtil.toConfigSources(secret.getMetadata(), secret.getData(), i));
if (log.isDebugEnabled()) {
log.debug("Done reading Secret " + secret.getMetadata().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ConfigMapConfigSourceUtilTest {
void testEmptyData() {
ConfigMap configMap = configMapBuilder("testEmptyData").build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).isEmpty();
}
Expand All @@ -29,12 +29,14 @@ void testOnlyLiteralData() {
ConfigMap configMap = configMapBuilder("testOnlyLiteralData")
.addToData("some.key", "someValue").addToData("some.other", "someOtherValue").build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).singleElement().satisfies(c -> {
assertThat(c.getProperties()).containsOnly(entry("some.key", "someValue"),
entry("some.other", "someOtherValue"));
assertThat(c.getName()).contains("testOnlyLiteralData");
assertThat(c.getName()).isEqualTo(
"ConfigMapLiteralDataPropertiesConfigSource[configMap=namespace/testOnlyLiteralData/uid/version]");
});
}

Expand All @@ -43,7 +45,7 @@ void testOnlySingleMatchingPropertiesData() {
ConfigMap configMap = configMapBuilder("testOnlySingleMatchingPropertiesData")
.addToData("application.properties", "key1=value1\nkey2=value2\nsome.key=someValue").build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).singleElement().satisfies(c -> {
assertThat(c.getProperties()).containsOnly(entry("key1", "value1"), entry("key2", "value2"),
Expand All @@ -58,7 +60,7 @@ void testOnlySingleNonMatchingPropertiesData() {
ConfigMap configMap = configMapBuilder("testOnlySingleMatchingPropertiesData")
.addToData("app.properties", "key1=value1\nkey2=value2\nsome.key=someValue").build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).isNotEmpty();
}
Expand All @@ -68,7 +70,7 @@ void testOnlySingleMatchingYamlData() {
ConfigMap configMap = configMapBuilder("testOnlySingleMatchingYamlData")
.addToData("application.yaml", "key1: value1\nkey2: value2\nsome:\n key: someValue").build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).singleElement().satisfies(c -> {
assertThat(c.getProperties()).containsOnly(entry("key1", "value1"), entry("key2", "value2"),
Expand All @@ -82,7 +84,7 @@ void testOnlySingleNonMatchingYamlData() {
ConfigMap configMap = configMapBuilder("testOnlySingleMatchingPropertiesData")
.addToData("app.yaml", "key1: value1\nkey2: value2\nsome:\n key: someValue").build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).isNotEmpty();
}
Expand All @@ -99,7 +101,7 @@ void testWithAllKindsOfData() {
.addToData("app.yml", "ignored3: ignoredValue3")
.build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).hasSize(4);

Expand Down Expand Up @@ -131,7 +133,8 @@ void testWithAllKindsOfData() {

private ConfigMapBuilder configMapBuilder(String name) {
return new ConfigMapBuilder().withNewMetadata()
.withName(name).endMetadata();
.withName(name).withNamespace("namespace").withUid("uid")
.withResourceVersion("version").endMetadata();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SecretConfigSourceUtilTest {
void testEmptyData() {
Secret secret = secretMapBuilder("testEmptyData").build();

List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0);

assertThat(configSources).isEmpty();
}
Expand All @@ -30,13 +30,15 @@ void testOnlyLiteralData() {
Secret configMap = secretMapBuilder("testOnlyLiteralData")
.addToData("some.key", encodeValue("someValue")).addToData("some.other", encodeValue("someOtherValue")).build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).singleElement().satisfies(c -> {
assertThat(c.getProperties()).containsOnly(entry("some.key", "someValue"),
entry("some.other", "someOtherValue"));
assertThat(c.getName()).contains("testOnlyLiteralData");
assertThat(c.getOrdinal()).isEqualTo(285);
assertThat(c.getName())
.isEqualTo("SecretLiteralDataPropertiesConfigSource[secret=namespace/testOnlyLiteralData/uid/version]");
});
}

Expand All @@ -45,7 +47,7 @@ void testOnlySingleMatchingPropertiesData() {
Secret secret = secretMapBuilder("testOnlySingleMatchingPropertiesData")
.addToData("application.properties", encodeValue("key1=value1\nkey2=value2\nsome.key=someValue")).build();

List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0);

assertThat(configSources).singleElement().satisfies(c -> {
assertThat(c.getProperties()).containsOnly(entry("key1", "value1"), entry("key2", "value2"),
Expand All @@ -59,7 +61,7 @@ void testOnlySingleNonMatchingPropertiesData() {
Secret secret = secretMapBuilder("testOnlySingleMatchingPropertiesData")
.addToData("app.properties", encodeValue("key1=value1\nkey2=value2\nsome.key=someValue")).build();

List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0);

assertThat(configSources).isNotEmpty();
}
Expand All @@ -69,7 +71,7 @@ void testOnlySingleMatchingYamlData() {
Secret configMap = secretMapBuilder("testOnlySingleMatchingYamlData")
.addToData("application.yaml", encodeValue("key1: value1\nkey2: value2\nsome:\n key: someValue")).build();

List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0);

assertThat(configSources).singleElement().satisfies(c -> {
assertThat(c.getProperties()).containsOnly(entry("key1", "value1"), entry("key2", "value2"),
Expand All @@ -83,7 +85,7 @@ void testOnlySingleNonMatchingYamlData() {
Secret secret = secretMapBuilder("testOnlySingleMatchingPropertiesData")
.addToData("app.yaml", encodeValue("key1: value1\nkey2: value2\nsome:\n key: someValue")).build();

List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0);

assertThat(configSources).isNotEmpty();
}
Expand All @@ -100,7 +102,7 @@ void testWithAllKindsOfData() {
.addToData("app.yml", encodeValue("ignored3: ignoredValue3"))
.build();

List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0);
List<ConfigSource> configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0);

assertThat(configSources).hasSize(4);
assertThat(configSources.get(0).getClass().getName().contains("SecretLiteralDataPropertiesConfigSource")).isTrue();
Expand Down Expand Up @@ -132,7 +134,8 @@ void testWithAllKindsOfData() {

private SecretBuilder secretMapBuilder(String name) {
return new SecretBuilder().withNewMetadata()
.withName(name).endMetadata();
.withName(name).withNamespace("namespace").withUid("uid")
.withResourceVersion("version").endMetadata();
}

private String encodeValue(String value) {
Expand Down

0 comments on commit af867d5

Please sign in to comment.