use of io.fabric8.kubernetes.api.model.networking.v1.IngressTLS in project fabric8 by jboss-fuse.
the class KubernetesHelper method getServiceURL.
/**
* Returns the URL to access the service; using the environment variables, routes
* or service clusterIP address
*
* @throws IllegalArgumentException if the URL cannot be found for the serviceName and namespace
*/
public static String getServiceURL(KubernetesClient client, String serviceName, String serviceNamespace, String serviceProtocol, String servicePortName, boolean serviceExternal) {
Service srv = null;
String serviceHost = KubernetesServices.serviceToHostOrBlank(serviceName);
String servicePort = KubernetesServices.serviceToPortOrBlank(serviceName, servicePortName);
String serviceProto = serviceProtocol != null ? serviceProtocol : KubernetesServices.serviceToProtocol(serviceName, servicePort);
// Use specified or fallback namespace.
String actualNamespace = Strings.isNotBlank(serviceNamespace) ? serviceNamespace : client.getNamespace();
// 1. Inside Kubernetes: Services as ENV vars
if (!serviceExternal && Strings.isNotBlank(serviceHost) && Strings.isNotBlank(servicePort) && Strings.isNotBlank(serviceProtocol)) {
return serviceProtocol + "://" + serviceHost + ":" + servicePort;
// 2. Anywhere: When namespace is passed System / Env var. Mostly needed for integration tests.
} else if (Strings.isNotBlank(actualNamespace)) {
try {
srv = client.services().inNamespace(actualNamespace).withName(serviceName).get();
} catch (Exception e) {
LOGGER.warn("Could not lookup service:" + serviceName + " in namespace:" + actualNamespace + ", due to: " + e.getMessage());
}
}
if (srv == null) {
// lets try use environment variables
String hostAndPort = Systems.getServiceHostAndPort(serviceName, "", "");
if (!hostAndPort.startsWith(":")) {
return serviceProto + "://" + hostAndPort;
}
}
if (srv == null) {
throw new IllegalArgumentException("No kubernetes service could be found for name: " + serviceName + " in namespace: " + actualNamespace);
}
String answer = getOrCreateAnnotations(srv).get(Annotations.Service.EXPOSE_URL);
if (Strings.isNotBlank(answer)) {
return answer;
}
try {
if (Strings.isNullOrBlank(servicePortName) && isOpenShift(client)) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
Route route = openShiftClient.routes().inNamespace(actualNamespace).withName(serviceName).get();
if (route != null) {
return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
}
}
} catch (KubernetesClientException e) {
if (e.getCode() == 403) {
LOGGER.warn("Could not lookup route:" + serviceName + " in namespace:" + actualNamespace + ", due to: " + e.getMessage());
} else {
throw e;
}
}
ServicePort port = findServicePortByName(srv, servicePortName);
if (port == null) {
throw new RuntimeException("Couldn't find port: " + servicePortName + " for service:" + serviceName);
}
String clusterIP = srv.getSpec().getClusterIP();
if ("None".equals(clusterIP)) {
throw new IllegalStateException("Service: " + serviceName + " in namespace:" + serviceNamespace + "is head-less. Search for endpoints instead.");
}
Integer portNumber = port.getPort();
if (Strings.isNullOrBlank(clusterIP)) {
IngressList ingresses = client.extensions().ingresses().inNamespace(serviceNamespace).list();
if (ingresses != null) {
List<Ingress> items = ingresses.getItems();
if (items != null) {
for (Ingress item : items) {
String ns = getNamespace(item);
if (Objects.equal(serviceNamespace, ns)) {
IngressSpec spec = item.getSpec();
if (spec != null) {
List<IngressRule> rules = spec.getRules();
List<IngressTLS> tls = spec.getTls();
if (rules != null) {
for (IngressRule rule : rules) {
HTTPIngressRuleValue http = rule.getHttp();
if (http != null) {
List<HTTPIngressPath> paths = http.getPaths();
if (paths != null) {
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 (tls != null) {
for (IngressTLS tlsHost : tls) {
List<String> hosts = tlsHost.getHosts();
if (hosts != null) {
for (String host : hosts) {
if (Strings.isNotBlank(host)) {
if (Strings.isNullOrBlank(pathPostfix)) {
pathPostfix = "/";
}
return "https://" + URLUtils.pathJoin(host, pathPostfix);
}
}
}
}
}
answer = rule.getHost();
if (Strings.isNotBlank(answer)) {
if (Strings.isNullOrBlank(pathPostfix)) {
pathPostfix = "/";
}
return "http://" + URLUtils.pathJoin(answer, pathPostfix);
}
}
}
}
}
}
}
}
}
}
}
}
}
// lets try use the status on GKE
ServiceStatus status = srv.getStatus();
if (status != null) {
LoadBalancerStatus loadBalancerStatus = status.getLoadBalancer();
if (loadBalancerStatus != null) {
List<LoadBalancerIngress> loadBalancerIngresses = loadBalancerStatus.getIngress();
if (loadBalancerIngresses != null) {
for (LoadBalancerIngress loadBalancerIngress : loadBalancerIngresses) {
String ip = loadBalancerIngress.getIp();
if (Strings.isNotBlank(ip)) {
clusterIP = ip;
break;
}
}
}
}
}
}
if (Strings.isNullOrBlank(clusterIP)) {
// on vanilla kubernetes we can use nodePort to access things externally
boolean found = false;
Integer nodePort = port.getNodePort();
if (nodePort != null) {
try {
NodeList nodeList = client.nodes().list();
if (nodeList != null) {
List<Node> items = nodeList.getItems();
if (items != null) {
for (Node item : items) {
NodeStatus status = item.getStatus();
if (!found && status != null) {
List<NodeAddress> addresses = status.getAddresses();
if (addresses != null) {
for (NodeAddress address : addresses) {
String ip = address.getAddress();
if (Strings.isNotBlank(ip)) {
clusterIP = ip;
portNumber = nodePort;
found = true;
break;
}
}
}
}
if (!found) {
NodeSpec spec = item.getSpec();
if (spec != null) {
clusterIP = spec.getExternalID();
if (Strings.isNotBlank(clusterIP)) {
portNumber = nodePort;
break;
}
}
}
}
}
}
} catch (Exception e) {
// ignore could not find a node!
LOG.warn("Could not find a node!: " + e, e);
}
}
}
return (serviceProto + "://" + clusterIP + ":" + portNumber).toLowerCase();
}
use of io.fabric8.kubernetes.api.model.networking.v1.IngressTLS in project fabric8 by fabric8io.
the class KubernetesHelper method getServiceURL.
/**
* Returns the URL to access the service; using the environment variables, routes
* or service clusterIP address
*
* @throws IllegalArgumentException if the URL cannot be found for the serviceName and namespace
*/
public static String getServiceURL(KubernetesClient client, String serviceName, String serviceNamespace, String serviceProtocol, String servicePortName, boolean serviceExternal) {
Service srv = null;
String serviceHost = KubernetesServices.serviceToHostOrBlank(serviceName);
String servicePort = KubernetesServices.serviceToPortOrBlank(serviceName, servicePortName);
String serviceProto = serviceProtocol != null ? serviceProtocol : KubernetesServices.serviceToProtocol(serviceName, servicePort);
// Use specified or fallback namespace.
String actualNamespace = Strings.isNotBlank(serviceNamespace) ? serviceNamespace : client.getNamespace();
// 1. Inside Kubernetes: Services as ENV vars
if (!serviceExternal && Strings.isNotBlank(serviceHost) && Strings.isNotBlank(servicePort) && Strings.isNotBlank(serviceProtocol)) {
return serviceProtocol + "://" + serviceHost + ":" + servicePort;
// 2. Anywhere: When namespace is passed System / Env var. Mostly needed for integration tests.
} else if (Strings.isNotBlank(actualNamespace)) {
try {
srv = client.services().inNamespace(actualNamespace).withName(serviceName).get();
} catch (Exception e) {
LOGGER.warn("Could not lookup service:" + serviceName + " in namespace:" + actualNamespace + ", due to: " + e.getMessage());
}
}
if (srv == null) {
// lets try use environment variables
String hostAndPort = Systems.getServiceHostAndPort(serviceName, "", "");
if (!hostAndPort.startsWith(":")) {
return serviceProto + "://" + hostAndPort;
}
}
if (srv == null) {
throw new IllegalArgumentException("No kubernetes service could be found for name: " + serviceName + " in namespace: " + actualNamespace);
}
String answer = getOrCreateAnnotations(srv).get(Annotations.Service.EXPOSE_URL);
if (Strings.isNotBlank(answer)) {
return answer;
}
try {
if (Strings.isNullOrBlank(servicePortName) && isOpenShift(client)) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
Route route = openShiftClient.routes().inNamespace(actualNamespace).withName(serviceName).get();
if (route != null) {
return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
}
}
} catch (KubernetesClientException e) {
if (e.getCode() == 403) {
LOGGER.warn("Could not lookup route:" + serviceName + " in namespace:" + actualNamespace + ", due to: " + e.getMessage());
} else {
throw e;
}
}
ServicePort port = findServicePortByName(srv, servicePortName);
if (port == null) {
throw new RuntimeException("Couldn't find port: " + servicePortName + " for service:" + serviceName);
}
String clusterIP = srv.getSpec().getClusterIP();
if ("None".equals(clusterIP)) {
throw new IllegalStateException("Service: " + serviceName + " in namespace:" + serviceNamespace + "is head-less. Search for endpoints instead.");
}
Integer portNumber = port.getPort();
if (Strings.isNullOrBlank(clusterIP)) {
IngressList ingresses = client.extensions().ingresses().inNamespace(serviceNamespace).list();
if (ingresses != null) {
List<Ingress> items = ingresses.getItems();
if (items != null) {
for (Ingress item : items) {
String ns = getNamespace(item);
if (Objects.equal(serviceNamespace, ns)) {
IngressSpec spec = item.getSpec();
if (spec != null) {
List<IngressRule> rules = spec.getRules();
List<IngressTLS> tls = spec.getTls();
if (rules != null) {
for (IngressRule rule : rules) {
HTTPIngressRuleValue http = rule.getHttp();
if (http != null) {
List<HTTPIngressPath> paths = http.getPaths();
if (paths != null) {
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 (tls != null) {
for (IngressTLS tlsHost : tls) {
List<String> hosts = tlsHost.getHosts();
if (hosts != null) {
for (String host : hosts) {
if (Strings.isNotBlank(host)) {
if (Strings.isNullOrBlank(pathPostfix)) {
pathPostfix = "/";
}
return "https://" + URLUtils.pathJoin(host, pathPostfix);
}
}
}
}
}
answer = rule.getHost();
if (Strings.isNotBlank(answer)) {
if (Strings.isNullOrBlank(pathPostfix)) {
pathPostfix = "/";
}
return "http://" + URLUtils.pathJoin(answer, pathPostfix);
}
}
}
}
}
}
}
}
}
}
}
}
}
// lets try use the status on GKE
ServiceStatus status = srv.getStatus();
if (status != null) {
LoadBalancerStatus loadBalancerStatus = status.getLoadBalancer();
if (loadBalancerStatus != null) {
List<LoadBalancerIngress> loadBalancerIngresses = loadBalancerStatus.getIngress();
if (loadBalancerIngresses != null) {
for (LoadBalancerIngress loadBalancerIngress : loadBalancerIngresses) {
String ip = loadBalancerIngress.getIp();
if (Strings.isNotBlank(ip)) {
clusterIP = ip;
break;
}
}
}
}
}
}
if (Strings.isNullOrBlank(clusterIP)) {
// on vanilla kubernetes we can use nodePort to access things externally
boolean found = false;
Integer nodePort = port.getNodePort();
if (nodePort != null) {
try {
NodeList nodeList = client.nodes().list();
if (nodeList != null) {
List<Node> items = nodeList.getItems();
if (items != null) {
for (Node item : items) {
NodeStatus status = item.getStatus();
if (!found && status != null) {
List<NodeAddress> addresses = status.getAddresses();
if (addresses != null) {
for (NodeAddress address : addresses) {
String ip = address.getAddress();
if (Strings.isNotBlank(ip)) {
clusterIP = ip;
portNumber = nodePort;
found = true;
break;
}
}
}
}
if (!found) {
NodeSpec spec = item.getSpec();
if (spec != null) {
clusterIP = spec.getExternalID();
if (Strings.isNotBlank(clusterIP)) {
portNumber = nodePort;
break;
}
}
}
}
}
}
} catch (Exception e) {
// ignore could not find a node!
LOG.warn("Could not find a node!: " + e, e);
}
}
}
return (serviceProto + "://" + clusterIP + ":" + portNumber).toLowerCase();
}
use of io.fabric8.kubernetes.api.model.networking.v1.IngressTLS in project fabric8-maven-plugin by fabric8io.
the class ServiceUrlUtil method getServiceURL.
/**
* Returns the URL to access the service; using the environment variables, routes
* or service clusterIP address
*
* @throws IllegalArgumentException if the URL cannot be found for the serviceName and namespace
*/
public static String getServiceURL(KubernetesClient client, String serviceName, String serviceNamespace, String serviceProtocol, boolean serviceExternal) {
Service srv = null;
String serviceHost = serviceToHostOrBlank(serviceName);
String servicePort = serviceToPortOrBlank(serviceName);
String serviceProto = serviceProtocol != null ? serviceProtocol : serviceToProtocol(serviceName, servicePort);
// Use specified or fallback namespace.
String actualNamespace = StringUtils.isNotBlank(serviceNamespace) ? serviceNamespace : client.getNamespace();
// 1. Inside Kubernetes: Services as ENV vars
if (!serviceExternal && StringUtils.isNotBlank(serviceHost) && StringUtils.isNotBlank(servicePort) && StringUtils.isNotBlank(serviceProtocol)) {
return serviceProtocol + "://" + serviceHost + ":" + servicePort;
// 2. Anywhere: When namespace is passed System / Env var. Mostly needed for integration tests.
} else if (StringUtils.isNotBlank(actualNamespace)) {
srv = client.services().inNamespace(actualNamespace).withName(serviceName).get();
}
if (srv == null) {
// lets try use environment variables
String hostAndPort = getServiceHostAndPort(serviceName, "", "");
if (!hostAndPort.startsWith(":")) {
return serviceProto + "://" + hostAndPort;
}
}
if (srv == null) {
throw new IllegalArgumentException("No kubernetes service could be found for name: " + serviceName + " in namespace: " + actualNamespace);
}
String answer = KubernetesHelper.getOrCreateAnnotations(srv).get(Fabric8Annotations.SERVICE_EXPOSE_URL.toString());
if (StringUtils.isNotBlank(answer)) {
return answer;
}
if (OpenshiftHelper.isOpenShift(client)) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
Route route = openShiftClient.routes().inNamespace(actualNamespace).withName(serviceName).get();
if (route != null) {
return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
}
}
ServicePort port = findServicePortByName(srv, null);
if (port == null) {
throw new RuntimeException("Couldn't find port: " + null + " for service:" + serviceName);
}
String clusterIP = srv.getSpec().getClusterIP();
if ("None".equals(clusterIP)) {
throw new IllegalStateException("Service: " + serviceName + " in namespace:" + serviceNamespace + "is head-less. Search for endpoints instead.");
}
Integer portNumber = port.getPort();
if (StringUtils.isBlank(clusterIP)) {
IngressList ingresses = client.extensions().ingresses().inNamespace(serviceNamespace).list();
if (ingresses != null) {
List<Ingress> items = ingresses.getItems();
if (items != null) {
for (Ingress item : items) {
String ns = KubernetesHelper.getNamespace(item);
if (Objects.equal(serviceNamespace, ns)) {
IngressSpec spec = item.getSpec();
if (spec != null) {
List<IngressRule> rules = spec.getRules();
List<IngressTLS> tls = spec.getTls();
if (rules != null) {
for (IngressRule rule : rules) {
HTTPIngressRuleValue http = rule.getHttp();
if (http != null) {
List<HTTPIngressPath> paths = http.getPaths();
if (paths != null) {
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 (tls != null) {
for (IngressTLS tlsHost : tls) {
List<String> hosts = tlsHost.getHosts();
if (hosts != null) {
for (String host : hosts) {
if (StringUtils.isNotBlank(host)) {
return String.format("https://%s/%s", host, preparePath(pathPostfix));
}
}
}
}
}
answer = rule.getHost();
if (StringUtils.isNotBlank(answer)) {
return String.format("http://%s/%s", answer, preparePath(pathPostfix));
}
}
}
}
}
}
}
}
}
}
}
}
}
// lets try use the status on GKE
ServiceStatus status = srv.getStatus();
if (status != null) {
LoadBalancerStatus loadBalancerStatus = status.getLoadBalancer();
if (loadBalancerStatus != null) {
List<LoadBalancerIngress> loadBalancerIngresses = loadBalancerStatus.getIngress();
if (loadBalancerIngresses != null) {
for (LoadBalancerIngress loadBalancerIngress : loadBalancerIngresses) {
String ip = loadBalancerIngress.getIp();
if (StringUtils.isNotBlank(ip)) {
clusterIP = ip;
break;
}
}
}
}
}
}
if (StringUtils.isBlank(clusterIP)) {
// on vanilla kubernetes we can use nodePort to access things externally
boolean found = false;
Integer nodePort = port.getNodePort();
if (nodePort != null) {
NodeList nodeList = client.nodes().list();
if (nodeList != null) {
List<Node> items = nodeList.getItems();
if (items != null) {
for (Node item : items) {
NodeStatus status = item.getStatus();
if (!found && status != null) {
List<NodeAddress> addresses = status.getAddresses();
if (addresses != null) {
for (NodeAddress address : addresses) {
String ip = address.getAddress();
if (StringUtils.isNotBlank(ip)) {
clusterIP = ip;
portNumber = nodePort;
found = true;
break;
}
}
}
}
if (!found) {
NodeSpec spec = item.getSpec();
if (spec != null) {
clusterIP = spec.getExternalID();
if (StringUtils.isNotBlank(clusterIP)) {
portNumber = nodePort;
break;
}
}
}
}
}
}
}
}
return (serviceProto + "://" + clusterIP + ":" + portNumber).toLowerCase();
}
use of io.fabric8.kubernetes.api.model.networking.v1.IngressTLS in project strimzi by strimzi.
the class KafkaCluster method generateExternalBootstrapIngressesV1Beta1.
/**
* Generates a list of bootstrap ingress which can be used to bootstrap clients outside of Kubernetes.
*
* @return The list of generated Ingresses
*/
public List<io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress> generateExternalBootstrapIngressesV1Beta1() {
List<GenericKafkaListener> ingressListeners = ListenersUtils.ingressListeners(listeners);
List<io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress> ingresses = new ArrayList<>(ingressListeners.size());
for (GenericKafkaListener listener : ingressListeners) {
String ingressName = ListenersUtils.backwardsCompatibleBootstrapRouteOrIngressName(cluster, listener);
String serviceName = ListenersUtils.backwardsCompatibleBootstrapServiceName(cluster, listener);
String host = ListenersUtils.bootstrapHost(listener);
String ingressClass = ListenersUtils.ingressClass(listener);
io.fabric8.kubernetes.api.model.networking.v1beta1.HTTPIngressPath path = new io.fabric8.kubernetes.api.model.networking.v1beta1.HTTPIngressPathBuilder().withPath("/").withNewBackend().withNewServicePort(listener.getPort()).withServiceName(serviceName).endBackend().build();
io.fabric8.kubernetes.api.model.networking.v1beta1.IngressRule rule = new io.fabric8.kubernetes.api.model.networking.v1beta1.IngressRuleBuilder().withHost(host).withNewHttp().withPaths(path).endHttp().build();
io.fabric8.kubernetes.api.model.networking.v1beta1.IngressTLS tls = new io.fabric8.kubernetes.api.model.networking.v1beta1.IngressTLSBuilder().withHosts(host).build();
io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress ingress = new io.fabric8.kubernetes.api.model.networking.v1beta1.IngressBuilder().withNewMetadata().withName(ingressName).withLabels(getLabelsWithStrimziName(name, Util.mergeLabelsOrAnnotations(templateExternalBootstrapIngressLabels, ListenersUtils.bootstrapLabels(listener))).toMap()).withAnnotations(Util.mergeLabelsOrAnnotations(generateInternalIngressAnnotations(), templateExternalBootstrapIngressAnnotations, ListenersUtils.bootstrapAnnotations(listener))).withNamespace(namespace).withOwnerReferences(createOwnerReference()).endMetadata().withNewSpec().withIngressClassName(ingressClass).withRules(rule).withTls(tls).endSpec().build();
ingresses.add(ingress);
}
return ingresses;
}
use of io.fabric8.kubernetes.api.model.networking.v1.IngressTLS in project strimzi by strimzi.
the class KafkaCluster method generateExternalBootstrapIngresses.
/**
* Generates a list of bootstrap ingress which can be used to bootstrap clients outside of Kubernetes.
*
* @return The list of generated Ingresses
*/
public List<Ingress> generateExternalBootstrapIngresses() {
List<GenericKafkaListener> ingressListeners = ListenersUtils.ingressListeners(listeners);
List<Ingress> ingresses = new ArrayList<>(ingressListeners.size());
for (GenericKafkaListener listener : ingressListeners) {
String ingressName = ListenersUtils.backwardsCompatibleBootstrapRouteOrIngressName(cluster, listener);
String serviceName = ListenersUtils.backwardsCompatibleBootstrapServiceName(cluster, listener);
String host = ListenersUtils.bootstrapHost(listener);
String ingressClass = ListenersUtils.ingressClass(listener);
HTTPIngressPath path = new HTTPIngressPathBuilder().withPath("/").withPathType("Prefix").withNewBackend().withNewService().withName(serviceName).withNewPort().withNumber(listener.getPort()).endPort().endService().endBackend().build();
IngressRule rule = new IngressRuleBuilder().withHost(host).withNewHttp().withPaths(path).endHttp().build();
IngressTLS tls = new IngressTLSBuilder().withHosts(host).build();
Ingress ingress = new IngressBuilder().withNewMetadata().withName(ingressName).withLabels(getLabelsWithStrimziName(name, Util.mergeLabelsOrAnnotations(templateExternalBootstrapIngressLabels, ListenersUtils.bootstrapLabels(listener))).toMap()).withAnnotations(Util.mergeLabelsOrAnnotations(generateInternalIngressAnnotations(), templateExternalBootstrapIngressAnnotations, ListenersUtils.bootstrapAnnotations(listener))).withNamespace(namespace).withOwnerReferences(createOwnerReference()).endMetadata().withNewSpec().withIngressClassName(ingressClass).withRules(rule).withTls(tls).endSpec().build();
ingresses.add(ingress);
}
return ingresses;
}
Aggregations