Search in sources :

Example 71 with KubernetesEnvironment

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project che-server by eclipse-che.

the class OpenShiftEnvironmentFactoryTest 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 = osEnvFactory.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) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Example 72 with KubernetesEnvironment

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project che-server by eclipse-che.

the class OpenShiftEnvironmentFactoryTest 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 = osEnvFactory.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) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) 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) Route(io.fabric8.openshift.api.model.Route) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) PodMerger(org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) RouteBuilder(io.fabric8.openshift.api.model.RouteBuilder) 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) ValidationException(org.eclipse.che.api.core.ValidationException) ImmutableList(com.google.common.collect.ImmutableList) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) 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) KubernetesRecipeParser(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesRecipeParser) 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) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) 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 73 with KubernetesEnvironment

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

the class KubernetesServerExposerTest method assertThatSecureServerIsExposed.

@SuppressWarnings("SameParameterValue")
private void assertThatSecureServerIsExposed(String machineName, String portProtocol, Integer port, String serverName, ServerConfig serverConfig) throws Exception {
    // then
    assertThatContainerPortIsExposed(portProtocol, port);
    // ensure that service is created
    Service service = findContainerRelatedService();
    assertNotNull(service);
    // ensure that no service port is exposed
    assertTrue(service.getSpec().getPorts().stream().noneMatch(p -> p.getTargetPort().getIntVal().equals(port)));
    ServicePort servicePort = new ServicePortBuilder().withName("server-" + port).withPort(port).withProtocol(portProtocol.toUpperCase()).withNewTargetPort(port).build();
    verify(secureServerExposer).expose(eq(kubernetesEnvironment), any(), eq(machineName), // no service exists for the backed server
    isNull(), // a non-unique server
    isNull(), eq(servicePort), eq(ImmutableMap.of(serverName, serverConfig)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Listeners(org.testng.annotations.Listeners) Container(io.fabric8.kubernetes.api.model.Container) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) Assert.assertEquals(org.testng.Assert.assertEquals) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Test(org.testng.annotations.Test) SERVER_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.ServerConfig.SERVER_NAME_ATTRIBUTE) IngressServerExposer(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.IngressServerExposer) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Collections.singletonMap(java.util.Collections.singletonMap) Service(io.fabric8.kubernetes.api.model.Service) Assert.assertFalse(org.testng.Assert.assertFalse) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) DISCOVERABLE_SERVER_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.ServerConfig.DISCOVERABLE_SERVER_ATTRIBUTE) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeMethod(org.testng.annotations.BeforeMethod) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.times(org.mockito.Mockito.times) SecureServerExposer(org.eclipse.che.workspace.infrastructure.kubernetes.server.secure.SecureServerExposer) Assert.assertNotNull(org.testng.Assert.assertNotNull) Mockito.verify(org.mockito.Mockito.verify) ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) SERVER_UNIQUE_PART_SIZE(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer.SERVER_UNIQUE_PART_SIZE) Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) SERVER_PREFIX(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer.SERVER_PREFIX) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Entry(java.util.Map.Entry) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Service(io.fabric8.kubernetes.api.model.Service)

Example 74 with KubernetesEnvironment

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

the class PerWorkspacePVCStrategyTest method shouldPreparePerWorkspacePVCWithSubPathsWhenWaitBoundIsDisabled.

@Test
public void shouldPreparePerWorkspacePVCWithSubPathsWhenWaitBoundIsDisabled() throws Exception {
    // given
    strategy = new PerWorkspacePVCStrategy(PVC_NAME_PREFIX, PVC_QUANTITY, PVC_ACCESS_MODE, true, PVC_STORAGE_CLASS_NAME, false, pvcSubPathHelper, factory, ephemeralWorkspaceAdapter, volumeConverter, podsVolumes, subpathPrefixes, workspaceManager);
    final PersistentVolumeClaim pvc = newPVC(PVC_NAME_PREFIX + "-" + WORKSPACE_ID);
    String perWorkspacePVCName = pvc.getMetadata().getName();
    KubernetesEnvironment k8sEnv = KubernetesEnvironment.builder().build();
    k8sEnv.getPersistentVolumeClaims().put(perWorkspacePVCName, pvc);
    String[] subPaths = { "/projects", "/plugins" };
    pvc.getAdditionalProperties().put(format(SUBPATHS_PROPERTY_FMT, WORKSPACE_ID), subPaths);
    // when
    strategy.prepare(k8sEnv, IDENTITY, 100, emptyMap());
    // then
    verify(pvcs).get();
    verify(pvcs).create(pvc);
    verify(pvcs, never()).waitBound(anyString(), anyLong());
    verify(pvcSubPathHelper).createDirs(IDENTITY, WORKSPACE_ID, perWorkspacePVCName, emptyMap(), subPaths);
}
Also used : KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test)

Example 75 with KubernetesEnvironment

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

the class KubernetesPreviewUrlCommandProvisionerTest method shouldDoNothingWhenCommandsWithoutPreviewUrlDefined.

@Test
public void shouldDoNothingWhenCommandsWithoutPreviewUrlDefined() throws InfrastructureException {
    List<CommandImpl> commands = Arrays.asList(new CommandImpl("a", "a", "a"), new CommandImpl("b", "b", "b"));
    KubernetesEnvironment env = KubernetesEnvironment.builder().setCommands(new ArrayList<>(commands)).build();
    Mockito.when(mockNamespace.ingresses()).thenReturn(mockIngresses);
    Mockito.when(mockNamespace.services()).thenReturn(mockServices);
    previewUrlCommandProvisioner.provision(env, mockNamespace);
    assertTrue(commands.containsAll(env.getCommands()));
    assertTrue(env.getCommands().containsAll(commands));
    assertTrue(env.getWarnings().isEmpty());
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Aggregations

KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)94 Test (org.testng.annotations.Test)68 Map (java.util.Map)30 HashMap (java.util.HashMap)28 Service (io.fabric8.kubernetes.api.model.Service)26 Container (io.fabric8.kubernetes.api.model.Container)24 Pod (io.fabric8.kubernetes.api.model.Pod)24 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)22 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)20 PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)20 ArrayList (java.util.ArrayList)19 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)18 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)18 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)18 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)18 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)18 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)14 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)14 ServerConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl)14 InternalRecipe (org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe)14