Search in sources :

Example 6 with KubernetesEnvironment

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

the class KubernetesPluginsToolingApplier method apply.

@Override
public void apply(RuntimeIdentity runtimeIdentity, InternalEnvironment internalEnvironment, Collection<ChePlugin> chePlugins) throws InfrastructureException {
    if (chePlugins.isEmpty()) {
        return;
    }
    KubernetesEnvironment k8sEnv = (KubernetesEnvironment) internalEnvironment;
    Map<String, PodData> pods = k8sEnv.getPodsData();
    switch(pods.size()) {
        case 0:
            addToolingPod(k8sEnv);
            pods = k8sEnv.getPodsData();
            break;
        case 1:
            break;
        default:
            throw new InfrastructureException("Che plugins tooling configuration can be applied to a workspace with one pod only");
    }
    PodData pod = pods.values().iterator().next();
    CommandsResolver commandsResolver = new CommandsResolver(k8sEnv);
    for (ChePlugin chePlugin : chePlugins) {
        Map<String, ComponentImpl> devfilePlugins = k8sEnv.getDevfile().getComponents().stream().filter(c -> c.getType().equals("cheEditor") || c.getType().equals("chePlugin")).collect(Collectors.toMap(ComponentImpl::getId, Function.identity()));
        if (!devfilePlugins.containsKey(chePlugin.getId())) {
            throw new InfrastructureException(String.format("The downloaded plugin '%s' configuration does not have the " + "corresponding component in devfile. Devfile contains the following cheEditor/chePlugins: %s", chePlugin.getId(), devfilePlugins.keySet()));
        }
        ComponentImpl pluginRelatedComponent = devfilePlugins.get(chePlugin.getId());
        for (CheContainer container : chePlugin.getInitContainers()) {
            Container k8sInitContainer = toK8sContainer(container);
            envVars.apply(k8sInitContainer, pluginRelatedComponent.getEnv());
            chePluginsVolumeApplier.applyVolumes(pod, k8sInitContainer, container.getVolumes(), k8sEnv);
            pod.getSpec().getInitContainers().add(k8sInitContainer);
        }
        Collection<CommandImpl> pluginRelatedCommands = commandsResolver.resolve(chePlugin);
        for (CheContainer container : chePlugin.getContainers()) {
            addSidecar(pod, container, chePlugin, k8sEnv, pluginRelatedCommands, pluginRelatedComponent, runtimeIdentity);
        }
    }
    chePlugins.forEach(chePlugin -> populateWorkspaceEnvVars(chePlugin, 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) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) Container(io.fabric8.kubernetes.api.model.Container) CheContainer(org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer) ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 7 with KubernetesEnvironment

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

the class KubernetesArtifactsBrokerApplierTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    // Workspace env setup
    ObjectMeta workspacePodMeta = new ObjectMetaBuilder().withAnnotations(workspacePodAnnotations).build();
    workspacePod = new PodBuilder().withMetadata(workspacePodMeta).withSpec(new PodSpec()).build();
    Map<String, ConfigMap> workspaceConfigMaps = new HashMap<>();
    workspaceEnvironment = KubernetesEnvironment.builder().setPods(ImmutableMap.of(WORKSPACE_POD_NAME, workspacePod)).setMachines(new HashMap<>()).setConfigMaps(workspaceConfigMaps).build();
    // Broker env setup
    ObjectMeta brokerPodMeta = new ObjectMetaBuilder().withAnnotations(brokerPodAnnotations).build();
    brokerContainer = new ContainerBuilder().withName(BROKER_CONTAINER_NAME).build();
    brokerVolume = new VolumeBuilder().build();
    Pod brokerPod = new PodBuilder().withMetadata(brokerPodMeta).withNewSpec().withContainers(brokerContainer).withVolumes(brokerVolume).endSpec().build();
    brokerConfigMap = new ConfigMapBuilder().addToData(brokerConfigMapData).build();
    KubernetesEnvironment brokerEnvironment = KubernetesEnvironment.builder().setPods(ImmutableMap.of(BROKER_POD_NAME, brokerPod)).setConfigMaps(ImmutableMap.of(BROKER_CONFIGMAP_NAME, brokerConfigMap)).setMachines(ImmutableMap.of(BROKER_MACHINE_NAME, brokerMachine)).build();
    doReturn(brokerEnvironment).when(brokerEnvironmentFactory).createForArtifactsBroker(any(), any(), anyBoolean());
    applier = new KubernetesArtifactsBrokerApplier<>(brokerEnvironmentFactory);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Pod(io.fabric8.kubernetes.api.model.Pod) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) HashMap(java.util.HashMap) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 8 with KubernetesEnvironment

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

the class GatewayTlsProvisionerTest method provisionTlsForGatewayRouteConfigmaps.

@Test(dataProvider = "tlsProvisionData")
public void provisionTlsForGatewayRouteConfigmaps(ServerConfigImpl server, boolean tlsEnabled, String expectedProtocol) throws Exception {
    // given
    Map<String, String> composedAnnotations = new HashMap<>(annotations);
    composedAnnotations.putAll(Annotations.newSerializer().server("server", server).machineName(machine).annotations());
    ConfigMap routeConfigMap = new ConfigMapBuilder().withNewMetadata().withName("route").withAnnotations(composedAnnotations).endMetadata().build();
    GatewayTlsProvisioner<KubernetesEnvironment> gatewayTlsProvisioner = new GatewayTlsProvisioner<>(tlsEnabled, gatewayConfigmapLabels, tlsProvisionerProvider);
    lenient().when(k8sEnv.getConfigMaps()).thenReturn(singletonMap("route", routeConfigMap));
    // when
    gatewayTlsProvisioner.provision(k8sEnv, runtimeIdentity);
    // then
    Map<String, ServerConfigImpl> servers = Annotations.newDeserializer(routeConfigMap.getMetadata().getAnnotations()).servers();
    assertEquals(servers.get("server").getProtocol(), expectedProtocol);
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) HashMap(java.util.HashMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) Test(org.testng.annotations.Test)

