Search in sources :

Example 6 with ServiceSpec

use of io.fabric8.kubernetes.api.model.ServiceSpec 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;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) IngressBuilder(io.fabric8.kubernetes.api.model.extensions.IngressBuilder) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) ArrayList(java.util.ArrayList) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) HTTPIngressPathBuilder(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with ServiceSpec

use of io.fabric8.kubernetes.api.model.ServiceSpec in project fabric8 by fabric8io.

the class KubernetesAssert method getServiceSpec.

protected ServiceSpec getServiceSpec(String serviceId, String namespace) {
    Service service = getService(serviceId, namespace);
    ServiceSpec spec = service.getSpec();
    assertThat(spec).isNotNull();
    return spec;
}
Also used : ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) Service(io.fabric8.kubernetes.api.model.Service)

Example 8 with ServiceSpec

use of io.fabric8.kubernetes.api.model.ServiceSpec in project fabric8 by fabric8io.

the class KubernetesAssert method hasServicePort.

/**
 * Asserts that the service can be found for the given name and has a port of the given value
 */
public void hasServicePort(String serviceId, int port) {
    ServiceSpec spec = getServiceSpec(serviceId, namespace());
    boolean found = false;
    List<ServicePort> ports = spec.getPorts();
    List<Integer> portNumbers = new ArrayList<>();
    if (ports != null) {
        for (ServicePort servicePort : ports) {
            Integer aPort = servicePort.getPort();
            if (aPort != null) {
                if (aPort == port) {
                    found = true;
                    break;
                } else {
                    portNumbers.add(aPort);
                }
            }
        }
    }
    assertThat(found).describedAs("No port found for " + port + " but found ports: " + portNumbers).isTrue();
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) ArrayList(java.util.ArrayList)

Example 9 with ServiceSpec

use of io.fabric8.kubernetes.api.model.ServiceSpec in project fabric8 by fabric8io.

the class ServicePodsAssert method pods.

@Override
public PodSelectionAssert pods() {
    spec().isNotNull().selector().isNotNull();
    ServiceSpec spec = this.actual.getSpec();
    int replicas = 1;
    LabelSelector selector = null;
    Map<String, String> matchLabels = spec.getSelector();
    List<LabelSelectorRequirement> matchExpressions = selector.getMatchExpressions();
    return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "Service " + KubernetesHelper.getName(actual));
}
Also used : ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) LabelSelectorRequirement(io.fabric8.kubernetes.api.model.LabelSelectorRequirement)

Example 10 with ServiceSpec

use of io.fabric8.kubernetes.api.model.ServiceSpec in project fabric8-maven-plugin by fabric8io.

the class OpenShiftRouteEnricher method createRoutePort.

private RoutePort createRoutePort(ServiceBuilder serviceBuilder) {
    RoutePort routePort = null;
    ServiceSpec spec = serviceBuilder.getSpec();
    if (spec != null) {
        List<ServicePort> ports = spec.getPorts();
        if (ports != null && ports.size() > 0) {
            ServicePort servicePort = ports.get(0);
            if (servicePort != null) {
                IntOrString targetPort = servicePort.getTargetPort();
                if (targetPort != null) {
                    routePort = new RoutePort();
                    routePort.setTargetPort(targetPort);
                }
            }
        }
    }
    return routePort;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) RoutePort(io.fabric8.openshift.api.model.RoutePort) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec)

Aggregations

ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)12 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)7 Service (io.fabric8.kubernetes.api.model.Service)6 ArrayList (java.util.ArrayList)5 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3 HashMap (java.util.HashMap)3 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)2 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)2 List (java.util.List)2 Map (java.util.Map)2 Exchange (org.apache.camel.Exchange)2 Processor (org.apache.camel.Processor)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 KubernetesContainerClient (de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient)1 Container (io.fabric8.kubernetes.api.model.Container)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)1 EndpointAddress (io.fabric8.kubernetes.api.model.EndpointAddress)1 EndpointSubset (io.fabric8.kubernetes.api.model.EndpointSubset)1