public static String resolvePortFromEnvVarOrSystemProperty(String serviceName, String portName) { String envVarName = toServicePortEnvironmentVariable(serviceName, portName); return getEnvVarOrSystemProperty(envVarName, ""); }
public static String getURLFromIngressList(List<Ingress> ingressList, String namespace, String serviceName, ServicePort port) { for(Ingress item : ingressList) { String ns = getNamespace(item); if(Objects.equals(ns, namespace) && item.getSpec() != null) { return getURLFromIngressSpec(item.getSpec(), serviceName, port); } } return null; }
public static String resolveHostFromEnvVarOrSystemProperty(String serviceName) { return getEnvVarOrSystemProperty(toServiceHostEnvironmentVariable(serviceName), ""); }
public static String resolveProtocolFromEnvVarOrSystemProperty(String serviceName, String servicePort) { return getEnvVarOrSystemProperty(toEnvVariable(serviceName + PORT_SUFFIX + "_" + servicePort + PROTO_SUFFIX), DEFAULT_PROTO); }
@Override public String getURL(Service service, String portName, String namespace, KubernetesClient client) { String serviceHost = URLFromServiceUtil.resolveHostFromEnvVarOrSystemProperty(service.getMetadata().getName()); String servicePort = URLFromServiceUtil.resolvePortFromEnvVarOrSystemProperty(service.getMetadata().getName(), ""); String serviceProtocol = URLFromServiceUtil.resolveProtocolFromEnvVarOrSystemProperty(service.getSpec().getPorts().iterator().next().getProtocol(), ""); if(!serviceHost.isEmpty() && !servicePort.isEmpty() && !serviceProtocol.isEmpty()) { return serviceProtocol + "://" + serviceHost + ":" + servicePort; } else { String answer = URLFromServiceUtil.getOrCreateAnnotations(service).get(ANNOTATION_EXPOSE_URL); if(answer != null && !answer.isEmpty()) { return answer; } } return null; }
public String getURL(Service service, String portName, String namespace, KubernetesClient client) { ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName); String serviceProto = port.getProtocol(); NodePortUrlComponents urlComponents = null; Integer nodePort = port.getNodePort(); if(nodePort != null) { try { NodeList nodeList = client.nodes().list(); if(nodeList != null && nodeList.getItems() != null) { for(Node item : nodeList.getItems()) { urlComponents = getUrlComponentsFromNodeList(item.getStatus(), nodePort); if(urlComponents != null) { break; } } } } catch (KubernetesClientException exception) { logger.warn("Could not find a node! " + exception); } } return urlComponents != null ? (serviceProto + "://" + urlComponents.getClusterIP() + ":" + urlComponents.getPortNumber()).toLowerCase() : null; }
public static String getURLFromIngressRules(List<HTTPIngressPath> paths, IngressSpec spec, String serviceName, ServicePort port, IngressRule rule) { for(HTTPIngressPath path : paths) { IngressBackend backend = path.getBackend(); if(backend != null) { String backendServiceName = backend.getServiceName(); if(serviceName.equals(backendServiceName) && portsMatch(port, backend.getServicePort())) { String pathPostFix = path.getPath(); if(spec.getTls() != null) { return getURLFromTLSHost(rule, pathPostFix); } String answer = rule.getHost(); if(answer != null && !answer.isEmpty()) { pathPostFix = fixPathPostFixIfEmpty(pathPostFix); return "http://" + URLUtils.pathJoin(answer, pathPostFix); } } } } return null; }
@Override public String getURL(Service service, String portName, String namespace, KubernetesClient client) { ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName); String serviceName = service.getMetadata().getName(); if(port == null) { throw new RuntimeException("Couldn't find port: " + portName + " for service " + service.getMetadata().getName()); } IngressList ingresses = client.extensions().ingresses().inNamespace(namespace).list(); if(ingresses != null && !ingresses.getItems().isEmpty()) { return URLFromServiceUtil.getURLFromIngressList(ingresses.getItems(), namespace, serviceName, port); } return null; }
public static String getURLFromTLSHost(IngressRule rule, String pathPostFix) { String host = rule.getHost(); if (!host.isEmpty()) { pathPostFix = fixPathPostFixIfEmpty(pathPostFix); return "https://" + URLUtils.pathJoin(host, pathPostFix); } return null; }
public static Map<String, String> getOrCreateAnnotations(HasMetadata entity) { ObjectMeta metadata = getOrCreateMetadata(entity); Map<String, String> answer = metadata.getAnnotations(); if (answer == null) { // use linked so the annotations can be in the FIFO order answer = new LinkedHashMap<>(); metadata.setAnnotations(answer); } return answer; }
public static String getURLFromIngressSpec(IngressSpec spec, String serviceName, ServicePort port) { List<IngressRule> ingressRules = spec.getRules(); if(ingressRules != null && !ingressRules.isEmpty()) { for(IngressRule rule : ingressRules) { HTTPIngressRuleValue http = rule.getHttp(); if(http != null && http.getPaths() != null) { return getURLFromIngressRules(http.getPaths(), spec, serviceName, port, rule); } } } return null; }
@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 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; }