Example 9 with KubernetesEnvironment

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

the class KubernetesPreviewUrlCommandProvisionerTest method shouldUpdateCommandWhenServiceAndIngressFound.

@Test
public void shouldUpdateCommandWhenServiceAndIngressFound() throws InfrastructureException {
    final int PORT = 8080;
    final String SERVICE_PORT_NAME = "service-" + PORT;
    List<CommandImpl> commands = Collections.singletonList(new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap()));
    KubernetesEnvironment env = KubernetesEnvironment.builder().setCommands(new ArrayList<>(commands)).build();
    Mockito.when(mockNamespace.services()).thenReturn(mockServices);
    Service service = new Service();
    ObjectMeta metadata = new ObjectMeta();
    metadata.setName("servicename");
    service.setMetadata(metadata);
    ServiceSpec spec = new ServiceSpec();
    spec.setPorts(Collections.singletonList(new ServicePort(null, SERVICE_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
    service.setSpec(spec);
    Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));
    Ingress ingress = new Ingress();
    IngressSpec ingressSpec = new IngressSpec();
    IngressRule rule = new IngressRule("testhost", new HTTPIngressRuleValue(Collections.singletonList(new HTTPIngressPath(new IngressBackend(null, new IngressServiceBackend("servicename", new ServiceBackendPort(SERVICE_PORT_NAME, PORT))), null, null))));
    ingressSpec.setRules(Collections.singletonList(rule));
    ingress.setSpec(ingressSpec);
    Mockito.when(mockNamespace.ingresses()).thenReturn(mockIngresses);
    Mockito.when(mockIngresses.get()).thenReturn(Collections.singletonList(ingress));
    previewUrlCommandProvisioner.provision(env, mockNamespace);
    assertTrue(env.getCommands().get(0).getAttributes().containsKey("previewUrl"));
    assertEquals(env.getCommands().get(0).getAttributes().get("previewUrl"), "testhost");
    assertTrue(env.getWarnings().isEmpty());
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) PreviewUrlImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.PreviewUrlImpl) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ArrayList(java.util.ArrayList) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) Service(io.fabric8.kubernetes.api.model.Service) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) HTTPIngressPath(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath) IngressServiceBackend(io.fabric8.kubernetes.api.model.networking.v1.IngressServiceBackend) IngressSpec(io.fabric8.kubernetes.api.model.networking.v1.IngressSpec) ServiceBackendPort(io.fabric8.kubernetes.api.model.networking.v1.ServiceBackendPort) IngressRule(io.fabric8.kubernetes.api.model.networking.v1.IngressRule) HTTPIngressRuleValue(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressRuleValue) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) IngressBackend(io.fabric8.kubernetes.api.model.networking.v1.IngressBackend) Test(org.testng.annotations.Test)

Example 10 with KubernetesEnvironment

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

the class ProxySettingsProvisionerTest method shouldApplyProxySettingsToAllContainers.

@Test
public void shouldApplyProxySettingsToAllContainers() throws Exception {
    Map<String, Pod> pods = new HashMap<>();
    Pod pod1 = pods.put("pod1", buildPod("pod1", buildContainers(2)));
    pods.put("pod2", buildPod("pod2", buildContainers(3)));
    KubernetesEnvironment k8sEnv = KubernetesEnvironment.builder().setPods(pods).build();
    provisioner.provision(k8sEnv, runtimeId);
    assertTrue(k8sEnv.getPodsData().values().stream().flatMap(pod -> pod.getSpec().getContainers().stream()).allMatch(container -> container.getEnv().contains(new EnvVar(HTTP_PROXY, HTTP_PROXY_VALUE, null)) && container.getEnv().contains(new EnvVar(HTTPS_PROXY, HTTPS_PROXY_VALUE, null)) && container.getEnv().contains(new EnvVar(NO_PROXY, NO_PROXY_VALUE, null))));
}
Also used : HTTP_PROXY(org.eclipse.che.workspace.infrastructure.kubernetes.provision.ProxySettingsProvisioner.HTTP_PROXY) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Arrays(java.util.Arrays) NameGenerator.generate(org.eclipse.che.commons.lang.NameGenerator.generate) Listeners(org.testng.annotations.Listeners) Container(io.fabric8.kubernetes.api.model.Container) Mock(org.mockito.Mock) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) Mockito.lenient(org.mockito.Mockito.lenient) ArrayList(java.util.ArrayList) HTTPS_PROXY(org.eclipse.che.workspace.infrastructure.kubernetes.provision.ProxySettingsProvisioner.HTTPS_PROXY) NO_PROXY(org.eclipse.che.workspace.infrastructure.kubernetes.provision.ProxySettingsProvisioner.NO_PROXY) JWT_PROXY_POD_NAME(org.eclipse.che.workspace.infrastructure.kubernetes.server.secure.jwtproxy.JwtProxyProvisioner.JWT_PROXY_POD_NAME) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) BeforeMethod(org.testng.annotations.BeforeMethod) Pod(io.fabric8.kubernetes.api.model.Pod) List(java.util.List) Stream(java.util.stream.Stream) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Assert.assertTrue(org.testng.Assert.assertTrue) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) 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