Search in sources :

Example 66 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.

the class KubernetesComponentToWorkspaceApplierTest method shouldProvisionEnvIntoK8SList.

@Test
public void shouldProvisionEnvIntoK8SList() throws Exception {
    // given
    List<HasMetadata> k8sList = new ArrayList<>();
    Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").endMetadata().withNewSpec().endSpec().build();
    Pod pod2 = new PodBuilder().withNewMetadata().withName("pod2").endMetadata().withNewSpec().endSpec().build();
    k8sList.add(pod1);
    k8sList.add(pod2);
    doReturn(k8sList).when(k8sRecipeParser).parse(anyString());
    ComponentImpl component = new ComponentImpl();
    component.setType(KUBERNETES_COMPONENT_TYPE);
    component.setReference(REFERENCE_FILENAME);
    component.setAlias(COMPONENT_NAME);
    List<EnvImpl> envToApply = singletonList(new EnvImpl("TEST_ENV", "anyValue"));
    component.setEnv(envToApply);
    // when
    applier.apply(workspaceConfig, component, s -> "content");
    // then
    envVars.apply(new PodData(pod1), envToApply);
    envVars.apply(new PodData(pod2), envToApply);
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) ArrayList(java.util.ArrayList) EnvImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 67 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.

the class KubernetesEnvironmentFactoryTest method createPodData.

private static PodData createPodData(String machineName, long ramLimit, long ramRequest) {
    final String containerName = "container_" + machineName;
    final Container containerMock = mock(Container.class);
    final ResourceRequirements resourcesMock = mock(ResourceRequirements.class);
    final Quantity limitQuantityMock = mock(Quantity.class);
    final Quantity requestQuantityMock = mock(Quantity.class);
    final PodSpec specMock = mock(PodSpec.class);
    final ObjectMeta metadataMock = mock(ObjectMeta.class);
    when(limitQuantityMock.getAmount()).thenReturn(String.valueOf(ramLimit));
    when(requestQuantityMock.getAmount()).thenReturn(String.valueOf(ramRequest));
    when(resourcesMock.getLimits()).thenReturn(ImmutableMap.of("memory", limitQuantityMock));
    when(resourcesMock.getRequests()).thenReturn(ImmutableMap.of("memory", requestQuantityMock));
    when(containerMock.getName()).thenReturn(containerName);
    when(containerMock.getResources()).thenReturn(resourcesMock);
    when(metadataMock.getAnnotations()).thenReturn(Names.createMachineNameAnnotations(containerName, machineName));
    when(specMock.getContainers()).thenReturn(ImmutableList.of(containerMock));
    return new PodData(specMock, metadataMock);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Quantity(io.fabric8.kubernetes.api.model.Quantity) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements)

Example 68 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.

the class KubernetesEnvironmentFactoryTest method shouldReconfigureServiceToMatchMergedDeployment.

