From 86a606e5f986ce0d54ac55d0bf7d05345964d347 Mon Sep 17 00:00:00 2001 From: Steven Hawkins Date: Tue, 23 Feb 2021 07:29:30 -0500 Subject: [PATCH] providing a full set of information on what config is used --- .../AbstractKubernetesConfigSourceUtil.java | 10 +++++++++- .../KubernetesConfigSourceProvider.java | 4 ++-- .../ConfigMapConfigSourceUtilTest.java | 19 +++++++++++-------- .../runtime/SecretConfigSourceUtilTest.java | 19 +++++++++++-------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/extensions/kubernetes-config/runtime/src/main/java/io/quarkus/kubernetes/client/runtime/AbstractKubernetesConfigSourceUtil.java b/extensions/kubernetes-config/runtime/src/main/java/io/quarkus/kubernetes/client/runtime/AbstractKubernetesConfigSourceUtil.java index 3cfaf120a82d4..6f7632b81694e 100644 --- a/extensions/kubernetes-config/runtime/src/main/java/io/quarkus/kubernetes/client/runtime/AbstractKubernetesConfigSourceUtil.java +++ b/extensions/kubernetes-config/runtime/src/main/java/io/quarkus/kubernetes/client/runtime/AbstractKubernetesConfigSourceUtil.java @@ -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); @@ -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 toConfigSources(String kubernetesConfigSourceName, Map kubernetesConfigSourceDataMap, + List toConfigSources(ObjectMeta metadata, Map 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"); diff --git a/extensions/kubernetes-config/runtime/src/main/java/io/quarkus/kubernetes/client/runtime/KubernetesConfigSourceProvider.java b/extensions/kubernetes-config/runtime/src/main/java/io/quarkus/kubernetes/client/runtime/KubernetesConfigSourceProvider.java index 588860a2fc92f..582218d5db8e5 100644 --- a/extensions/kubernetes-config/runtime/src/main/java/io/quarkus/kubernetes/client/runtime/KubernetesConfigSourceProvider.java +++ b/extensions/kubernetes-config/runtime/src/main/java/io/quarkus/kubernetes/client/runtime/KubernetesConfigSourceProvider.java @@ -72,7 +72,7 @@ private List getConfigMapConfigSources(List 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()); @@ -107,7 +107,7 @@ private List getSecretConfigSources(List 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()); } diff --git a/extensions/kubernetes-config/runtime/src/test/java/io/quarkus/kubernetes/client/runtime/ConfigMapConfigSourceUtilTest.java b/extensions/kubernetes-config/runtime/src/test/java/io/quarkus/kubernetes/client/runtime/ConfigMapConfigSourceUtilTest.java index 27e408143a9a1..200e05a0da675 100644 --- a/extensions/kubernetes-config/runtime/src/test/java/io/quarkus/kubernetes/client/runtime/ConfigMapConfigSourceUtilTest.java +++ b/extensions/kubernetes-config/runtime/src/test/java/io/quarkus/kubernetes/client/runtime/ConfigMapConfigSourceUtilTest.java @@ -19,7 +19,7 @@ class ConfigMapConfigSourceUtilTest { void testEmptyData() { ConfigMap configMap = configMapBuilder("testEmptyData").build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0); assertThat(configSources).isEmpty(); } @@ -29,12 +29,14 @@ void testOnlyLiteralData() { ConfigMap configMap = configMapBuilder("testOnlyLiteralData") .addToData("some.key", "someValue").addToData("some.other", "someOtherValue").build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List 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]"); }); } @@ -43,7 +45,7 @@ void testOnlySingleMatchingPropertiesData() { ConfigMap configMap = configMapBuilder("testOnlySingleMatchingPropertiesData") .addToData("application.properties", "key1=value1\nkey2=value2\nsome.key=someValue").build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0); assertThat(configSources).singleElement().satisfies(c -> { assertThat(c.getProperties()).containsOnly(entry("key1", "value1"), entry("key2", "value2"), @@ -58,7 +60,7 @@ void testOnlySingleNonMatchingPropertiesData() { ConfigMap configMap = configMapBuilder("testOnlySingleMatchingPropertiesData") .addToData("app.properties", "key1=value1\nkey2=value2\nsome.key=someValue").build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0); assertThat(configSources).isNotEmpty(); } @@ -68,7 +70,7 @@ void testOnlySingleMatchingYamlData() { ConfigMap configMap = configMapBuilder("testOnlySingleMatchingYamlData") .addToData("application.yaml", "key1: value1\nkey2: value2\nsome:\n key: someValue").build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0); assertThat(configSources).singleElement().satisfies(c -> { assertThat(c.getProperties()).containsOnly(entry("key1", "value1"), entry("key2", "value2"), @@ -82,7 +84,7 @@ void testOnlySingleNonMatchingYamlData() { ConfigMap configMap = configMapBuilder("testOnlySingleMatchingPropertiesData") .addToData("app.yaml", "key1: value1\nkey2: value2\nsome:\n key: someValue").build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0); assertThat(configSources).isNotEmpty(); } @@ -99,7 +101,7 @@ void testWithAllKindsOfData() { .addToData("app.yml", "ignored3: ignoredValue3") .build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0); assertThat(configSources).hasSize(4); @@ -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(); } } diff --git a/extensions/kubernetes-config/runtime/src/test/java/io/quarkus/kubernetes/client/runtime/SecretConfigSourceUtilTest.java b/extensions/kubernetes-config/runtime/src/test/java/io/quarkus/kubernetes/client/runtime/SecretConfigSourceUtilTest.java index 2730f4cb62dab..0e1988063b660 100644 --- a/extensions/kubernetes-config/runtime/src/test/java/io/quarkus/kubernetes/client/runtime/SecretConfigSourceUtilTest.java +++ b/extensions/kubernetes-config/runtime/src/test/java/io/quarkus/kubernetes/client/runtime/SecretConfigSourceUtilTest.java @@ -20,7 +20,7 @@ class SecretConfigSourceUtilTest { void testEmptyData() { Secret secret = secretMapBuilder("testEmptyData").build(); - List configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0); + List configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0); assertThat(configSources).isEmpty(); } @@ -30,13 +30,15 @@ void testOnlyLiteralData() { Secret configMap = secretMapBuilder("testOnlyLiteralData") .addToData("some.key", encodeValue("someValue")).addToData("some.other", encodeValue("someOtherValue")).build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List 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]"); }); } @@ -45,7 +47,7 @@ void testOnlySingleMatchingPropertiesData() { Secret secret = secretMapBuilder("testOnlySingleMatchingPropertiesData") .addToData("application.properties", encodeValue("key1=value1\nkey2=value2\nsome.key=someValue")).build(); - List configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0); + List configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0); assertThat(configSources).singleElement().satisfies(c -> { assertThat(c.getProperties()).containsOnly(entry("key1", "value1"), entry("key2", "value2"), @@ -59,7 +61,7 @@ void testOnlySingleNonMatchingPropertiesData() { Secret secret = secretMapBuilder("testOnlySingleMatchingPropertiesData") .addToData("app.properties", encodeValue("key1=value1\nkey2=value2\nsome.key=someValue")).build(); - List configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0); + List configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0); assertThat(configSources).isNotEmpty(); } @@ -69,7 +71,7 @@ void testOnlySingleMatchingYamlData() { Secret configMap = secretMapBuilder("testOnlySingleMatchingYamlData") .addToData("application.yaml", encodeValue("key1: value1\nkey2: value2\nsome:\n key: someValue")).build(); - List configSources = sut.toConfigSources(configMap.getMetadata().getName(), configMap.getData(), 0); + List configSources = sut.toConfigSources(configMap.getMetadata(), configMap.getData(), 0); assertThat(configSources).singleElement().satisfies(c -> { assertThat(c.getProperties()).containsOnly(entry("key1", "value1"), entry("key2", "value2"), @@ -83,7 +85,7 @@ void testOnlySingleNonMatchingYamlData() { Secret secret = secretMapBuilder("testOnlySingleMatchingPropertiesData") .addToData("app.yaml", encodeValue("key1: value1\nkey2: value2\nsome:\n key: someValue")).build(); - List configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0); + List configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0); assertThat(configSources).isNotEmpty(); } @@ -100,7 +102,7 @@ void testWithAllKindsOfData() { .addToData("app.yml", encodeValue("ignored3: ignoredValue3")) .build(); - List configSources = sut.toConfigSources(secret.getMetadata().getName(), secret.getData(), 0); + List configSources = sut.toConfigSources(secret.getMetadata(), secret.getData(), 0); assertThat(configSources).hasSize(4); assertThat(configSources.get(0).getClass().getName().contains("SecretLiteralDataPropertiesConfigSource")).isTrue(); @@ -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) {