Search in sources :

Example 16 with IngressRule

use of io.fabric8.kubernetes.api.model.networking.v1.IngressRule in project xp-operator by enonic.

the class OperatorIngressLabel method setStatus.

private void setStatus(final Ingress ingress) {
    // Collect all nodegroup vhost states
    Map<String, Xp7ConfigStatus.State> states = searchers.xp7Config().stream().filter(inSameNamespaceAs(ingress)).filter(isDeleted().negate()).filter(this::isVHostConfig).collect(Collectors.toMap(c -> c.getSpec().getNodeGroup(), c -> c.getStatus().getState()));
    // Set all nodeGroups state
    if (states.values().stream().anyMatch(s -> !s.equals(Xp7ConfigStatus.State.READY))) {
        states.put(cfgStr("operator.charts.values.allNodesKey"), Xp7ConfigStatus.State.PENDING);
    } else {
        states.put(cfgStr("operator.charts.values.allNodesKey"), Xp7ConfigStatus.State.READY);
    }
    // Figure out state
    boolean loaded = true;
    for (IngressRule r : ingress.getSpec().getRules()) {
        for (HTTPIngressPath p : r.getHttp().getPaths()) {
            Xp7ConfigStatus.State pathState = states.get(p.getBackend().getService().getName());
            if (pathState != null && !pathState.equals(Xp7ConfigStatus.State.READY)) {
                loaded = false;
                break;
            }
        }
    }
    // Update if true
    if (loaded) {
        K8sLogHelper.logEdit(clients.k8s().network().ingress().inNamespace(ingress.getMetadata().getNamespace()).withName(ingress.getMetadata().getName()), i -> {
            Map<String, String> labels = i.getMetadata().getLabels();
            if (labels == null) {
                labels = new HashMap<>();
            }
            labels.put(cfgStr("operator.charts.values.labelKeys.ingressVhostLoaded"), "true");
            i.getMetadata().setLabels(labels);
            return i;
        });
    }
}
Also used : Clients(com.enonic.kubernetes.kubernetes.Clients) Predicates.isDeleted(com.enonic.kubernetes.kubernetes.Predicates.isDeleted) Predicates.inSameNamespaceAs(com.enonic.kubernetes.kubernetes.Predicates.inSameNamespaceAs) HTTPIngressPath(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath) Xp7Config(com.enonic.kubernetes.client.v1.xp7config.Xp7Config) Searchers(com.enonic.kubernetes.kubernetes.Searchers) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) Predicates.onCondition(com.enonic.kubernetes.kubernetes.Predicates.onCondition) Informers(com.enonic.kubernetes.kubernetes.Informers) K8sLogHelper(com.enonic.kubernetes.kubernetes.commands.K8sLogHelper) IngressRule(io.fabric8.kubernetes.api.model.networking.v1.IngressRule) Xp7ConfigStatus(com.enonic.kubernetes.client.v1.xp7config.Xp7ConfigStatus) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) Observes(javax.enterprise.event.Observes) Map(java.util.Map) Configuration.cfgStr(com.enonic.kubernetes.common.Configuration.cfgStr) Predicates.matchLabel(com.enonic.kubernetes.kubernetes.Predicates.matchLabel) InformerEventHandler(com.enonic.kubernetes.operator.helpers.InformerEventHandler) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StartupEvent(io.quarkus.runtime.StartupEvent) IngressRule(io.fabric8.kubernetes.api.model.networking.v1.IngressRule) Xp7ConfigStatus(com.enonic.kubernetes.client.v1.xp7config.Xp7ConfigStatus) HTTPIngressPath(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath)

Example 17 with IngressRule

use of io.fabric8.kubernetes.api.model.networking.v1.IngressRule in project jkube by eclipse.

the class NetworkingV1IngressGenerator method getXmlConfiguredIngressSpec.

private static IngressSpec getXmlConfiguredIngressSpec(List<IngressRuleConfig> ingressRuleConfigs, List<IngressTlsConfig> ingressTlsConfigs) {
    IngressSpecBuilder ingressSpecBuilder = new IngressSpecBuilder();
    for (IngressRuleConfig ingressRuleConfig : ingressRuleConfigs) {
        IngressRule ingressRule = getIngressRuleFromXmlConfig(ingressRuleConfig);
        ingressSpecBuilder.addToRules(ingressRule);
    }
    for (IngressTlsConfig ingressTlsConfig : ingressTlsConfigs) {
        IngressTLS ingressTLS = getIngressTlsFromXMLConfig(ingressTlsConfig);
        ingressSpecBuilder.addToTls(ingressTLS);
    }
    return ingressSpecBuilder.build();
}
Also used : IngressTlsConfig(org.eclipse.jkube.kit.config.resource.IngressTlsConfig) IngressRule(io.fabric8.kubernetes.api.model.networking.v1.IngressRule) IngressSpecBuilder(io.fabric8.kubernetes.api.model.networking.v1.IngressSpecBuilder) IngressRuleConfig(org.eclipse.jkube.kit.config.resource.IngressRuleConfig) IngressTLS(io.fabric8.kubernetes.api.model.networking.v1.IngressTLS)