@Test
public void shouldReconfigureServiceToMatchMergedDeployment() throws Exception {
    // given
    Pod pod1 = new PodBuilder().withNewMetadata().withName("bare-pod1").withLabels(ImmutableMap.of("name", "pod1")).endMetadata().withNewSpec().endSpec().build();
    Pod pod2 = new PodBuilder().withNewMetadata().withName("bare-pod2").withLabels(ImmutableMap.of("name", "pod2")).endMetadata().withNewSpec().endSpec().build();
    Service service1 = new ServiceBuilder().withNewMetadata().withName("pod1-service").endMetadata().withNewSpec().withSelector(ImmutableMap.of("name", "pod1")).endSpec().build();
    Service service2 = new ServiceBuilder().withNewMetadata().withName("pod2-service").endMetadata().withNewSpec().withSelector(ImmutableMap.of("name", "pod2")).endSpec().build();
    when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(pod1, pod2, service1, service2));
    Deployment merged = createEmptyDeployment("merged");
    when(podMerger.merge(any())).thenReturn(merged);
    // when
    final KubernetesEnvironment k8sEnv = k8sEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());
    // then
    verify(podMerger).merge(asList(new PodData(pod1), new PodData(pod2)));
    PodData mergedPodData = k8sEnv.getPodsData().get("merged");
    assertEquals(mergedPodData.getMetadata().getLabels().get(DEPLOYMENT_NAME_LABEL), "merged");
    assertTrue(k8sEnv.getServices().values().stream().allMatch(s -> ImmutableMap.of(DEPLOYMENT_NAME_LABEL, "merged").equals(s.getSpec().getSelector())));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) Listeners(org.testng.annotations.Listeners) Test(org.testng.annotations.Test) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Collections.singletonList(java.util.Collections.singletonList) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Arrays.asList(java.util.Arrays.asList) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) IngressBuilder(io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder) WarningImpl(org.eclipse.che.api.workspace.server.model.impl.WarningImpl) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertNotNull(org.testng.Assert.assertNotNull) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Secret(io.fabric8.kubernetes.api.model.Secret) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Container(io.fabric8.kubernetes.api.model.Container) Mock(org.mockito.Mock) Assert.assertEquals(org.testng.Assert.assertEquals) INGRESSES_IGNORED_WARNING_MESSAGE(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.INGRESSES_IGNORED_WARNING_MESSAGE) Mockito.lenient(org.mockito.Mockito.lenient) ValidationException(org.eclipse.che.api.core.ValidationException) ImmutableList(com.google.common.collect.ImmutableList) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) INGRESSES_IGNORED_WARNING_CODE(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.INGRESSES_IGNORED_WARNING_CODE) Service(io.fabric8.kubernetes.api.model.Service) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) Collections.emptyMap(java.util.Collections.emptyMap) DEPLOYMENT_NAME_LABEL(org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger.DEPLOYMENT_NAME_LABEL) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.when(org.mockito.Mockito.when) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Mockito.verify(org.mockito.Mockito.verify) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) InternalRecipe(org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Pod(io.fabric8.kubernetes.api.model.Pod) InternalRecipe(org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Test(org.testng.annotations.Test)

Example 69 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.

the class KubernetesInternalRuntimeTest method testMultipleMachinesRequiringSamePodInjectionResultInOnePodInjected.

@Test
public void testMultipleMachinesRequiringSamePodInjectionResultInOnePodInjected() throws Exception {
    // given
    Map<String, Pod> injectedPods = ImmutableMap.of("injected", mockPod(singletonList(mockContainer("injectedContainer"))));
    Map<String, Pod> injectedPods2 = ImmutableMap.of("injected", mockPod(singletonList(mockContainer("injectedContainer"))));
    doReturn(ImmutableMap.of(M1_NAME, injectedPods, M2_NAME, injectedPods2)).when(k8sEnv).getInjectablePodsCopy();
    doReturn(concat(podsMap.entrySet().stream(), injectedPods.entrySet().stream()).collect(toMap(Map.Entry::getKey, e -> new PodData(e.getValue())))).when(k8sEnv).getPodsData();
    doReturn(ImmutableMap.of(M1_NAME, mock(InternalMachineConfig.class), M2_NAME, mock(InternalMachineConfig.class), WORKSPACE_POD_NAME + "/injectedContainer", mock(InternalMachineConfig.class))).when(k8sEnv).getMachines();
    // when
    internalRuntime.start(emptyMap());
    // then
    ArgumentCaptor<Deployment> podCaptor = ArgumentCaptor.forClass(Deployment.class);
    verify(deployments).deploy(podCaptor.capture());
    List<Deployment> depls = podCaptor.getAllValues();
    assertEquals(depls.size(), 1);
    Deployment deployedPod = depls.get(0);
    List<String> containerNames = deployedPod.getSpec().getTemplate().getSpec().getContainers().stream().map(Container::getName).collect(toList());
    assertEquals(containerNames.size(), 3);
    assertEquals(new HashSet<>(containerNames), new HashSet<>(asList(CONTAINER_NAME_1, CONTAINER_NAME_2, "injectedContainer")));
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Entry(java.util.Map.Entry) Pod(io.fabric8.kubernetes.api.model.Pod) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test)

Example 70 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.

the class KubernetesEnvironmentFactoryTest method shouldMergeDeploymentAndPodIntoOneDeployment.

@Test
public void shouldMergeDeploymentAndPodIntoOneDeployment() throws Exception {
    // given
    PodTemplateSpec podTemplate = new PodTemplateSpecBuilder().withNewMetadata().withName("deployment-pod").endMetadata().withNewSpec().endSpec().build();
    Deployment deployment = new DeploymentBuilder().withNewMetadata().withName("deployment-test").endMetadata().withNewSpec().withTemplate(podTemplate).endSpec().build();
    Pod pod = new PodBuilder().withNewMetadata().withName("bare-pod").endMetadata().withNewSpec().endSpec().build();
    when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(deployment, pod));
    Deployment merged = createEmptyDeployment("merged");
    when(podMerger.merge(any())).thenReturn(merged);
    // when
    final KubernetesEnvironment k8sEnv = k8sEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());
    // then
    verify(podMerger).merge(asList(new PodData(pod), new PodData(deployment)));
    assertEquals(k8sEnv.getPodsData().size(), 1);
    assertTrue(k8sEnv.getPodsCopy().isEmpty());
    assertEquals(k8sEnv.getDeploymentsCopy().size(), 1);
    assertEquals(k8sEnv.getDeploymentsCopy().get("merged"), merged);
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Pod(io.fabric8.kubernetes.api.model.Pod) InternalRecipe(org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Aggregations

PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)156 Test (org.testng.annotations.Test)86 Container (io.fabric8.kubernetes.api.model.Container)62 Pod (io.fabric8.kubernetes.api.model.Pod)56 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)52 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)52 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)40 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)36 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)34 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)30 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)28 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)26 Map (java.util.Map)22 InternalMachineConfig (org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig)22 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)20 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)20 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)20 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)16 Volume (io.fabric8.kubernetes.api.model.Volume)16 HashMap (java.util.HashMap)16