Search in sources :

Example 1 with EnvFromSource

use of io.fabric8.kubernetes.api.model.EnvFromSource in project che-server by eclipse-che.

the class UniqueNamesProvisionerTest method rewritePodConfigMapEnvFrom.

@Test
public void rewritePodConfigMapEnvFrom() throws Exception {
    when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);
    ConfigMap configMap = newConfigMap();
    doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps();
    EnvFromSource envFrom = new EnvFromSourceBuilder().withNewConfigMapRef().withName(CONFIGMAP_NAME).endConfigMapRef().build();
    Container container = new ContainerBuilder().withEnvFrom(envFrom).build();
    Pod pod = newPod();
    pod.getSpec().setContainers(ImmutableList.of(container));
    PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
    doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData();
    uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity);
    String newConfigMapName = configMap.getMetadata().getName();
    EnvFromSource newEnvFromSource = container.getEnvFrom().iterator().next();
    assertEquals(newEnvFromSource.getConfigMapRef().getName(), newConfigMapName);
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Pod(io.fabric8.kubernetes.api.model.Pod) EnvFromSourceBuilder(io.fabric8.kubernetes.api.model.EnvFromSourceBuilder) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Test(org.testng.annotations.Test)

Example 2 with EnvFromSource

use of io.fabric8.kubernetes.api.model.EnvFromSource in project devspaces-images by redhat-developer.

the class UniqueNamesProvisionerTest method rewritePodConfigMapEnvFrom.

@Test
public void rewritePodConfigMapEnvFrom() throws Exception {
    when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);
    ConfigMap configMap = newConfigMap();
    doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps();
    EnvFromSource envFrom = new EnvFromSourceBuilder().withNewConfigMapRef().withName(CONFIGMAP_NAME).endConfigMapRef().build();
    Container container = new ContainerBuilder().withEnvFrom(envFrom).build();
    Pod pod = newPod();
    pod.getSpec().setContainers(ImmutableList.of(container));
    PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
    doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData();
    uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity);
    String newConfigMapName = configMap.getMetadata().getName();
    EnvFromSource newEnvFromSource = container.getEnvFrom().iterator().next();
    assertEquals(newEnvFromSource.getConfigMapRef().getName(), newConfigMapName);
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Pod(io.fabric8.kubernetes.api.model.Pod) EnvFromSourceBuilder(io.fabric8.kubernetes.api.model.EnvFromSourceBuilder) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Test(org.testng.annotations.Test)

Example 3 with EnvFromSource

use of io.fabric8.kubernetes.api.model.EnvFromSource in project quarkus by quarkusio.

the class KubernetesWithMixedStyleEnvTest method assertGeneratedResources.

@Test
public void assertGeneratedResources() throws IOException {
    Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir).isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json")).isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
    assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
        assertThat(d.getMetadata()).satisfies(m -> {
            assertThat(m.getName()).isEqualTo(APPLICATION_NAME);
        });
        assertThat(d.getSpec()).satisfies(deploymentSpec -> {
            assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
                assertThat(t.getSpec()).satisfies(podSpec -> {
                    assertThat(podSpec.getContainers()).singleElement().satisfies(container -> {
                        assertThat(container.getEnv()).filteredOn(env -> "FROMFIELD".equals(env.getName())).singleElement().satisfies(env -> assertThat(env.getValueFrom().getFieldRef().getFieldPath()).isEqualTo("metadata.name"));
                        assertThat(container.getEnv()).filteredOn(env -> "ENVVAR".equals(env.getName())).singleElement().satisfies(env -> assertThat(env.getValue()).isEqualTo("value"));
                        final List<EnvFromSource> envFrom = container.getEnvFrom();
                        assertThat(envFrom).hasSize(2);
                        assertThat(envFrom).filteredOn(e -> e.getSecretRef() != null).singleElement().satisfies(e -> assertThat(e.getSecretRef().getName()).isEqualTo("secretName"));
                        assertThat(envFrom).filteredOn(e -> e.getConfigMapRef() != null).singleElement().satisfies(e -> assertThat(e.getConfigMapRef().getName()).isEqualTo("configName"));
                    });
                });
            });
        });
    });
}
Also used : Path(java.nio.file.Path) Test(org.junit.jupiter.api.Test) List(java.util.List) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProdBuildResults(io.quarkus.test.ProdBuildResults) IOException(java.io.IOException) ProdModeTestResults(io.quarkus.test.ProdModeTestResults) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Path(java.nio.file.Path) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Test(org.junit.jupiter.api.Test) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest)

