Search in sources :

Example 96 with PodData

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

the class PodsVolumesTest method setUp.

@BeforeMethod
public void setUp() {
    Pod pod = newPod(POD_1_NAME).withContainers(newContainer(CONTAINER_1_NAME).build(), newContainer(CONTAINER_2_NAME).build()).build();
    podData = new PodData(pod.getSpec(), pod.getMetadata());
    podsVolumes = new PodsVolumes();
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Pod(io.fabric8.kubernetes.api.model.Pod) TestObjects.newPod(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.TestObjects.newPod) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 97 with PodData

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

the class KubernetesInternalRuntimeTest method testInjectablePodsMergedIntoRuntimePods.

@Test
public void testInjectablePodsMergedIntoRuntimePods() throws Exception {
    // given
    Map<String, Pod> injectedPods = ImmutableMap.of("injected", mockPod(singletonList(mockContainer("injectedContainer"))));
    doReturn(ImmutableMap.of(M1_NAME, injectedPods)).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 98 with PodData

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

the class KubernetesArtifactsBrokerApplier method apply.

/**
 * Apply plugin broker as init container to workspace environment. Workspace environment will have
 * broker's configmap, machines, and volumes added in addition to the init container
 */
public void apply(E workspaceEnvironment, RuntimeIdentity runtimeID, Collection<PluginFQN> pluginFQNs, boolean mergePlugins) throws InfrastructureException {
    E brokerEnvironment = brokerEnvironmentFactory.createForArtifactsBroker(pluginFQNs, runtimeID, mergePlugins);
    Map<String, PodData> workspacePods = workspaceEnvironment.getPodsData();
    if (workspacePods.size() != 1) {
        throw new InfrastructureException("Che plugins tooling configuration can be applied to a workspace with one pod only.");
    }
    PodData workspacePod = workspacePods.values().iterator().next();
    Map<String, PodData> brokerPods = brokerEnvironment.getPodsData();
    if (brokerPods.size() != 1) {
        throw new InfrastructureException("Broker environment must have only one Pod.");
    }
    PodData brokerPod = brokerPods.values().iterator().next();
    // Add broker machines to workspace environment so that the init containers can be provisioned.
    List<Container> brokerContainers = brokerPod.getSpec().getContainers();
    for (Container container : brokerContainers) {
        InternalMachineConfig brokerMachine = brokerEnvironment.getMachines().get(Names.machineName(brokerPod, container));
        if (brokerMachine == null) {
            throw new InfrastructureException(String.format("Could not retrieve the specification of the plugin broker container %s", container.getName()));
        }
        workspaceEnvironment.getMachines().put(Names.machineName(workspacePod, container), brokerMachine);
    }
    workspaceEnvironment.getConfigMaps().putAll(brokerEnvironment.getConfigMaps());
    workspacePod.getSpec().getInitContainers().addAll(brokerPod.getSpec().getContainers());
    workspacePod.getSpec().getVolumes().addAll(brokerPod.getSpec().getVolumes());
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 99 with PodData

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

the class KubernetesPluginsToolingApplier method addSidecar.

/**
 * Adds k8s and Che specific configuration of a sidecar into the environment. For example:
 * <li>k8s container configuration {@link Container}
 * <li>k8s service configuration {@link Service}
 * <li>Che machine config {@link InternalMachineConfig}
 * <li>Fill in machine name attribute in related commands
 *
 * @throws InfrastructureException when any error occurs
 */
private void addSidecar(PodData pod, CheContainer container, ChePlugin chePlugin, KubernetesEnvironment k8sEnv, Collection<CommandImpl> sidecarRelatedCommands, Component pluginRelatedComponent, RuntimeIdentity runtimeIdentity) throws InfrastructureException {
    K8sContainerResolver k8sContainerResolver = toK8sContainerResolver(container, chePlugin.getEndpoints());
    List<ChePluginEndpoint> containerEndpoints = k8sContainerResolver.getEndpoints();
    Container k8sContainer = k8sContainerResolver.resolve();
    envVars.apply(k8sContainer, pluginRelatedComponent.getEnv());
    chePluginsVolumeApplier.applyVolumes(pod, k8sContainer, container.getVolumes(), k8sEnv);
    String machineName = k8sContainer.getName();
    Names.putMachineName(pod.getMetadata(), k8sContainer.getName(), machineName);
    pod.getSpec().getContainers().add(k8sContainer);
    MachineResolver machineResolver = new MachineResolverBuilder().setCheContainer(container).setContainer(k8sContainer).setContainerEndpoints(containerEndpoints).setDefaultSidecarMemoryLimitAttribute(defaultSidecarMemoryLimitBytes).setDefaultSidecarMemoryRequestAttribute(defaultSidecarMemoryRequestBytes).setDefaultSidecarCpuLimitAttribute(defaultSidecarCpuLimitCores).setDefaultSidecarCpuRequestAttribute(defaultSidecarCpuRequestCores).setProjectsRootPathEnvVar(projectsRootEnvVariableProvider.get(runtimeIdentity)).setComponent(pluginRelatedComponent).build();
    InternalMachineConfig machineConfig = machineResolver.resolve();
    machineConfig.getAttributes().put(CONTAINER_SOURCE_ATTRIBUTE, TOOL_CONTAINER_SOURCE);
    machineConfig.getAttributes().put(PLUGIN_MACHINE_ATTRIBUTE, chePlugin.getId());
    k8sEnv.getMachines().put(machineName, machineConfig);
    sidecarRelatedCommands.forEach(c -> c.getAttributes().put(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE, machineName));
    container.getCommands().stream().map(c -> asCommand(machineName, c)).forEach(c -> k8sEnv.getCommands().add(c));
    SidecarServicesProvisioner sidecarServicesProvisioner = new SidecarServicesProvisioner(containerEndpoints, pod.getMetadata().getName());
    sidecarServicesProvisioner.provision(k8sEnv);
}
Also used : PLUGIN_MACHINE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.PLUGIN_MACHINE_ATTRIBUTE) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) ChePluginEndpoint(org.eclipse.che.api.workspace.server.wsplugins.model.ChePluginEndpoint) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Singleton(javax.inject.Singleton) CONTAINER_SOURCE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.CONTAINER_SOURCE_ATTRIBUTE) Function(java.util.function.Function) WORKING_DIRECTORY_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.WORKING_DIRECTORY_ATTRIBUTE) Inject(javax.inject.Inject) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) InternalEnvironment(org.eclipse.che.api.workspace.server.spi.environment.InternalEnvironment) Map(java.util.Map) MACHINE_NAME_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE) Service(io.fabric8.kubernetes.api.model.Service) Named(javax.inject.Named) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) Command(org.eclipse.che.api.workspace.server.wsplugins.model.Command) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) ChePluginsApplier(org.eclipse.che.api.workspace.server.wsplugins.ChePluginsApplier) TOOL_CONTAINER_SOURCE(org.eclipse.che.api.workspace.shared.Constants.TOOL_CONTAINER_SOURCE) WarningImpl(org.eclipse.che.api.workspace.server.model.impl.WarningImpl) Component(org.eclipse.che.api.core.model.workspace.devfile.Component) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Pod(io.fabric8.kubernetes.api.model.Pod) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Sets(com.google.common.collect.Sets) Beta(com.google.common.annotations.Beta) ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Warnings(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) ProjectsRootEnvVariableProvider(org.eclipse.che.api.workspace.server.spi.provision.env.ProjectsRootEnvVariableProvider) KubernetesSize(org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSize) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) ChePluginEndpoint(org.eclipse.che.api.workspace.server.wsplugins.model.ChePluginEndpoint)

Example 100 with PodData

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

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)

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