-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: reading namespaced ConfigMaps
- Loading branch information
1 parent
754eced
commit 1c89c51
Showing
12 changed files
with
306 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
...rc/test/java/io/quarkus/kubernetes/client/runtime/KubernetesConfigSourceProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package io.quarkus.kubernetes.client.runtime; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.assertj.core.util.Lists; | ||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.fabric8.kubernetes.api.model.ConfigMapList; | ||
import io.fabric8.kubernetes.api.model.DoneableConfigMap; | ||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.dsl.MixedOperation; | ||
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; | ||
import io.fabric8.kubernetes.client.dsl.Resource; | ||
|
||
public class KubernetesConfigSourceProviderTest { | ||
|
||
@Test | ||
public void testEmptyConfigSources() { | ||
KubernetesConfigSourceConfig config = defaultConfig(); | ||
KubernetesClient kubernetesClient = Mockito.mock(KubernetesClient.class); | ||
KubernetesConfigSourceProvider kcsp = new KubernetesConfigSourceProvider(config, kubernetesClient); | ||
Iterable<ConfigSource> configSources = kcsp.getConfigSources(null); | ||
assertThat(configSources).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testRetrieveNamespacedConfigSources() { | ||
KubernetesConfigSourceConfig config = defaultConfig(); | ||
config.namespace = Optional.of("demo"); | ||
List<String> configMaps = Lists.list("cm1"); | ||
config.configMaps = Optional.of(configMaps); | ||
|
||
ConfigMap configMap = configMapBuilder("cm1") | ||
.addToData("some.key", "someValue").addToData("some.other", "someOtherValue").build(); | ||
|
||
KubernetesClient kubernetesClient = Mockito.mock(KubernetesClient.class); | ||
stubNamespacedConfigMap(kubernetesClient, configMap, "cm1"); | ||
|
||
KubernetesConfigSourceProvider kcsp = new KubernetesConfigSourceProvider(config, kubernetesClient); | ||
Iterable<ConfigSource> configSources = kcsp.getConfigSources(null); | ||
assertThat(configSources).isNotEmpty(); | ||
ConfigSource next = configSources.iterator().next(); | ||
assertThat(next.getProperties()).containsKeys("some.key", "some.other"); | ||
} | ||
|
||
@Test | ||
public void testNamespacedConfigSourcesAbsents() { | ||
KubernetesConfigSourceConfig config = defaultConfig(); | ||
config.namespace = Optional.of("demo"); | ||
List<String> configMaps = Lists.list("cm2"); | ||
config.configMaps = Optional.of(configMaps); | ||
|
||
KubernetesClient kubernetesClient = Mockito.mock(KubernetesClient.class); | ||
stubNamespacedConfigMap(kubernetesClient, null, "cm2"); | ||
|
||
KubernetesConfigSourceProvider kcsp = new KubernetesConfigSourceProvider(config, kubernetesClient); | ||
Iterable<ConfigSource> configSources = kcsp.getConfigSources(null); | ||
assertThat(configSources).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testRetrieveConfigSources() { | ||
KubernetesConfigSourceConfig config = defaultConfig(); | ||
List<String> configMaps = Lists.list("cm1"); | ||
config.configMaps = Optional.of(configMaps); | ||
|
||
ConfigMap configMap = configMapBuilder("cm1") | ||
.addToData("some.key", "someValue").addToData("some.other", "someOtherValue").build(); | ||
|
||
KubernetesClient kubernetesClient = Mockito.mock(KubernetesClient.class); | ||
stubConfigMap(kubernetesClient, configMap, "cm1"); | ||
|
||
KubernetesConfigSourceProvider kcsp = new KubernetesConfigSourceProvider(config, kubernetesClient); | ||
Iterable<ConfigSource> configSources = kcsp.getConfigSources(null); | ||
assertThat(configSources).isNotEmpty(); | ||
ConfigSource next = configSources.iterator().next(); | ||
assertThat(next.getProperties()).containsKeys("some.key", "some.other"); | ||
} | ||
|
||
@Test | ||
public void testConfigSourcesAbsent() { | ||
KubernetesConfigSourceConfig config = defaultConfig(); | ||
List<String> configMaps = Lists.list("cm2"); | ||
config.configMaps = Optional.of(configMaps); | ||
|
||
KubernetesClient kubernetesClient = Mockito.mock(KubernetesClient.class); | ||
stubConfigMap(kubernetesClient, null, "cm2"); | ||
|
||
KubernetesConfigSourceProvider kcsp = new KubernetesConfigSourceProvider(config, kubernetesClient); | ||
Iterable<ConfigSource> configSources = kcsp.getConfigSources(null); | ||
assertThat(configSources).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testConfigSourcesAbsentFailOnMissing() { | ||
try { | ||
KubernetesConfigSourceConfig config = defaultConfig(); | ||
config.namespace = Optional.of("demo"); | ||
List<String> configMaps = Lists.list("cm2"); | ||
config.configMaps = Optional.of(configMaps); | ||
config.failOnMissingConfig = true; | ||
|
||
KubernetesClient kubernetesClient = Mockito.mock(KubernetesClient.class); | ||
stubNamespacedConfigMap(kubernetesClient, null, "cm2"); | ||
|
||
KubernetesConfigSourceProvider kcsp = new KubernetesConfigSourceProvider(config, kubernetesClient); | ||
Iterable<ConfigSource> configSources = kcsp.getConfigSources(null); | ||
fail("an exception should be raised"); | ||
} catch (RuntimeException expected) { | ||
} | ||
} | ||
|
||
private void stubNamespacedConfigMap(KubernetesClient kubernetesClient, ConfigMap configMap, String configMapName) { | ||
MixedOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>> mixedOperation = (MixedOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>>) mock( | ||
MixedOperation.class); | ||
when(kubernetesClient.configMaps()).thenReturn(mixedOperation); | ||
Resource<ConfigMap, DoneableConfigMap> resource = (Resource<ConfigMap, DoneableConfigMap>) mock(Resource.class); | ||
NonNamespaceOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>> nsClient = (NonNamespaceOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>>) mock( | ||
NonNamespaceOperation.class); | ||
when(mixedOperation.inNamespace("demo")).thenReturn(nsClient); | ||
when(nsClient.withName(configMapName)).thenReturn(resource); | ||
|
||
when(resource.get()).thenReturn(configMap); | ||
Config kubernetesConfig = mock(Config.class); | ||
when(kubernetesClient.getConfiguration()).thenReturn(kubernetesConfig); | ||
when(kubernetesConfig.getMasterUrl()).thenReturn("url"); | ||
|
||
} | ||
|
||
private void stubConfigMap(KubernetesClient kubernetesClient, ConfigMap configMap, String configMapName) { | ||
MixedOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>> mixedOperation = (MixedOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>>) mock( | ||
MixedOperation.class); | ||
when(kubernetesClient.configMaps()).thenReturn(mixedOperation); | ||
Resource<ConfigMap, DoneableConfigMap> resource = (Resource<ConfigMap, DoneableConfigMap>) mock(Resource.class); | ||
when(mixedOperation.withName(configMapName)).thenReturn(resource); | ||
when(resource.get()).thenReturn(configMap); | ||
Config kubernetesConfig = mock(Config.class); | ||
when(kubernetesClient.getConfiguration()).thenReturn(kubernetesConfig); | ||
when(kubernetesConfig.getMasterUrl()).thenReturn("url"); | ||
|
||
} | ||
|
||
private KubernetesConfigSourceConfig defaultConfig() { | ||
KubernetesConfigSourceConfig config = new KubernetesConfigSourceConfig(); | ||
config.namespace = Optional.empty(); | ||
config.configMaps = Optional.empty(); | ||
return config; | ||
} | ||
|
||
private ConfigMapBuilder configMapBuilder(String name) { | ||
return new ConfigMapBuilder().withNewMetadata() | ||
.withName(name).endMetadata(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...s-client/src/test/java/io/quarkus/it/kubernetes/client/AbsentConfigMapPropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.it.kubernetes.client; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusProdModeTest; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
|
||
@QuarkusTestResource(CustomKubernetesMockServerTestResource.class) | ||
public class AbsentConfigMapPropertiesTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusProdModeTest config = new QuarkusProdModeTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(ConfigMapProperties.class)) | ||
.setApplicationName("k8s-configMaps") | ||
.withConfigurationResource("application-demo.properties") | ||
.setRun(true) | ||
.setExpectExit(true) | ||
.setApplicationVersion("0.1-SNAPSHOT"); | ||
|
||
@Test | ||
public void startUpShouldFail() throws IOException { | ||
Assertions.assertThat(config.getStartupConsoleOutput()) | ||
.contains("ConfigMap 'cmap4' not found in namespace 'demo'").contains("RuntimeException"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ient/src/test/java/io/quarkus/it/kubernetes/client/NamespacedConfigMapPropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.quarkus.it.kubernetes.client; | ||
|
||
import static io.quarkus.it.kubernetes.client.ConfigMapPropertiesTest.assertProperty; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
import io.quarkus.test.junit.TestProfile; | ||
|
||
@QuarkusTestResource(CustomKubernetesMockServerTestResource.class) | ||
@TestProfile(NamespacedConfigMapPropertiesTest.MyProfile.class) | ||
@QuarkusTest | ||
public class NamespacedConfigMapPropertiesTest { | ||
|
||
@Test | ||
public void testPropertiesReadFromConfigMap() { | ||
assertProperty("dummy", "dummyFromDemo"); | ||
assertProperty("someProp1", "val1FromDemo"); | ||
assertProperty("someProp2", "val2FromDemo"); | ||
assertProperty("someProp3", "val3FromDemo"); | ||
assertProperty("someProp4", "val4FromDemo"); | ||
assertProperty("someProp5", "val5FromDemo"); | ||
} | ||
|
||
public static class MyProfile implements QuarkusTestProfile { | ||
|
||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
Map<String, String> conf = new HashMap<>(); | ||
conf.put("quarkus.kubernetes-config.enabled", "true"); | ||
conf.put("quarkus.kubernetes-config.config-maps", "cmap3"); | ||
conf.put("quarkus.kubernetes-config.namespace", "demo"); | ||
conf.put("quarkus.kubernetes-config.secrets", "s1"); | ||
return conf; | ||
} | ||
|
||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
integration-tests/kubernetes-client/src/test/resources/application-demo.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
quarkus.kubernetes-config.enabled=true | ||
quarkus.kubernetes-config.config-maps=cmap4 | ||
quarkus.kubernetes-config.namespace=demo | ||
quarkus.kubernetes-config.secrets=s1 | ||
|