Example 4 with EnvFromSource

use of io.fabric8.kubernetes.api.model.EnvFromSource in project quarkus by quarkusio.

the class KubernetesWithNewStyleEnvTest method assertGeneratedResources.

@Test
public void assertGeneratedResources() throws IOException {
    Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir).isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json")).isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
    assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
        assertThat(d.getMetadata()).satisfies(m -> {
            assertThat(m.getName()).isEqualTo(APPLICATION_NAME);
        });
        assertThat(d.getSpec()).satisfies(deploymentSpec -> {
            assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
                assertThat(t.getSpec()).satisfies(podSpec -> {
                    assertThat(podSpec.getContainers()).singleElement().satisfies(container -> {
                        assertThat(container.getEnv()).filteredOn(env -> "FROMFIELD".equals(env.getName())).singleElement().satisfies(env -> assertThat(env.getValueFrom().getFieldRef().getFieldPath()).isEqualTo("metadata.name"));
                        assertThat(container.getEnv()).filteredOn(env -> "ENVVAR".equals(env.getName())).singleElement().satisfies(env -> assertThat(env.getValue()).isEqualTo("value"));
                        assertThat(container.getEnv()).filteredOn(env -> "QUARKUS_KUBERNETES_CONFIG_ENABLED".equals(env.getName())).singleElement().satisfies(env -> assertThat(env.getValue()).isEqualTo("true"));
                        assertThat(container.getEnv()).filteredOn(env -> "FOO_MP_REST_URL".equals(env.getName())).singleElement().satisfies(env -> assertThat(env.getValue()).isEqualTo("foo.url"));
                        final List<EnvFromSource> envFrom = container.getEnvFrom();
                        assertThat(envFrom).hasSize(2);
                        assertThat(envFrom).filteredOn(e -> e.getSecretRef() != null).singleElement().satisfies(e -> assertThat(e.getSecretRef().getName()).isEqualTo("secretName"));
                        assertThat(envFrom).filteredOn(e -> e.getConfigMapRef() != null).singleElement().satisfies(e -> assertThat(e.getConfigMapRef().getName()).isEqualTo("configName"));
                    });
                });
            });
        });
    });
}
Also used : Path(java.nio.file.Path) Test(org.junit.jupiter.api.Test) List(java.util.List) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProdBuildResults(io.quarkus.test.ProdBuildResults) IOException(java.io.IOException) ProdModeTestResults(io.quarkus.test.ProdModeTestResults) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Path(java.nio.file.Path) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Test(org.junit.jupiter.api.Test) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest)

Example 5 with EnvFromSource

use of io.fabric8.kubernetes.api.model.EnvFromSource in project stackgres by ongres.

the class DistributedLogsRequiredResourcesGeneratorTest method get095Sts.

