io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExternalServerIngressBuilder.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
public Ingress build() {

    IngressBackend ingressBackend =
        new IngressBackendBuilder()
            .withServiceName(serviceName)
            .withNewServicePort(servicePort.getStrVal())
            .build();

    HTTPIngressPathBuilder httpIngressPathBuilder =
        new HTTPIngressPathBuilder().withBackend(ingressBackend);

    if (!isNullOrEmpty(path)) {
      httpIngressPathBuilder.withPath(path);
    }

    HTTPIngressPath httpIngressPath = httpIngressPathBuilder.build();

    HTTPIngressRuleValue httpIngressRuleValue =
        new HTTPIngressRuleValueBuilder().withPaths(httpIngressPath).build();
    IngressRuleBuilder ingressRuleBuilder = new IngressRuleBuilder().withHttp(httpIngressRuleValue);

    if (!isNullOrEmpty(host)) {
      ingressRuleBuilder.withHost(host);
    }

    IngressRule ingressRule = ingressRuleBuilder.build();

    IngressSpec ingressSpec = new IngressSpecBuilder().withRules(ingressRule).build();

    Map<String, String> ingressAnnotations = new HashMap<>(annotations);
    ingressAnnotations.putAll(
        Annotations.newSerializer().servers(serversConfigs).machineName(machineName).annotations());

    return new io.fabric8.kubernetes.api.model.extensions.IngressBuilder()
        .withSpec(ingressSpec)
        .withMetadata(
            new ObjectMetaBuilder().withName(name).withAnnotations(ingressAnnotations).build())
        .build();
  }
 
Example #2
Source File: IngressEnricher.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
protected static Ingress addIngress(KubernetesListBuilder listBuilder, ServiceBuilder serviceBuilder, String routeDomainPostfix, KitLogger log) {
    ObjectMeta serviceMetadata = serviceBuilder.buildMetadata();
    if (serviceMetadata == null) {
        log.info("No Metadata for service! ");
    }
    if (isExposedService(serviceMetadata) && shouldCreateExternalURLForService(serviceBuilder, log)) {
        Objects.requireNonNull(serviceMetadata);
        String serviceName = serviceMetadata.getName();
        if (!hasIngress(listBuilder, serviceName)) {
            Integer servicePort = getServicePort(serviceBuilder);
            if (servicePort != null) {

                IngressBuilder ingressBuilder = new IngressBuilder().
                        withMetadata(serviceMetadata).
                        withNewSpec().
                        endSpec();

                // removing `expose : true` label from metadata.
                removeLabel(ingressBuilder.buildMetadata(), EXPOSE_LABEL, "true");
                removeLabel(ingressBuilder.buildMetadata(), JKubeAnnotations.SERVICE_EXPOSE_URL.value(), "true");
                ingressBuilder.withNewMetadataLike(ingressBuilder.buildMetadata());

                if (StringUtils.isNotBlank(routeDomainPostfix)) {
                    routeDomainPostfix = serviceName + "." + FileUtil.stripPrefix(routeDomainPostfix, ".");
                    ingressBuilder = ingressBuilder.withSpec(new IngressSpecBuilder().addNewRule().
                            withHost(routeDomainPostfix).
                            withNewHttp().
                            withPaths(new HTTPIngressPathBuilder()
                                    .withNewBackend()
                                    .withServiceName(serviceName)
                                    .withServicePort(KubernetesHelper.createIntOrString(getServicePort(serviceBuilder)))
                                    .endBackend()
                                    .build())
                            .endHttp().
                                    endRule().build());
                } else {
                    ingressBuilder.withSpec(new IngressSpecBuilder().withBackend(new IngressBackendBuilder().
                            withNewServiceName(serviceName)
                            .withNewServicePort(getServicePort(serviceBuilder))
                            .build()).build());
                }

                return ingressBuilder.build();
            }
        }
    }
    return null;
}
 
Example #3
Source File: IngressHandler.java    From module-ballerina-kubernetes with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}