@Override protected MixedOperation<Route, RouteList, DoneableRoute, Resource<Route, DoneableRoute>> operation() { return client.routes(); }
@Override public String getURL(Service service, String portName, String namespace, KubernetesClient client) { String serviceName = service.getMetadata().getName(); ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName); if(port != null && port.getName() != null && isOpenShift(client)) { try { String serviceProtocol = port.getProtocol(); OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class); Route route = openShiftClient.routes().inNamespace(namespace).withName(service.getMetadata().getName()).get(); if (route != null) { return (serviceProtocol + "://" + route.getSpec().getHost()).toLowerCase(); } } catch (KubernetesClientException e) { if(e.getCode() == HttpURLConnection.HTTP_FORBIDDEN) { logger.warn("Could not lookup route:" + serviceName + " in namespace:"+ namespace +", due to: " + e.getMessage()); } } } return null; }
@Override protected void mocker(OpenShiftClient mockClient, MixedOperation op) { when(mockClient.routes()).thenReturn(op); }
public Endpoint getKeycloakEndpoint() { OpenShiftClient openShift = client.adapt(OpenShiftClient.class); Route route = openShift.routes().inNamespace(globalNamespace).withName("keycloak").get(); Endpoint endpoint = new Endpoint(route.getSpec().getHost(), 443); log.info("Testing endpoint : " + endpoint); if (TestUtils.resolvable(endpoint)) { return endpoint; } else { log.info("Endpoint didn't resolve, falling back to service endpoint"); return getEndpoint("standard-authservice", "https"); } }
protected void createRoutes(Collection<HasMetadata> collection) { String routeDomainPostfix = this.routeDomain; Log log = getLog(); String namespace = clusterAccess.getNamespace(); // lets get the routes first to see if we should bother try { OpenShiftClient openshiftClient = applyService.getOpenShiftClient(); if (openshiftClient == null) { return; } RouteList routes = openshiftClient.routes().inNamespace(namespace).list(); if (routes != null) { routes.getItems(); } } catch (Exception e) { log.warn("Cannot load OpenShift Routes; maybe not connected to an OpenShift platform? " + e, e); return; } List<Route> routes = new ArrayList<>(); for (Object object : collection) { if (object instanceof Service) { Service service = (Service) object; Route route = createRouteForService(routeDomainPostfix, namespace, service); if (route != null) { routes.add(route); } } } collection.addAll(routes); }
/** * Creates specified route. * * @param route route to create * @return created route * @throws InfrastructureException when any exception occurs */ public Route create(Route route) throws InfrastructureException { putLabel(route, CHE_WORKSPACE_ID_LABEL, workspaceId); try { return clientFactory.createOC(workspaceId).routes().inNamespace(namespace).create(route); } catch (KubernetesClientException e) { throw new KubernetesInfrastructureException(e); } }
@Override public Endpoint getExternalEndpoint(String endpointName) { OpenShiftClient openShift = client.adapt(OpenShiftClient.class); Route route = openShift.routes().inNamespace(globalNamespace).withName(endpointName).get(); Endpoint endpoint = new Endpoint(route.getSpec().getHost(), 443); log.info("Testing endpoint : " + endpoint); if (TestUtils.resolvable(endpoint)) { return endpoint; } else { log.info("Endpoint didn't resolve, falling back to service endpoint"); String port; switch (endpointName) { case "messaging": port = "amqps"; break; case "console": port = "https"; break; case "mqtt": port = "secure-mqtt"; break; default: throw new IllegalStateException(String.format("Endpoint '%s' doesn't exist.", endpointName)); } return getEndpoint(endpointName, port); } } }
/** * Deletes all existing routes. * * @throws InfrastructureException when any exception occurs */ public void delete() throws InfrastructureException { try { clientFactory .createOC(workspaceId) .routes() .inNamespace(namespace) .withLabel(CHE_WORKSPACE_ID_LABEL, workspaceId) .delete(); } catch (KubernetesClientException e) { throw new KubernetesInfrastructureException(e); } } }
public void applyRoute(Route entity, String sourceName) { OpenShiftClient openShiftClient = getOpenShiftClient(); if (openShiftClient != null) { String id = getName(entity); Objects.requireNonNull(id, "No name for " + entity + " " + sourceName); String namespace = KubernetesHelper.getNamespace(entity); if (StringUtils.isBlank(namespace)) { namespace = getNamespace(); } Route route = openShiftClient.routes().inNamespace(namespace).withName(id).get(); if (route == null) { try { log.info("Creating Route " + namespace + ":" + id + " " + (entity.getSpec() != null ? "host: " + entity.getSpec().getHost() : "No Spec !")); openShiftClient.routes().inNamespace(namespace).create(entity); } catch (Exception e) { onApplyError("Failed to create Route from " + sourceName + ". " + e + ". " + entity, e); } } } }
@Override public void trigger(OpenShiftClient client, OpenShiftRuntimeConfig runtimeConfig) { String prjName = runtimeConfig.getProjectName(); RouteList routeList = client.getDelegate().routes().inNamespace(prjName).list(); String routerHost = System.getProperty(OpenShiftNameServiceClientListener.class.getName() + ".routerHost", "10.34.75.115"); OpenShiftNameService.setRoutes(routeList, routerHost); }
@Override public void trigger(OpenShiftClient client, OpenShiftRuntimeConfig runtimeConfig) { String prjName = runtimeConfig.getProjectName(); RouteList routeList = client.getDelegate().routes().inNamespace(prjName).list(); String routerHost = System.getProperty(OpenShiftNameServiceClientListener.class.getName() + ".routerHost", "10.34.75.115"); OpenShiftNameService.setRoutes(routeList, routerHost); }
/** * Returns all existing routes. * * @throws InfrastructureException when any exception occurs */ public List<Route> get() throws InfrastructureException { try { return clientFactory .createOC(workspaceId) .routes() .inNamespace(namespace) .withLabel(CHE_WORKSPACE_ID_LABEL, workspaceId) .list() .getItems(); } catch (KubernetesClientException e) { throw new KubernetesInfrastructureException(e); } }
RouteList routes = openShiftClient.routes().inNamespace(namespace).list(); for (Route route : routes.getItems()) { RouteSpec spec = route.getSpec();
@Test @OpenShiftResource("classpath:hello-route-2.yaml") public void should_register_extra_route() { final RouteList routes = openShiftClient.routes().list(); assertThat(routes.getItems()) .hasSize(2) .extracting(Route::getMetadata) .extracting(ObjectMeta::getName) .containsExactlyInAnyOrder("hello-world", "hello-world-2"); }
@Override public String getURL(Service service, String portName, String namespace, KubernetesClient client) { String serviceName = service.getMetadata().getName(); ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName); if(port != null && port.getName() != null && isOpenShift(client)) { try { String serviceProtocol = port.getProtocol(); OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class); Route route = openShiftClient.routes().inNamespace(namespace).withName(service.getMetadata().getName()).get(); if (route != null) { return (serviceProtocol + "://" + route.getSpec().getHost()).toLowerCase(); } } catch (KubernetesClientException e) { if(e.getCode() == HttpURLConnection.HTTP_FORBIDDEN) { logger.warn("Could not lookup route:" + serviceName + " in namespace:"+ namespace +", due to: " + e.getMessage()); } } } return null; }
openShiftClient.routes().inNamespace(sessionNamespace).delete(); } catch (KubernetesClientException e) { errors.add(e);
Route route = openShiftClient.routes().inNamespace(actualNamespace).withName(serviceName).get(); if (route != null) { return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
public OpenShiftRuntimeEndpoint getRuntimeEndpoint(String id) throws OpenShiftClientException { try { OpenShiftRuntimeId runtimeId = OpenShiftRuntimeId.fromString(id); String prjName = runtimeId.project(); String svcName = runtimeId.service(); OpenShiftRuntimeEndpoint endpoint = new OpenShiftRuntimeEndpoint(); Route route = delegate.routes().inNamespace(prjName).withName(svcName).get(); if (route != null) { RouteSpec routeSpec = route.getSpec(); endpoint.setProtocol(routeSpec.getTls() != null ? "https" : "http"); endpoint.setHost(routeSpec.getHost()); RoutePort routePort = routeSpec.getPort(); if (routePort != null) { IntOrString targetPort = routePort.getTargetPort(); if (targetPort != null) { endpoint.setPort(targetPort.getIntVal()); } } } return endpoint; } catch (Throwable t) { throw new OpenShiftClientException(t.getMessage(), t); } }