@NotNull
private StatefulSet get095Sts(StackGresDistributedLogs distributedLogs095) {
    StatefulSet sts = JsonUtil.readFromJson("statefulset/0.9.5-distributedlogs.json", StatefulSet.class);
    final String namespace = distributedLogs095.getMetadata().getNamespace();
    sts.getMetadata().setNamespace(namespace);
    final String distributedLogsName = distributedLogs095.getMetadata().getName();
    sts.getMetadata().setName(distributedLogsName);
    final String uid = distributedLogs095.getMetadata().getUid();
    fix095Labels(sts, distributedLogs095);
    fix095Labels(sts.getSpec().getTemplate().getMetadata(), distributedLogs095);
    fix095Labels(sts.getSpec().getSelector().getMatchLabels(), distributedLogs095);
    Container patroniContainer = sts.getSpec().getTemplate().getSpec().getContainers().get(0);
    patroniContainer.getEnv().forEach(envVar -> {
        Optional.ofNullable(envVar.getValueFrom()).map(EnvVarSource::getSecretKeyRef).filter(secretKeySelector -> secretKeySelector.getName().equals("distributedlogs")).ifPresent(secretKeySelector -> secretKeySelector.setName(distributedLogsName));
        Optional.ofNullable(envVar.getValueFrom()).map(EnvVarSource::getConfigMapKeyRef).filter(configMapKeySelector -> configMapKeySelector.getName().equals("distributedlogs")).ifPresent(configMapKeySelector -> configMapKeySelector.setName(distributedLogsName));
    });
    patroniContainer.getEnvFrom().stream().map(EnvFromSource::getConfigMapRef).forEach(configMapEnvSource -> configMapEnvSource.setName(distributedLogsName));
    final List<Container> containers = sts.getSpec().getTemplate().getSpec().getContainers();
    final List<Container> initContainers = sts.getSpec().getTemplate().getSpec().getInitContainers();
    var allContainers = Stream.concat(containers.stream(), initContainers.stream());
    allContainers.forEach(container -> container.getVolumeMounts().stream().filter(volumeMount -> volumeMount.getName().startsWith("distributedlogs")).forEach(volumeMount -> {
        String name = volumeMount.getName().replaceAll("distributedlogs", distributedLogsName);
        volumeMount.setName(name);
    }));
    final List<OwnerReference> ownerReferences = List.of(new OwnerReferenceBuilder().withApiVersion(CommonDefinition.GROUP + "/v1beta1").withKind(distributedLogs095.getKind()).withName(distributedLogsName).withUid(uid).withController(true).build());
    sts.getMetadata().setOwnerReferences(ownerReferences);
    sts.getSpec().setServiceName(distributedLogsName);
    sts.getSpec().getTemplate().getSpec().setServiceAccount(distributedLogsName + "-patroni");
    sts.getSpec().getTemplate().getSpec().setServiceAccountName(distributedLogsName + "-patroni");
    sts.getSpec().getTemplate().getSpec().getVolumes().stream().filter(volume -> volume.getName().startsWith("distributedlogs")).forEach(volume -> {
        String name = volume.getName().replaceAll("distributedlogs", distributedLogsName);
        volume.setName(name);
    });
    sts.getSpec().getTemplate().getSpec().getVolumes().stream().map(Volume::getConfigMap).filter(Objects::nonNull).forEach(configMap -> {
        String configMapName = configMap.getName().replaceAll("distributedlogs", distributedLogsName);
        configMap.setName(configMapName);
    });
    sts.getSpec().getVolumeClaimTemplates().forEach(vct -> {
        fix095Labels(vct, distributedLogs095);
        vct.getMetadata().setName(distributedLogsName + "-data");
        vct.getMetadata().setNamespace(namespace);
        vct.getMetadata().setOwnerReferences(ownerReferences);
    });
    return sts;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresClusterList(io.stackgres.common.crd.sgcluster.StackGresClusterList) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) Seq(org.jooq.lambda.Seq) Mockito.lenient(org.mockito.Mockito.lenient) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Inject(javax.inject.Inject) StringUtils(io.stackgres.testutil.StringUtils) CharStreams(com.google.common.io.CharStreams) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) StackGresDistributedLogs(io.stackgres.common.crd.sgdistributedlogs.StackGresDistributedLogs) Volume(io.fabric8.kubernetes.api.model.Volume) InjectMock(io.quarkus.test.junit.mockito.InjectMock) ImmutableMap(com.google.common.collect.ImmutableMap) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) ReconciliationScope(io.stackgres.operator.conciliation.ReconciliationScope) IOException(java.io.IOException) StackGresUtil(io.stackgres.common.StackGresUtil) Reader(java.io.Reader) UUID(java.util.UUID) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StandardCharsets(java.nio.charset.StandardCharsets) DefaultComparator(io.stackgres.operator.conciliation.comparator.DefaultComparator) JsonUtil(io.stackgres.testutil.JsonUtil) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) Comparator(java.util.Comparator) CommonDefinition(io.stackgres.common.crd.CommonDefinition) NotNull(org.jetbrains.annotations.NotNull) InputStream(java.io.InputStream) CustomResource(io.fabric8.kubernetes.client.CustomResource) Container(io.fabric8.kubernetes.api.model.Container) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

EnvFromSource (io.fabric8.kubernetes.api.model.EnvFromSource)15 Container (io.fabric8.kubernetes.api.model.Container)9 Test (org.junit.jupiter.api.Test)9 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)8 List (java.util.List)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 IOException (java.io.IOException)6 ProdBuildResults (io.quarkus.test.ProdBuildResults)5 ProdModeTestResults (io.quarkus.test.ProdModeTestResults)5 QuarkusProdModeTest (io.quarkus.test.QuarkusProdModeTest)5 Path (java.nio.file.Path)5 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)5 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)4 EnvFromSourceBuilder (io.fabric8.kubernetes.api.model.EnvFromSourceBuilder)4 Pod (io.fabric8.kubernetes.api.model.Pod)4 Optional (java.util.Optional)4 PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Test (org.testng.annotations.Test)4