use of io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder 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;
}
use of io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder 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);
}
}
use of io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder in project fabric8-maven-plugin by fabric8io.
the class IngressEnricher method hasIngress.
/**
* Returns true if we already have a route created for the given name
*/
private boolean hasIngress(final KubernetesListBuilder listBuilder, final String name) {
final AtomicBoolean answer = new AtomicBoolean(false);
listBuilder.accept(new TypedVisitor<IngressBuilder>() {
@Override
public void visit(IngressBuilder builder) {
ObjectMeta metadata = builder.getMetadata();
if (metadata != null && name.equals(metadata.getName())) {
answer.set(true);
}
}
});
return answer.get();
}
use of io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder in project strimzi by strimzi.
the class KafkaCluster method generateExternalIngresses.
/**
* Generates list of ingress for pod. This ingress is used for exposing it externally using Nginx Ingress.
*
* @param pod Number of the pod for which this ingress should be generated
* @return The list of generated Ingresses
*/
public List<Ingress> generateExternalIngresses(int pod) {
List<GenericKafkaListener> ingressListeners = ListenersUtils.ingressListeners(listeners);
List<Ingress> ingresses = new ArrayList<>(ingressListeners.size());
for (GenericKafkaListener listener : ingressListeners) {
String ingressName = ListenersUtils.backwardsCompatibleBrokerServiceName(cluster, pod, listener);
String host = ListenersUtils.brokerHost(listener, pod);
String ingressClass = ListenersUtils.ingressClass(listener);
HTTPIngressPath path = new HTTPIngressPathBuilder().withPath("/").withPathType("Prefix").withNewBackend().withNewService().withName(ingressName).withNewPort().withNumber(listener.getPort()).endPort().endService().endBackend().build();
IngressRule rule = new IngressRuleBuilder().withHost(host).withNewHttp().withPaths(path).endHttp().build();
IngressTLS tls = new IngressTLSBuilder().withHosts(host).build();
Ingress ingress = new IngressBuilder().withNewMetadata().withName(ingressName).withLabels(getLabelsWithStrimziName(name, Util.mergeLabelsOrAnnotations(templatePerPodIngressLabels, ListenersUtils.brokerLabels(listener, pod))).toMap()).withAnnotations(Util.mergeLabelsOrAnnotations(generateInternalIngressAnnotations(), templatePerPodIngressAnnotations, ListenersUtils.brokerAnnotations(listener, pod))).withNamespace(namespace).withOwnerReferences(createOwnerReference()).endMetadata().withNewSpec().withIngressClassName(ingressClass).withRules(rule).withTls(tls).endSpec().build();
ingresses.add(ingress);
}
return ingresses;
}
use of io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder in project strimzi by strimzi.
the class KafkaCluster method generateExternalIngressesV1Beta1.
/**
* Generates list of ingress for pod. This ingress is used for exposing it externally using Nginx Ingress.
*
* @param pod Number of the pod for which this ingress should be generated
* @return The list of generated Ingresses
*/
public List<io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress> generateExternalIngressesV1Beta1(int pod) {
List<GenericKafkaListener> ingressListeners = ListenersUtils.ingressListeners(listeners);
List<io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress> ingresses = new ArrayList<>(ingressListeners.size());
for (GenericKafkaListener listener : ingressListeners) {
String ingressName = ListenersUtils.backwardsCompatibleBrokerServiceName(cluster, pod, listener);
String host = ListenersUtils.brokerHost(listener, pod);
String ingressClass = ListenersUtils.ingressClass(listener);
io.fabric8.kubernetes.api.model.networking.v1beta1.HTTPIngressPath path = new io.fabric8.kubernetes.api.model.networking.v1beta1.HTTPIngressPathBuilder().withPath("/").withNewBackend().withNewServicePort(listener.getPort()).withServiceName(ingressName).endBackend().build();
io.fabric8.kubernetes.api.model.networking.v1beta1.IngressRule rule = new io.fabric8.kubernetes.api.model.networking.v1beta1.IngressRuleBuilder().withHost(host).withNewHttp().withPaths(path).endHttp().build();
io.fabric8.kubernetes.api.model.networking.v1beta1.IngressTLS tls = new io.fabric8.kubernetes.api.model.networking.v1beta1.IngressTLSBuilder().withHosts(host).build();
io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress ingress = new io.fabric8.kubernetes.api.model.networking.v1beta1.IngressBuilder().withNewMetadata().withName(ingressName).withLabels(getLabelsWithStrimziName(name, Util.mergeLabelsOrAnnotations(templatePerPodIngressLabels, ListenersUtils.brokerLabels(listener, pod))).toMap()).withAnnotations(Util.mergeLabelsOrAnnotations(generateInternalIngressAnnotations(), templatePerPodIngressAnnotations, ListenersUtils.brokerAnnotations(listener, pod))).withNamespace(namespace).withOwnerReferences(createOwnerReference()).endMetadata().withNewSpec().withIngressClassName(ingressClass).withRules(rule).withTls(tls).endSpec().build();
ingresses.add(ingress);
}
return ingresses;
}
Aggregations