Example 18 with IngressRule

use of io.fabric8.kubernetes.api.model.networking.v1.IngressRule in project flink-kubernetes-operator by apache.

the class IngressUtils method fromDeployment.

private static IngressRule fromDeployment(FlinkDeployment flinkDeployment, Configuration effectiveConfig) {
    final String clusterId = flinkDeployment.getMetadata().getName();
    final int restPort = effectiveConfig.getInteger(RestOptions.PORT);
    final String ingressHost = getIngressHost(flinkDeployment, clusterId);
    return new IngressRule(ingressHost, new HTTPIngressRuleValueBuilder().addNewPath().withPathType("ImplementationSpecific").withNewBackend().withNewService().withName(clusterId + REST_SVC_NAME_SUFFIX).withNewPort().withNumber(restPort).endPort().endService().endBackend().endPath().build());
}
Also used : IngressRule(io.fabric8.kubernetes.api.model.networking.v1.IngressRule) HTTPIngressRuleValueBuilder(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressRuleValueBuilder)

Example 19 with IngressRule

use of io.fabric8.kubernetes.api.model.networking.v1.IngressRule 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();
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IngressList(io.fabric8.kubernetes.api.model.extensions.IngressList) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) IngressSpec(io.fabric8.kubernetes.api.model.extensions.IngressSpec) IngressRule(io.fabric8.kubernetes.api.model.extensions.IngressRule) HTTPIngressRuleValue(io.fabric8.kubernetes.api.model.extensions.HTTPIngressRuleValue) Route(io.fabric8.openshift.api.model.Route) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) TextParseException(org.xbill.DNS.TextParseException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ParseException(java.text.ParseException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLKeyException(javax.net.ssl.SSLKeyException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) IngressTLS(io.fabric8.kubernetes.api.model.extensions.IngressTLS) IngressBackend(io.fabric8.kubernetes.api.model.extensions.IngressBackend) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 20 with IngressRule

use of io.fabric8.kubernetes.api.model.networking.v1.IngressRule in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method serviceHasIngressRule.

/**
 * Returns true if there is an existing ingress rule for the given service
 */
private boolean serviceHasIngressRule(List<Ingress> ingresses, Service service) {
    String serviceName = KubernetesHelper.getName(service);
    for (Ingress ingress : ingresses) {
        IngressSpec spec = ingress.getSpec();
        if (spec == null) {
            break;
        }
        List<IngressRule> rules = spec.getRules();
        if (rules == null) {
            break;
        }
        for (IngressRule rule : rules) {
            HTTPIngressRuleValue http = rule.getHttp();
            if (http == null) {
                break;
            }
            List<HTTPIngressPath> paths = http.getPaths();
            if (paths == null) {
                break;
            }
            for (HTTPIngressPath path : paths) {
                IngressBackend backend = path.getBackend();
                if (backend == null) {
                    break;
                }
                if (Objects.equals(serviceName, backend.getServiceName())) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : IngressSpec(io.fabric8.kubernetes.api.model.extensions.IngressSpec) IngressRule(io.fabric8.kubernetes.api.model.extensions.IngressRule) HTTPIngressRuleValue(io.fabric8.kubernetes.api.model.extensions.HTTPIngressRuleValue) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) IngressBackend(io.fabric8.kubernetes.api.model.extensions.IngressBackend)

Aggregations

IngressRule (io.fabric8.kubernetes.api.model.networking.v1.IngressRule)39 Ingress (io.fabric8.kubernetes.api.model.networking.v1.Ingress)37 IngressBackend (io.fabric8.kubernetes.api.model.networking.v1.IngressBackend)24 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)19 HTTPIngressPath (io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath)19 IngressServiceBackend (io.fabric8.kubernetes.api.model.networking.v1.IngressServiceBackend)19 Service (io.fabric8.kubernetes.api.model.Service)17 ServiceBackendPort (io.fabric8.kubernetes.api.model.networking.v1.ServiceBackendPort)16 IngressSpec (io.fabric8.kubernetes.api.model.networking.v1.IngressSpec)14 HTTPIngressPathBuilder (io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPathBuilder)12 Test (org.testng.annotations.Test)12 HTTPIngressRuleValue (io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressRuleValue)11 IngressBuilder (io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder)10 IngressRuleBuilder (io.fabric8.kubernetes.api.model.networking.v1.IngressRuleBuilder)10 ArrayList (java.util.ArrayList)10 IngressTLSBuilder (io.fabric8.kubernetes.api.model.networking.v1.IngressTLSBuilder)9 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)8 GenericKafkaListener (io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListener)8 HTTPIngressRuleValueBuilder (io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressRuleValueBuilder)7 Annotations (org.eclipse.che.workspace.infrastructure.kubernetes.Annotations)6