Search in sources :

Example 16 with Ingress

use of io.fabric8.kubernetes.api.model.extensions.Ingress 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

Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)12 Service (io.fabric8.kubernetes.api.model.Service)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)5 HTTPIngressPath (io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath)5 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)5 ArrayList (java.util.ArrayList)5 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)3 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 HTTPIngressPathBuilder (io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder)3 IngressBackend (io.fabric8.kubernetes.api.model.extensions.IngressBackend)3 IngressBuilder (io.fabric8.kubernetes.api.model.extensions.IngressBuilder)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 Namespace (io.fabric8.kubernetes.api.model.Namespace)2 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)2 Secret (io.fabric8.kubernetes.api.model.Secret)2 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)2