use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project che-server by eclipse-che.
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)));
}
}
}
use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project che-server by eclipse-che.
the class OpenShiftPreviewUrlCommandProvisionerTest method shouldUpdateCommandWhenServiceAndIngressFound.
@Test
public void shouldUpdateCommandWhenServiceAndIngressFound() throws InfrastructureException {
int port = 8080;
List<CommandImpl> commands = Collections.singletonList(new CommandImpl("a", "a", "a", new PreviewUrlImpl(port, null), Collections.emptyMap()));
OpenShiftEnvironment env = OpenShiftEnvironment.builder().setCommands(new ArrayList<>(commands)).build();
Mockito.when(mockProject.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, "8080", null, port, "TCP", new IntOrString(port))));
service.setSpec(spec);
Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));
Route route = new Route();
RouteSpec routeSpec = new RouteSpec();
routeSpec.setPort(new RoutePort(new IntOrString("8080")));
routeSpec.setTo(new RouteTargetReference("a", "servicename", 1));
routeSpec.setHost("testhost");
route.setSpec(routeSpec);
Mockito.when(mockProject.routes()).thenReturn(mockRoutes);
Mockito.when(mockRoutes.get()).thenReturn(Collections.singletonList(route));
previewUrlCommandProvisioner.provision(env, mockProject);
assertTrue(env.getCommands().get(0).getAttributes().containsKey("previewUrl"));
assertEquals(env.getCommands().get(0).getAttributes().get("previewUrl"), "testhost");
assertTrue(env.getWarnings().isEmpty());
}
use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project che-server by eclipse-che.
the class OpenShiftPreviewUrlCommandProvisionerTest method shouldDoNothingWhenCommandsWithoutPreviewUrlDefined.
@Test
public void shouldDoNothingWhenCommandsWithoutPreviewUrlDefined() throws InfrastructureException {
List<CommandImpl> commands = Arrays.asList(new CommandImpl("a", "a", "a"), new CommandImpl("b", "b", "b"));
OpenShiftEnvironment env = OpenShiftEnvironment.builder().setCommands(new ArrayList<>(commands)).build();
Mockito.when(mockProject.routes()).thenReturn(mockRoutes);
Mockito.when(mockProject.services()).thenReturn(mockServices);
previewUrlCommandProvisioner.provision(env, mockProject);
assertTrue(commands.containsAll(env.getCommands()));
assertTrue(env.getCommands().containsAll(commands));
assertTrue(env.getWarnings().isEmpty());
}
use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project che-server by eclipse-che.
the class OpenShiftPreviewUrlCommandProvisionerTest method shouldDoNothingWhenCantFindRouteForPreviewUrl.
@Test
public void shouldDoNothingWhenCantFindRouteForPreviewUrl() throws InfrastructureException {
int port = 8080;
List<CommandImpl> commands = Collections.singletonList(new CommandImpl("a", "a", "a", new PreviewUrlImpl(port, null), Collections.emptyMap()));
OpenShiftEnvironment env = OpenShiftEnvironment.builder().setCommands(new ArrayList<>(commands)).build();
Mockito.when(mockProject.services()).thenReturn(mockServices);
Service service = new Service();
ServiceSpec spec = new ServiceSpec();
spec.setPorts(Collections.singletonList(new ServicePort(null, "a", null, port, "TCP", new IntOrString(port))));
service.setSpec(spec);
Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));
Mockito.when(mockProject.routes()).thenReturn(mockRoutes);
Mockito.when(mockRoutes.get()).thenReturn(Collections.emptyList());
previewUrlCommandProvisioner.provision(env, mockProject);
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);
}
use of org.eclipse.che.api.workspace.server.model.impl.CommandImpl in project che-server by eclipse-che.
the class OpenShiftPreviewUrlExposerTest method shouldProvisionServiceAndRouteWhenNotFound.
@Test
public void shouldProvisionServiceAndRouteWhenNotFound() throws InternalInfrastructureException {
final int PORT = 8080;
final String SERVER_PORT_NAME = "server-" + PORT;
CommandImpl command = new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());
OpenShiftEnvironment env = OpenShiftEnvironment.builder().setCommands(singletonList(new CommandImpl(command))).setRoutes(new HashMap<>()).setServices(new HashMap<>()).build();
previewUrlEndpointsProvisioner.expose(env);
assertEquals(env.getRoutes().size(), 1);
assertEquals(env.getServices().size(), 1);
Service provisionedService = env.getServices().values().iterator().next();
ServicePort provisionedServicePort = provisionedService.getSpec().getPorts().get(0);
assertEquals(provisionedServicePort.getName(), SERVER_PORT_NAME);
assertEquals(provisionedServicePort.getPort().intValue(), PORT);
Route provisionedRoute = env.getRoutes().values().iterator().next();
assertEquals(provisionedRoute.getSpec().getTo().getName(), provisionedService.getMetadata().getName());
assertEquals(provisionedRoute.getSpec().getPort().getTargetPort().getStrVal(), SERVER_PORT_NAME);
}
Aggregations