Search in sources :

Example 1 with HTTPIngressPathBuilder

use of io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method createIngressForService.

private Ingress createIngressForService(String routeDomainPostfix, String namespace, Service service) {
    Ingress ingress = null;
    String serviceName = KubernetesHelper.getName(service);
    ServiceSpec serviceSpec = service.getSpec();
    if (serviceSpec != null && Strings.isNotBlank(serviceName) && shouldCreateExternalURLForService(service, serviceName)) {
        String ingressId = serviceName;
        String host = "";
        if (Strings.isNotBlank(routeDomainPostfix)) {
            host = serviceName + "." + namespace + "." + Strings.stripPrefix(routeDomainPostfix, ".");
        }
        List<HTTPIngressPath> paths = new ArrayList<>();
        List<ServicePort> ports = serviceSpec.getPorts();
        if (ports != null) {
            for (ServicePort port : ports) {
                Integer portNumber = port.getPort();
                if (portNumber != null) {
                    HTTPIngressPath path = new HTTPIngressPathBuilder().withNewBackend().withServiceName(serviceName).withServicePort(createIntOrString(portNumber.intValue())).endBackend().build();
                    paths.add(path);
                }
            }
        }
        if (paths.isEmpty()) {
            return ingress;
        }
        ingress = new IngressBuilder().withNewMetadata().withName(ingressId).withNamespace(namespace).endMetadata().withNewSpec().addNewRule().withHost(host).withNewHttp().withPaths(paths).endHttp().endRule().endSpec().build();
        String json;
        try {
            json = KubernetesHelper.toJson(ingress);
        } catch (JsonProcessingException e) {
            json = e.getMessage() + ". object: " + ingress;
        }
        log.debug("Created ingress: " + json);
    }
    return ingress;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) IngressBuilder(io.fabric8.kubernetes.api.model.extensions.IngressBuilder) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) ArrayList(java.util.ArrayList) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) HTTPIngressPathBuilder(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with HTTPIngressPathBuilder

use of io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder in project kubernetes by ballerinax.

the class IngressHandler method generate.

/**
 * Generate kubernetes ingress definition from annotation.
 *
 * @return Generated kubernetes {@link Ingress} definition
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
public String generate() throws KubernetesPluginException {
    // generate ingress backend
    IngressBackend ingressBackend = new IngressBackendBuilder().withServiceName(ingressModel.getServiceName()).withNewServicePort(ingressModel.getServicePort()).build();
    // generate ingress path
    HTTPIngressPath ingressPath = new HTTPIngressPathBuilder().withBackend(ingressBackend).withPath(ingressModel.getPath()).build();
    // generate TLS
    IngressTLS ingressTLS;
    if (ingressModel.isEnableTLS()) {
        ingressTLS = new IngressTLSBuilder().withHosts(ingressModel.getHostname()).build();
    } else {
        ingressTLS = new IngressTLSBuilder().build();
    }
    // generate annotationMap
    Map<String, String> annotationMap = new HashMap<>();
    annotationMap.put("kubernetes.io/ingress.class", ingressModel.getIngressClass());
    annotationMap.put("nginx.ingress.kubernetes.io/ssl-passthrough", String.valueOf(ingressModel.isEnableTLS()));
    if (ingressModel.getTargetPath() != null) {
        annotationMap.put("nginx.ingress.kubernetes.io/rewrite-target", ingressModel.getTargetPath());
    }
    // generate ingress
    Ingress ingress = new IngressBuilder().withNewMetadata().withName(ingressModel.getName()).addToLabels(ingressModel.getLabels()).addToAnnotations(annotationMap).endMetadata().withNewSpec().withTls(ingressTLS).addNewRule().withHost(ingressModel.getHostname()).withNewHttp().withPaths(ingressPath).endHttp().endRule().endSpec().build();
    String ingressYAML;
    try {
        ingressYAML = SerializationUtils.dumpWithoutRuntimeStateAsYaml(ingress);
    } catch (JsonProcessingException e) {
        String errorMessage = "Error while generating yaml file for ingress: " + ingressModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
    return ingressYAML;
}
Also used : IngressBackendBuilder(io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder) HashMap(java.util.HashMap) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) IngressBuilder(io.fabric8.kubernetes.api.model.extensions.IngressBuilder) HTTPIngressPathBuilder(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder) IngressTLSBuilder(io.fabric8.kubernetes.api.model.extensions.IngressTLSBuilder) IngressTLS(io.fabric8.kubernetes.api.model.extensions.IngressTLS) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IngressBackend(io.fabric8.kubernetes.api.model.extensions.IngressBackend)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 HTTPIngressPath (io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath)2 HTTPIngressPathBuilder (io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder)2 Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)2 IngressBuilder (io.fabric8.kubernetes.api.model.extensions.IngressBuilder)2 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)1 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)1 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)1 IngressBackend (io.fabric8.kubernetes.api.model.extensions.IngressBackend)1 IngressBackendBuilder (io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder)1 IngressTLS (io.fabric8.kubernetes.api.model.extensions.IngressTLS)1 IngressTLSBuilder (io.fabric8.kubernetes.api.model.extensions.IngressTLSBuilder)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)1