Search in sources :

Example 6 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 7 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 8 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 9 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 10 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)18 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)6 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)6 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)6 Test (org.testng.annotations.Test)6 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)4 Pod (io.fabric8.kubernetes.api.model.Pod)4 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)2 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)2 IntOrStringBuilder (io.fabric8.kubernetes.api.model.IntOrStringBuilder)2 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)2 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)2 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)2 Logger (io.fabric8.maven.docker.util.Logger)2 ServerServiceBuilder (org.eclipse.che.workspace.infrastructure.kubernetes.server.ServerServiceBuilder)2 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)1 io.fabric8.kubernetes.api.model (io.fabric8.kubernetes.api.model)1