Search in sources :

Example 81 with Service

use of io.fabric8.knative.serving.v1.Service in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method createRouteForService.

private Route createRouteForService(String routeDomainPostfix, String namespace, Service service) {
    Route route = null;
    String id = KubernetesHelper.getName(service);
    if (Strings.isNotBlank(id) && hasExactlyOneService(service, id)) {
        route = new Route();
        String routeId = id;
        KubernetesHelper.setName(route, namespace, routeId);
        RouteSpec routeSpec = new RouteSpec();
        RouteTargetReference objectRef = new RouteTargetReferenceBuilder().withName(id).build();
        // objectRef.setNamespace(namespace);
        routeSpec.setTo(objectRef);
        if (!Strings.isNullOrBlank(routeDomainPostfix)) {
            String host = Strings.stripSuffix(Strings.stripSuffix(id, "-service"), ".");
            routeSpec.setHost(host + "." + Strings.stripPrefix(routeDomainPostfix, "."));
        } else {
            routeSpec.setHost("");
        }
        route.setSpec(routeSpec);
        String json;
        try {
            json = KubernetesHelper.toJson(route);
        } catch (JsonProcessingException e) {
            json = e.getMessage() + ". object: " + route;
        }
        log.debug("Created route: " + json);
    }
    return route;
}
Also used : RouteTargetReference(io.fabric8.openshift.api.model.RouteTargetReference) RouteTargetReferenceBuilder(io.fabric8.openshift.api.model.RouteTargetReferenceBuilder) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) RouteSpec(io.fabric8.openshift.api.model.RouteSpec) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Route(io.fabric8.openshift.api.model.Route)

Example 82 with Service

use of io.fabric8.knative.serving.v1.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 83 with Service

use of io.fabric8.knative.serving.v1.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 84 with Service

use of io.fabric8.knative.serving.v1.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 85 with Service

use of io.fabric8.knative.serving.v1.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)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)142 Test (org.junit.Test)93 File (java.io.File)60 IOException (java.io.IOException)54 ArrayList (java.util.ArrayList)54 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)48 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)43 HashMap (java.util.HashMap)41 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)38 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)33 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)33 Pod (io.fabric8.kubernetes.api.model.Pod)31 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)30 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)29 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)25 List (java.util.List)25 Map (java.util.Map)25 FileInputStream (java.io.FileInputStream)22 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)20 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)20