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;
}
Aggregations