Search in sources :

Example 1 with IngressBackendBuilder

use of io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder 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)1 HTTPIngressPath (io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath)1 HTTPIngressPathBuilder (io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder)1 Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)1 IngressBackend (io.fabric8.kubernetes.api.model.extensions.IngressBackend)1 IngressBackendBuilder (io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder)1 IngressBuilder (io.fabric8.kubernetes.api.model.extensions.IngressBuilder)1 IngressTLS (io.fabric8.kubernetes.api.model.extensions.IngressTLS)1 IngressTLSBuilder (io.fabric8.kubernetes.api.model.extensions.IngressTLSBuilder)1 HashMap (java.util.HashMap)1 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)1