-
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
46ebbf0
commit f736200
Showing
14 changed files
with
324 additions
and
11 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
167 changes: 167 additions & 0 deletions
167
...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,167 @@ | ||
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.getName()).isEqualTo("cm1"); | ||
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
1 change: 1 addition & 0 deletions
1
integration-tests/kubernetes-client/src/main/resources/application.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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
quarkus.kubernetes-config.enabled=true | ||
quarkus.kubernetes-config.config-maps=cmap1,cmap2 | ||
quarkus.kubernetes-config.secrets=s1 | ||
quarkus.kubernetes-config.namespace=test |
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
Oops, something went wrong.