Search in sources :

Example 1 with ServiceResource

use of io.fabric8.kubernetes.client.dsl.ServiceResource in project strimzi-kafka-operator by strimzi.

the class ServiceMockBuilder method mockCreate.

/**
 * Override Service creation to also create Endpoints
 */
@Override
protected void mockCreate(String resourceName, ServiceResource<Service> resource) {
    when(resource.create(any(Service.class))).thenAnswer(i -> {
        Service argument = i.getArgument(0);
        db.put(resourceName, copyResource(argument));
        LOGGER.debug("create {} (and endpoint) {} ", resourceType, resourceName);
        endpointsDb.put(resourceName, new Endpoints());
        return argument;
    });
}
Also used : Endpoints(io.fabric8.kubernetes.api.model.Endpoints) Service(io.fabric8.kubernetes.api.model.Service)

Example 2 with ServiceResource

use of io.fabric8.kubernetes.client.dsl.ServiceResource in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method applyEntities.

protected void applyEntities(Controller controller, KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception {
    // Apply all items
    for (HasMetadata entity : entities) {
        if (entity instanceof Pod) {
            Pod pod = (Pod) entity;
            controller.applyPod(pod, fileName);
        } else if (entity instanceof Service) {
            Service service = (Service) entity;
            controller.applyService(service, fileName);
        } else if (entity instanceof ReplicationController) {
            ReplicationController replicationController = (ReplicationController) entity;
            controller.applyReplicationController(replicationController, fileName);
        } else if (entity != null) {
            controller.apply(entity, fileName);
        }
    }
    String command = clusterAccess.isOpenShiftImageStream(log) ? "oc" : "kubectl";
    log.info("[[B]]HINT:[[B]] Use the command `%s get pods -w` to watch your pods start up", command);
    Logger serviceLogger = createExternalProcessLogger("[[G]][SVC][[G]] ");
    long serviceUrlWaitTimeSeconds = this.serviceUrlWaitTimeSeconds;
    for (HasMetadata entity : entities) {
        if (entity instanceof Service) {
            Service service = (Service) entity;
            String name = getName(service);
            Resource<Service, DoneableService> serviceResource = kubernetes.services().inNamespace(namespace).withName(name);
            String url = null;
            // lets wait a little while until there is a service URL in case the exposecontroller is running slow
            for (int i = 0; i < serviceUrlWaitTimeSeconds; i++) {
                if (i > 0) {
                    Thread.sleep(1000);
                }
                Service s = serviceResource.get();
                if (s != null) {
                    url = getExternalServiceURL(s);
                    if (Strings.isNotBlank(url)) {
                        break;
                    }
                }
                if (!isExposeService(service)) {
                    break;
                }
            }
            // lets not wait for other services
            serviceUrlWaitTimeSeconds = 1;
            if (Strings.isNotBlank(url) && url.startsWith("http")) {
                serviceLogger.info("" + name + ": " + url);
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) Logger(io.fabric8.maven.docker.util.Logger)

Example 3 with ServiceResource

use of io.fabric8.kubernetes.client.dsl.ServiceResource in project elastest-torm by elastest.

the class K8sService method getServiceIp.

public String getServiceIp(String serviceName, String port, String namespace) {
    String serviceIP = "";
    logger.debug("Nodes in the cluster: {}", client.nodes().list().getItems().size());
    for (NodeAddress address : client.nodes().list().getItems().get(0).getStatus().getAddresses()) {
        logger.debug("Check ip for the cluster 1. Ip type: {}", address.getType());
        if (address.getType().equals("ExternalIP")) {
            serviceIP = address.getAddress();
        }
    }
    if (serviceIP.isEmpty()) {
        ServiceResource<Service, DoneableService> service = client.services().inNamespace(namespace != null && !namespace.isEmpty() ? namespace : DEFAULT_NAMESPACE).withName(serviceName.replace("_", "-"));
        serviceIP = service.getURL(service.get().getSpec().getPorts().get(0).getName()).split(":")[1].replace("//", "");
        logger.debug("External service ip {}", serviceIP);
    }
    return serviceIP;
}
Also used : DoneableService(io.fabric8.kubernetes.api.model.DoneableService) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) NodeAddress(io.fabric8.kubernetes.api.model.NodeAddress)

Example 4 with ServiceResource

use of io.fabric8.kubernetes.client.dsl.ServiceResource in project fabric8-maven-plugin by fabric8io.

the class SpringBootWatcher method getServiceExposeUrl.

private String getServiceExposeUrl(KubernetesClient kubernetes, Set<HasMetadata> resources) throws InterruptedException {
    long serviceUrlWaitTimeSeconds = Configs.asInt(getConfig(Config.serviceUrlWaitTimeSeconds));
    for (HasMetadata entity : resources) {
        if (entity instanceof Service) {
            Service service = (Service) entity;
            String name = KubernetesHelper.getName(service);
            Resource<Service, DoneableService> serviceResource = kubernetes.services().inNamespace(getContext().getClusterConfiguration().getNamespace()).withName(name);
            String url = null;
            // lets wait a little while until there is a service URL in case the exposecontroller is running slow
            for (int i = 0; i < serviceUrlWaitTimeSeconds; i++) {
                if (i > 0) {
                    Thread.sleep(1000);
                }
                Service s = serviceResource.get();
                if (s != null) {
                    url = KubernetesHelper.getOrCreateAnnotations(s).get(Fabric8Annotations.SERVICE_EXPOSE_URL);
                    if (StringUtils.isNotBlank(url)) {
                        break;
                    }
                }
                if (!isExposeService(service)) {
                    break;
                }
            }
            // lets not wait for other services
            serviceUrlWaitTimeSeconds = 1;
            if (StringUtils.isNotBlank(url) && url.startsWith("http")) {
                return url;
            }
        }
    }
    log.info("No exposed service found for connecting the dev tools");
    return null;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) PodLogService(io.fabric8.maven.core.service.PodLogService) Service(io.fabric8.kubernetes.api.model.Service) PortForwardService(io.fabric8.maven.core.service.PortForwardService)

Example 5 with ServiceResource

use of io.fabric8.kubernetes.client.dsl.ServiceResource in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method applyEntities.

protected void applyEntities(KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception {
    // Apply all items
    for (HasMetadata entity : entities) {
        if (entity instanceof Pod) {
            Pod pod = (Pod) entity;
            applyService.applyPod(pod, fileName);
        } else if (entity instanceof Service) {
            Service service = (Service) entity;
            applyService.applyService(service, fileName);
        } else if (entity instanceof ReplicationController) {
            ReplicationController replicationController = (ReplicationController) entity;
            applyService.applyReplicationController(replicationController, fileName);
        } else if (entity != null) {
            applyService.apply(entity, fileName);
        }
    }
    Logger serviceLogger = createExternalProcessLogger("[[G]][SVC][[G]] ");
    long serviceUrlWaitTimeSeconds = this.serviceUrlWaitTimeSeconds;
    for (HasMetadata entity : entities) {
        if (entity instanceof Service) {
            Service service = (Service) entity;
            String name = KubernetesHelper.getName(service);
            Resource<Service, DoneableService> serviceResource = kubernetes.services().inNamespace(namespace).withName(name);
            String url = null;
            // lets wait a little while until there is a service URL in case the exposecontroller is running slow
            for (int i = 0; i < serviceUrlWaitTimeSeconds; i++) {
                if (i > 0) {
                    Thread.sleep(1000);
                }
                Service s = serviceResource.get();
                if (s != null) {
                    url = getExternalServiceURL(s);
                    if (StringUtils.isNotBlank(url)) {
                        break;
                    }
                }
                if (!isExposeService(service)) {
                    break;
                }
            }
            // lets not wait for other services
            serviceUrlWaitTimeSeconds = 1;
            if (StringUtils.isNotBlank(url) && url.startsWith("http")) {
                serviceLogger.info("" + name + ": " + url);
            }
        }
    }
    processCustomEntities(kubernetes, namespace, resources != null ? resources.getCrdContexts() : null, false);
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) ApplyService(io.fabric8.maven.core.service.ApplyService) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) Logger(io.fabric8.maven.docker.util.Logger)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)10 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)2 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)2 Logger (io.fabric8.maven.docker.util.Logger)2 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)1 NodeAddress (io.fabric8.kubernetes.api.model.NodeAddress)1 ApplyService (io.fabric8.maven.core.service.ApplyService)1 PodLogService (io.fabric8.maven.core.service.PodLogService)1 PortForwardService (io.fabric8.maven.core.service.PortForwardService)1