Search in sources :

Example 21 with IngressBackend

use of io.fabric8.kubernetes.api.model.networking.v1.IngressBackend in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class KubernetesNetworkingService method buildIngress.

private Ingress buildIngress(BridgeIngress bridgeIngress, Service service) {
    Ingress ingress = templateProvider.loadBridgeIngressKubernetesIngressTemplate(bridgeIngress);
    IngressBackend ingressBackend = new IngressBackendBuilder().withService(new IngressServiceBackendBuilder().withName(service.getMetadata().getName()).withPort(new ServiceBackendPortBuilder().withNumber(service.getSpec().getPorts().get(0).getPort()).build()).build()).build();
    HTTPIngressPath httpIngressPath = new HTTPIngressPathBuilder().withBackend(ingressBackend).withPath("/" + service.getMetadata().getName() + PATH_REGEX).withPathType("Prefix").build();
    IngressRule ingressRule = new IngressRuleBuilder().withHttp(new HTTPIngressRuleValueBuilder().withPaths(httpIngressPath).build()).build();
    IngressSpec ingressSpec = new IngressSpecBuilder().withRules(ingressRule).build();
    ingress.setSpec(ingressSpec);
    return ingress;
}
Also used : IngressRuleBuilder(io.fabric8.kubernetes.api.model.networking.v1.IngressRuleBuilder) IngressSpec(io.fabric8.kubernetes.api.model.networking.v1.IngressSpec) IngressRule(io.fabric8.kubernetes.api.model.networking.v1.IngressRule) IngressBackendBuilder(io.fabric8.kubernetes.api.model.networking.v1.IngressBackendBuilder) HTTPIngressRuleValueBuilder(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressRuleValueBuilder) IngressSpecBuilder(io.fabric8.kubernetes.api.model.networking.v1.IngressSpecBuilder) IngressServiceBackendBuilder(io.fabric8.kubernetes.api.model.networking.v1.IngressServiceBackendBuilder) ServiceBackendPortBuilder(io.fabric8.kubernetes.api.model.networking.v1.ServiceBackendPortBuilder) BridgeIngress(com.redhat.service.smartevents.shard.operator.resources.BridgeIngress) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) HTTPIngressPathBuilder(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPathBuilder) HTTPIngressPath(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath) IngressBackend(io.fabric8.kubernetes.api.model.networking.v1.IngressBackend)

Example 22 with IngressBackend

use of io.fabric8.kubernetes.api.model.networking.v1.IngressBackend 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)

Example 23 with IngressBackend

use of io.fabric8.kubernetes.api.model.networking.v1.IngressBackend in project kubernetes by ballerinax.

the class IngressHandler method generate.

