Search in sources :

Example 91 with CommandImpl

use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project devspaces-images by redhat-developer.

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 92 with CommandImpl

use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project devspaces-images by redhat-developer.

the class KubernetesPreviewUrlCommandProvisionerTest method shouldDoNothingWhenCantFindServiceForPreviewurl.

@Test
public void shouldDoNothingWhenCantFindServiceForPreviewurl() throws InfrastructureException {
    List<CommandImpl> commands = Collections.singletonList(new CommandImpl("a", "a", "a", new PreviewUrlImpl(8080, null), Collections.emptyMap()));
    KubernetesEnvironment env = KubernetesEnvironment.builder().setCommands(new ArrayList<>(commands)).build();
    Mockito.when(mockNamespace.ingresses()).thenReturn(mockIngresses);
    Mockito.when(mockNamespace.services()).thenReturn(mockServices);
    Mockito.when(mockServices.get()).thenReturn(Collections.emptyList());
    previewUrlCommandProvisioner.provision(env, mockNamespace);
    assertTrue(commands.containsAll(env.getCommands()));
    assertTrue(env.getCommands().containsAll(commands));
    assertEquals(env.getWarnings().get(0).getCode(), Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL);
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) PreviewUrlImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.PreviewUrlImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 93 with CommandImpl

use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project devspaces-images by redhat-developer.

the class PreviewUrlCommandProvisioner method injectsPreviewUrlToCommands.

/**
 * Go through all commands, find matching service and exposed host. Then construct full preview
 * url from this data and set it as Command's parameter under `previewUrl` key.
 *
 * @param env environment to get commands
 * @param namespace current kubernetes namespace where we're looking for services and ingresses
 */
private void injectsPreviewUrlToCommands(E env, KubernetesNamespace namespace) throws InfrastructureException {
    if (env.getCommands() == null) {
        return;
    }
    List<T> exposureObjects = loadExposureObjects(namespace);
    List<Service> services = namespace.services().get();
    for (CommandImpl command : env.getCommands().stream().filter(c -> c.getPreviewUrl() != null).collect(Collectors.toList())) {
        Optional<Service> foundService = Services.findServiceWithPort(services, command.getPreviewUrl().getPort());
        if (!foundService.isPresent()) {
            String message = String.format("unable to find service for port '%s' for command '%s'", command.getPreviewUrl().getPort(), command.getName());
            LOG.warn(message);
            env.addWarning(new WarningImpl(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL, String.format(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE, message)));
            continue;
        }
        Optional<String> foundHost = findHostForServicePort(exposureObjects, foundService.get(), command.getPreviewUrl().getPort());
        if (foundHost.isPresent()) {
            command.getAttributes().put(PREVIEW_URL_ATTRIBUTE, foundHost.get());
        } else {
            String message = String.format("unable to find ingress for service '%s' and port '%s'", foundService.get(), command.getPreviewUrl().getPort());
            LOG.warn(message);
            env.addWarning(new WarningImpl(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL, String.format(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE, message)));
        }
    }
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL) Logger(org.slf4j.Logger) WarningImpl(org.eclipse.che.api.workspace.server.model.impl.WarningImpl) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) LoggerFactory(org.slf4j.LoggerFactory) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) Services(org.eclipse.che.workspace.infrastructure.kubernetes.util.Services) NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) Optional(java.util.Optional) Service(io.fabric8.kubernetes.api.model.Service) PREVIEW_URL_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.PREVIEW_URL_ATTRIBUTE) WarningImpl(org.eclipse.che.api.workspace.server.model.impl.WarningImpl) Service(io.fabric8.kubernetes.api.model.Service)

Example 94 with CommandImpl

use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project devspaces-images by redhat-developer.

the class KubernetesPluginsToolingApplierTest method shouldResolveMachineNameForCommandsInEnvironment.

@Test
public void shouldResolveMachineNameForCommandsInEnvironment() throws Exception {
    // given
    ChePlugin chePlugin = createChePlugin(createContainer("container"));
    String pluginRef = chePlugin.getId();
    CommandImpl pluginCommand = new CommandImpl("test-command", "echo Hello World!", "custom");
    pluginCommand.getAttributes().put("plugin", pluginRef);
    internalEnvironment.getCommands().add(pluginCommand);
    // when
    applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin));
    // then
    List<CommandImpl> envCommands = internalEnvironment.getCommands();
    assertEquals(envCommands.size(), 1);
    CommandImpl envCommand = envCommands.get(0);
    assertEquals(envCommand.getName(), pluginCommand.getName());
    assertEquals(envCommand.getType(), pluginCommand.getType());
    assertEquals(envCommand.getCommandLine(), pluginCommand.getCommandLine());
    assertEquals(envCommand.getAttributes().get("plugin"), pluginRef);
    validateContainerNameName(envCommand.getAttributes().get(MACHINE_NAME_ATTRIBUTE), "container");
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) Test(org.testng.annotations.Test)

Example 95 with CommandImpl

use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project devspaces-images by redhat-developer.

the class KubernetesPluginsToolingApplierTest method shouldFillInWarningIfChePluginHasMultiplyContainersButThereAreRelatedCommands.

@Test
public void shouldFillInWarningIfChePluginHasMultiplyContainersButThereAreRelatedCommands() throws Exception {
    // given
    ChePlugin chePlugin = createChePlugin(createContainer(), createContainer());
    String pluginRef = chePlugin.getId();
    CommandImpl pluginCommand = new CommandImpl("test-command", "echo Hello World!", "custom");
    pluginCommand.getAttributes().put("plugin", pluginRef);
    internalEnvironment.getCommands().add(pluginCommand);
    // when
    applier.apply(runtimeIdentity, internalEnvironment, singletonList(chePlugin));
    // then
    List<Warning> envWarnings = internalEnvironment.getWarnings();
    assertEquals(envWarnings.size(), 1);
    Warning warning = envWarnings.get(0);
    assertEquals(warning.getCode(), Warnings.COMMAND_IS_CONFIGURED_IN_PLUGIN_WITH_MULTIPLY_CONTAINERS_WARNING_CODE);
    assertEquals(warning.getMessage(), "There are configured commands for plugin '" + pluginRef + "' that has multiply containers. Commands will be configured to be run in first container");
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) Warning(org.eclipse.che.api.core.model.workspace.Warning) ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) Test(org.testng.annotations.Test)

Aggregations

CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)106 Test (org.testng.annotations.Test)80 PreviewUrlImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.PreviewUrlImpl)40 ArrayList (java.util.ArrayList)32 Service (io.fabric8.kubernetes.api.model.Service)30 HashMap (java.util.HashMap)30 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)26 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)24 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)24 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)20 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)18 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)18 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)18 OpenShiftEnvironment (org.eclipse.che.workspace.infrastructure.openshift.environment.OpenShiftEnvironment)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)14 Ingress (io.fabric8.kubernetes.api.model.networking.v1.Ingress)14 List (java.util.List)12 IngressBackend (io.fabric8.kubernetes.api.model.networking.v1.IngressBackend)10 ChePlugin (org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin)10 ProjectConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl)9