use of io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder in project fabric8-maven-plugin by fabric8io.
the class ApplyMojo method createIngressForService.
private Ingress createIngressForService(String routeDomainPostfix, String namespace, Service service) {
Ingress ingress = null;
String serviceName = KubernetesHelper.getName(service);
ServiceSpec serviceSpec = service.getSpec();
if (serviceSpec != null && Strings.isNotBlank(serviceName) && shouldCreateExternalURLForService(service, serviceName)) {
String ingressId = serviceName;
String host = "";
if (Strings.isNotBlank(routeDomainPostfix)) {
host = serviceName + "." + namespace + "." + Strings.stripPrefix(routeDomainPostfix, ".");
}
List<HTTPIngressPath> paths = new ArrayList<>();
List<ServicePort> ports = serviceSpec.getPorts();
if (ports != null) {
for (ServicePort port : ports) {
Integer portNumber = port.getPort();
if (portNumber != null) {
HTTPIngressPath path = new HTTPIngressPathBuilder().withNewBackend().withServiceName(serviceName).withServicePort(createIntOrString(portNumber.intValue())).endBackend().build();
paths.add(path);
}
}
}
if (paths.isEmpty()) {
return ingress;
}
ingress = new IngressBuilder().withNewMetadata().withName(ingressId).withNamespace(namespace).endMetadata().withNewSpec().addNewRule().withHost(host).withNewHttp().withPaths(paths).endHttp().endRule().endSpec().build();
String json;
try {
json = KubernetesHelper.toJson(ingress);
} catch (JsonProcessingException e) {
json = e.getMessage() + ". object: " + ingress;
}
log.debug("Created ingress: " + json);
}
return ingress;
}
use of io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder 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