/**
 * Generate kubernetes ingress definition from annotation.
 *
 * @param ingressModel IngressModel object
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
private void generate(IngressModel ingressModel) 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
    List<IngressTLS> ingressTLS = new ArrayList<>();
    if (ingressModel.isEnableTLS()) {
        ingressTLS.add(new IngressTLSBuilder().withHosts(ingressModel.getHostname()).build());
    }
    // generate annotationMap
    Map<String, String> annotationMap = new HashMap<>();
    annotationMap.put("kubernetes.io/ingress.class", ingressModel.getIngressClass());
    if (NGINX.equals(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());
        }
    }
    // Add user defined ingress annotations to yaml.
    Map<String, String> userDefinedAnnotationMap = ingressModel.getAnnotations();
    if (userDefinedAnnotationMap != null) {
        userDefinedAnnotationMap.forEach(annotationMap::putIfAbsent);
    }
    // generate ingress
    Ingress ingress = new IngressBuilder().withNewMetadata().withName(ingressModel.getName()).withNamespace(dataHolder.getNamespace()).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);
        KubernetesUtils.writeToFile(ingressYAML, INGRESS_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        String errorMessage = "error while generating yaml file for ingress: " + ingressModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
Also used : IngressBackendBuilder(io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) IOException(java.io.IOException) 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) IngressBackend(io.fabric8.kubernetes.api.model.extensions.IngressBackend)

Example 24 with IngressBackend

use of io.fabric8.kubernetes.api.model.networking.v1.IngressBackend in project che-server by eclipse-che.

the class MultiHostExternalServiceExposureStrategyTest method assertThatExternalServerIsExposed.

private void assertThatExternalServerIsExposed(String machineName, String serviceName, String serverNameRegex, String portProtocol, Integer port, ServicePort servicePort, ServerConfigImpl expected) {
    // ensure that required ingress is created
    String ingressName = serviceName + "-" + (expected.isUnique() ? serverNameRegex : servicePort.getName());
    Ingress ingress = kubernetesEnvironment.getIngresses().get(ingressName);
    IngressRule ingressRule = ingress.getSpec().getRules().get(0);
    assertEquals(ingressRule.getHost(), ingressName + "." + DOMAIN);
    assertEquals(ingressRule.getHttp().getPaths().get(0).getPath(), "/");
    IngressBackend backend = ingressRule.getHttp().getPaths().get(0).getBackend();
    assertEquals(backend.getService().getName(), serviceName);
    assertEquals(backend.getService().getPort().getName(), servicePort.getName());
    Annotations.Deserializer ingressAnnotations = Annotations.newDeserializer(ingress.getMetadata().getAnnotations());
    Map<String, ServerConfigImpl> servers = ingressAnnotations.servers();
    ServerConfig serverConfig = servers.get(serverNameRegex);
    assertEquals(serverConfig, expected);
    assertEquals(ingressAnnotations.machineName(), machineName);
    assertEquals(ingress.getMetadata().getLabels().get("foo"), "bar");
}
Also used : ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) IngressRule(io.fabric8.kubernetes.api.model.networking.v1.IngressRule) Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) IngressBackend(io.fabric8.kubernetes.api.model.networking.v1.IngressBackend)

Example 25 with IngressBackend

use of io.fabric8.kubernetes.api.model.networking.v1.IngressBackend in project che-server by eclipse-che.

the class IngressServerResolverTest method createIngress.

private Ingress createIngress(String name, String machineName, Pair<String, ServerConfig> server) {
    Serializer serializer = Annotations.newSerializer();
    serializer.machineName(machineName);
    serializer.server(server.first, server.second);
    return new IngressBuilder().withNewMetadata().withName(name).withAnnotations(serializer.annotations()).endMetadata().withNewSpec().withRules(new IngressRule(null, new HTTPIngressRuleValue(singletonList(new HTTPIngressPath(new IngressBackend(null, new IngressServiceBackend(name, new ServiceBackendPort("8080", 8080))), INGRESS_PATH_PREFIX, null))))).endSpec().withNewStatus().withLoadBalancer(new LoadBalancerStatusBuilder().addNewIngress().withIp("127.0.0.1").endIngress().build()).endStatus().build();
}
Also used : IngressBuilder(io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder) ServiceBackendPort(io.fabric8.kubernetes.api.model.networking.v1.ServiceBackendPort) IngressRule(io.fabric8.kubernetes.api.model.networking.v1.IngressRule) HTTPIngressRuleValue(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressRuleValue) HTTPIngressPath(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath) IngressBackend(io.fabric8.kubernetes.api.model.networking.v1.IngressBackend) LoadBalancerStatusBuilder(io.fabric8.kubernetes.api.model.LoadBalancerStatusBuilder) Serializer(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations.Serializer) IngressServiceBackend(io.fabric8.kubernetes.api.model.networking.v1.IngressServiceBackend)

Aggregations

IngressBackend (io.fabric8.kubernetes.api.model.networking.v1.IngressBackend)28 Ingress (io.fabric8.kubernetes.api.model.networking.v1.Ingress)26 IngressRule (io.fabric8.kubernetes.api.model.networking.v1.IngressRule)26 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)21 IngressServiceBackend (io.fabric8.kubernetes.api.model.networking.v1.IngressServiceBackend)18 Service (io.fabric8.kubernetes.api.model.Service)17 ServiceBackendPort (io.fabric8.kubernetes.api.model.networking.v1.ServiceBackendPort)16 Test (org.testng.annotations.Test)16 HTTPIngressPath (io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath)12 IngressSpec (io.fabric8.kubernetes.api.model.networking.v1.IngressSpec)12 HTTPIngressRuleValue (io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressRuleValue)10 HashMap (java.util.HashMap)10 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)9 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)8 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)8 PreviewUrlImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.PreviewUrlImpl)8 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)8 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)6 HTTPIngressPath (io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath)6 Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)6