public RouteSpecBuilder(){ this(new RouteSpec()); } public RouteSpecBuilder( RouteSpecFluent<?> fluent ){
public RouteSpecBuilder(RouteSpecFluent<?> fluent,RouteSpec instance,Boolean validationEnabled){ this.fluent = fluent; fluent.withAlternateBackends(instance.getAlternateBackends()); fluent.withHost(instance.getHost()); fluent.withPath(instance.getPath()); fluent.withPort(instance.getPort()); fluent.withTls(instance.getTls()); fluent.withTo(instance.getTo()); this.validationEnabled = validationEnabled; } public RouteSpecBuilder(RouteSpec instance){
@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; }
for (Route route : routes.getItems()) { RouteSpec spec = route.getSpec(); if (spec != null && spec.getTo() != null && "Service".equalsIgnoreCase(spec.getTo().getKind()) && serviceName.equalsIgnoreCase(spec.getTo().getName())) { String host = spec.getHost(); if (host != null && host.length() > 0) { if (spec.getTls() != null) { return "https://" + host;
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); } }
if (route != null) { statusBuilder.withExternalPorts(Collections.singletonMap(exposeSpec.getRouteServicePort(), 443)); statusBuilder.withExternalHost(route.getSpec().getHost()); if (exposeSpec.getRouteTlsTermination().equals(TlsTermination.passthrough)) { Secret certSecret = client.secrets().inNamespace(namespace).withName(KubeUtil.getExternalCertSecretName(endpointInfo.endpointSpec.getService(), addressSpace)).get(); statusBuilder.withCert(route.getSpec().getTls().getCertificate());
private Route createRouteForService(String routeDomainPostfix, String namespace, Service service) { Route route = null; String id = KubernetesHelper.getName(service); if (StringUtils.isNotBlank(id) && hasExactlyOneService(service, id)) { route = new Route(); ObjectMeta routeMeta = KubernetesHelper.getOrCreateMetadata(route); routeMeta.setName(id); routeMeta.setNamespace(namespace); RouteSpec routeSpec = new RouteSpec(); RouteTargetReference objectRef = new RouteTargetReferenceBuilder().withName(id).build(); //objectRef.setNamespace(namespace); routeSpec.setTo(objectRef); if (StringUtils.isNotBlank(routeDomainPostfix)) { routeSpec.setHost(prepareHostForRoute(routeDomainPostfix, id)); } else { routeSpec.setHost(""); } route.setSpec(routeSpec); String json; try { json = ResourceUtil.toJson(route); } catch (JsonProcessingException e) { json = e.getMessage() + ". object: " + route; } log.debug("Created route: " + json); } return route; }
private static String getKeycloakAuthUrl(NamespacedOpenShiftClient client, Map<String, String> env) { Route keycloakRoute = client.routes().withName("keycloak").get(); String keycloakOauthUrl = null; if (keycloakRoute != null && !keycloakRoute.getSpec().getHost().contains("127.0.0.1")) { keycloakOauthUrl = String.format("https://%s/auth", keycloakRoute.getSpec().getHost()); } else { keycloakOauthUrl = getEnv(env, "STANDARD_AUTHSERVICE_SERVICE_HOST").map(ip -> "https://" + ip + ":8443/auth").orElse(null); } return keycloakOauthUrl; }
public static Route createRouteForService(String routeDomainPostfix, String namespace, Service service, Logger log) { Route route = null; String id = KubernetesHelper.getName(service); if (Strings.isNotBlank(id) && shouldCreateRouteForService(log, service, id)) { route = new Route(); String routeId = id; KubernetesHelper.setName(route, namespace, routeId); RouteSpec routeSpec = new RouteSpec(); RouteTargetReference objectRef = new RouteTargetReferenceBuilder().withName(id).build(); //objectRef.setNamespace(namespace); routeSpec.setTo(objectRef); if (Strings.isNotBlank(routeDomainPostfix)) { // Let Openshift determine the route host when the domain is not set String host = Strings.stripSuffix(Strings.stripSuffix(id, "-service"), "."); String namespaceSuffix = "-" + namespace; routeSpec.setHost(host + namespaceSuffix + "." + Strings.stripPrefix(routeDomainPostfix, ".")); } route.setSpec(routeSpec); String json = null; try { json = KubernetesHelper.toJson(route); } catch (JsonProcessingException e) { json = e.getMessage() + ". object: " + route; } } return route; }
public RouteSpecBuilder(RouteSpec instance,Boolean validationEnabled){ this.fluent = this; this.withAlternateBackends(instance.getAlternateBackends()); this.withHost(instance.getHost()); this.withPath(instance.getPath()); this.withPort(instance.getPort()); this.withTls(instance.getTls()); this.withTo(instance.getTo()); this.validationEnabled = validationEnabled; }
public static void setRoutes(RouteList routeList, String routerHost) { InetAddress routerAddr; try { routerAddr = routerHost == null ? null : InetAddress.getByName(routerHost); } catch (UnknownHostException e) { throw new IllegalArgumentException("Invalid IP for router host", e); } if (routeList != null) { synchronized (ROUTING) { for (Route route : routeList.getItems()) { String host = route.getSpec().getHost(); if (routerAddr != null) { System.out.println(String.format("Adding route (router -> host): %s -> %s", routerHost, host)); ROUTING.put(host, routerAddr); } else { ROUTING.remove(host); } } } } }
public RouteSpecBuilder( RouteSpecFluent<?> fluent ){ this(fluent, new RouteSpec()); } public RouteSpecBuilder( RouteSpecFluent<?> fluent , RouteSpec instance ){
private void validateRoutesMatchServices(OpenShiftEnvironment env) throws ValidationException { Set<String> recipeServices = env.getServices() .values() .stream() .map(s -> s.getMetadata().getName()) .collect(Collectors.toSet()); for (Route route : env.getRoutes().values()) { if (route.getSpec() == null || route.getSpec().getTo() == null || !route.getSpec().getTo().getKind().equals(SERVICE_KIND)) { continue; } String serviceName = route.getSpec().getTo().getName(); if (!recipeServices.contains(serviceName)) { throw new ValidationException( String.format( "Route '%s' refers to Service '%s'. Routes must refer to Services included in recipe", route.getMetadata().getName(), serviceName)); } } } }
public static void setRoutes(RouteList routeList, String routerHost) { InetAddress routerAddr; try { routerAddr = routerHost == null ? null : InetAddress.getByName(routerHost); } catch (UnknownHostException e) { throw new IllegalArgumentException("Invalid IP for router host", e); } if (routeList != null) { synchronized (ROUTING) { for (Route route : routeList.getItems()) { String host = route.getSpec().getHost(); if (routerAddr != null) { System.out.println(String.format("Adding route (router -> host): %s -> %s", routerHost, host)); ROUTING.put(host, routerAddr); } else { ROUTING.remove(host); } } } } }