Search in sources :

Example 86 with Service

use of io.fabric8.patch.Service 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 87 with Service

use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method serviceHasIngressRule.

/**
 * Returns true if there is an existing ingress rule for the given service
 */
private boolean serviceHasIngressRule(List<Ingress> ingresses, Service service) {
    String serviceName = KubernetesHelper.getName(service);
    for (Ingress ingress : ingresses) {
        IngressSpec spec = ingress.getSpec();
        if (spec == null) {
            break;
        }
        List<IngressRule> rules = spec.getRules();
        if (rules == null) {
            break;
        }
        for (IngressRule rule : rules) {
            HTTPIngressRuleValue http = rule.getHttp();
            if (http == null) {
                break;
            }
            List<HTTPIngressPath> paths = http.getPaths();
            if (paths == null) {
                break;
            }
            for (HTTPIngressPath path : paths) {
                IngressBackend backend = path.getBackend();
                if (backend == null) {
                    break;
                }
                if (Objects.equals(serviceName, backend.getServiceName())) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : IngressSpec(io.fabric8.kubernetes.api.model.extensions.IngressSpec) IngressRule(io.fabric8.kubernetes.api.model.extensions.IngressRule) HTTPIngressRuleValue(io.fabric8.kubernetes.api.model.extensions.HTTPIngressRuleValue) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) IngressBackend(io.fabric8.kubernetes.api.model.extensions.IngressBackend)

Example 88 with Service

use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method createRoutes.

protected void createRoutes(Controller controller, Collection<HasMetadata> collection) {
    String routeDomainPostfix = this.routeDomain;
    Log log = getLog();
    String namespace = clusterAccess.getNamespace();
    // lets get the routes first to see if we should bother
    try {
        OpenShiftClient openshiftClient = controller.getOpenShiftClientOrNull();
        if (openshiftClient == null) {
            return;
        }
        RouteList routes = openshiftClient.routes().inNamespace(namespace).list();
        if (routes != null) {
            routes.getItems();
        }
    } catch (Exception e) {
        log.warn("Cannot load OpenShift Routes; maybe not connected to an OpenShift platform? " + e, e);
        return;
    }
    List<Route> routes = new ArrayList<>();
    for (Object object : collection) {
        if (object instanceof Service) {
            Service service = (Service) object;
            Route route = createRouteForService(routeDomainPostfix, namespace, service);
            if (route != null) {
                routes.add(route);
            }
        }
    }
    collection.addAll(routes);
}
Also used : Log(org.apache.maven.plugin.logging.Log) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) RouteList(io.fabric8.openshift.api.model.RouteList) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Route(io.fabric8.openshift.api.model.Route)

Example 89 with Service

use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method getGogsURL.

private String getGogsURL(KubernetesClient kubernetes, String namespace) throws MojoExecutionException {
    try {
        Endpoints endpoints = kubernetes.endpoints().inNamespace(namespace).withName(ServiceNames.GOGS).get();
        int runningEndpoints = 0;
        if (endpoints != null) {
            List<EndpointSubset> subsets = endpoints.getSubsets();
            for (EndpointSubset subset : subsets) {
                List<EndpointAddress> addresses = subset.getAddresses();
                if (addresses != null) {
                    runningEndpoints += addresses.size();
                }
            }
        }
        if (runningEndpoints == 0) {
            return null;
        }
        log.info("Running %s endpoints of %s in namespace %s", runningEndpoints, ServiceNames.GOGS, namespace);
        return KubernetesHelper.getServiceURL(kubernetes, ServiceNames.GOGS, namespace, "http", true);
    } catch (Exception e) {
        String errorMessage = e.getMessage();
        if (errorMessage.contains(String.format("No service gogs running in namespace %s", namespace))) {
            log.info("Gogs service does not exists, defaulting to GitHub");
            return null;
        } else {
            throw new MojoExecutionException(e.getMessage());
        }
    }
}
Also used : Endpoints(io.fabric8.kubernetes.api.model.Endpoints) EndpointSubset(io.fabric8.kubernetes.api.model.EndpointSubset) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) EndpointAddress(io.fabric8.kubernetes.api.model.EndpointAddress)

Example 90 with Service

use of io.fabric8.patch.Service in project strimzi by strimzi.

the class KafkaConnectClusterTest method testGenerateService.

@Test
public void testGenerateService() {
    Service svc = kc.generateService();
    assertEquals("ClusterIP", svc.getSpec().getType());
    Map<String, String> expectedLabels = ResourceUtils.labels(Labels.STRIMZI_CLUSTER_LABEL, this.cluster, Labels.STRIMZI_TYPE_LABEL, "kafka-connect", "my-user-label", "cromulent", Labels.STRIMZI_NAME_LABEL, kc.kafkaConnectClusterName(cluster));
    assertEquals(expectedLabels, svc.getMetadata().getLabels());
    assertEquals(expectedLabels, svc.getSpec().getSelector());
    assertEquals(1, svc.getSpec().getPorts().size());
    assertEquals(new Integer(KafkaConnectCluster.REST_API_PORT), svc.getSpec().getPorts().get(0).getPort());
    assertEquals(KafkaConnectCluster.REST_API_PORT_NAME, svc.getSpec().getPorts().get(0).getName());
    assertEquals("TCP", svc.getSpec().getPorts().get(0).getProtocol());
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) Test(org.junit.Test)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)100 Test (org.junit.Test)78 IOException (java.io.IOException)35 ArrayList (java.util.ArrayList)35 HashMap (java.util.HashMap)33 File (java.io.File)28 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)26 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)24 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)22 Map (java.util.Map)19 Pod (io.fabric8.kubernetes.api.model.Pod)18 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)18 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)17 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)17 List (java.util.List)17 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 Reconciliation (io.strimzi.controller.cluster.Reconciliation)15 ConfigMapOperator (io.strimzi.controller.cluster.operator.resource.ConfigMapOperator)15 ServiceOperator (io.strimzi.controller.cluster.operator.resource.ServiceOperator)15