Search in sources :

Example 31 with Endpoint

use of io.fabric8.annotations.Endpoint in project syndesis by syndesisio.

the class SupportUtil method getLogs.

public Optional<Reader> getLogs(String label, String integrationName) {
    return client.pods().list().getItems().stream().filter(pod -> integrationName.equals(pod.getMetadata().getLabels().get(label))).findAny().map(pod -> pod.getMetadata().getName()).flatMap(podName -> {
        PodOperationsImpl pod = (PodOperationsImpl) client.pods().withName(podName);
        try {
            Request request = new Request.Builder().url(pod.getResourceUrl().toString() + "/log?pretty=false&timestamps=true").build();
            Response response = null;
            try {
                response = okHttpClient.newCall(request).execute();
                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected response from /log endpoint: " + response);
                }
                return Optional.of(new RegexBasedMasqueradeReader(new BufferedReader(response.body().charStream()), MASKING_REGEXP));
            } catch (IOException e) {
                // NOPMD
                LOG.error("Error downloading log file for integration {}", integrationName, e);
                if (response != null) {
                    response.close();
                }
            }
        } catch (MalformedURLException e) {
            LOG.error("Error downloading log file for integration {}", integrationName, e);
        }
        return Optional.empty();
    });
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Yaml(org.yaml.snakeyaml.Yaml) DumperOptions(org.yaml.snakeyaml.DumperOptions) Service(org.springframework.stereotype.Service) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) IntegrationOverview(io.syndesis.common.model.integration.IntegrationOverview) Response(okhttp3.Response) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) ZipEntry(java.util.zip.ZipEntry) HttpClientUtils(io.fabric8.kubernetes.client.utils.HttpClientUtils) OutputStream(java.io.OutputStream) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag) Request(okhttp3.Request) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) NamespacedOpenShiftClient(io.fabric8.openshift.client.NamespacedOpenShiftClient) FileOutputStream(java.io.FileOutputStream) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) IOException(java.io.IOException) StreamingOutput(javax.ws.rs.core.StreamingOutput) ListResult(io.syndesis.common.model.ListResult) FileUtils(org.apache.commons.io.FileUtils) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) IOUtils(org.apache.commons.io.IOUtils) Stream(java.util.stream.Stream) OkHttpClient(okhttp3.OkHttpClient) StringReader(java.io.StringReader) IntegrationSupportHandler(io.syndesis.server.endpoint.v1.handler.integration.support.IntegrationSupportHandler) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) IntegrationHandler(io.syndesis.server.endpoint.v1.handler.integration.IntegrationHandler) BufferedReader(java.io.BufferedReader) UriInfo(javax.ws.rs.core.UriInfo) InputStream(java.io.InputStream) Response(okhttp3.Response) MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl)

Example 32 with Endpoint

use of io.fabric8.annotations.Endpoint in project flink by apache.

the class Fabric8FlinkKubeClient method getLoadBalancerRestEndpoint.

private Optional<Endpoint> getLoadBalancerRestEndpoint(LoadBalancerStatus loadBalancer, int restPort) {
    boolean hasIngress = loadBalancer.getIngress() != null && !loadBalancer.getIngress().isEmpty();
    String address;
    if (hasIngress) {
        address = loadBalancer.getIngress().get(0).getIp();
        // Use hostname when the ip address is null
        if (address == null || address.isEmpty()) {
            address = loadBalancer.getIngress().get(0).getHostname();
        }
    } else {
        // Use node port. Node port is accessible on any node within kubernetes cluster. We'll
        // only consider IPs with the configured address type.
        address = internalClient.nodes().list().getItems().stream().flatMap(node -> node.getStatus().getAddresses().stream()).filter(nodeAddress -> nodePortAddressType.name().equals(nodeAddress.getType())).map(NodeAddress::getAddress).filter(ip -> !ip.isEmpty()).findAny().orElse(null);
        if (address == null) {
            LOG.warn("Unable to find any node ip with type [{}]. Please see [{}] config option for more details.", nodePortAddressType, KubernetesConfigOptions.REST_SERVICE_EXPOSED_NODE_PORT_ADDRESS_TYPE.key());
        }
    }
    boolean noAddress = address == null || address.isEmpty();
    return noAddress ? Optional.empty() : Optional.of(new Endpoint(address, restPort));
}
Also used : LoadBalancerStatus(io.fabric8.kubernetes.api.model.LoadBalancerStatus) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceType(org.apache.flink.kubernetes.kubeclient.services.ServiceType) LoggerFactory(org.slf4j.LoggerFactory) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ExceptionUtils(org.apache.flink.util.ExceptionUtils) KubernetesService(org.apache.flink.kubernetes.kubeclient.resources.KubernetesService) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) KubernetesPod(org.apache.flink.kubernetes.kubeclient.resources.KubernetesPod) NamespacedKubernetesClient(io.fabric8.kubernetes.client.NamespacedKubernetesClient) Map(java.util.Map) ExternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.ExternalServiceDecorator) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) KubernetesUtils(org.apache.flink.kubernetes.utils.KubernetesUtils) KubernetesConfigOptions(org.apache.flink.kubernetes.configuration.KubernetesConfigOptions) CompletionException(java.util.concurrent.CompletionException) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) ExecutorUtils(org.apache.flink.util.ExecutorUtils) List(java.util.List) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) Optional(java.util.Optional) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) Constants(org.apache.flink.kubernetes.utils.Constants) KubernetesPodsWatcher(org.apache.flink.kubernetes.kubeclient.resources.KubernetesPodsWatcher) CompletableFuture(java.util.concurrent.CompletableFuture) KubernetesWatch(org.apache.flink.kubernetes.kubeclient.resources.KubernetesWatch) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) KubernetesLeaderElectionConfiguration(org.apache.flink.kubernetes.configuration.KubernetesLeaderElectionConfiguration) KubernetesConfigMapSharedInformer(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMapSharedInformer) NodeAddress(io.fabric8.kubernetes.api.model.NodeAddress) Service(io.fabric8.kubernetes.api.model.Service) ExecutorService(java.util.concurrent.ExecutorService) InternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.InternalServiceDecorator) Logger(org.slf4j.Logger) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) Configuration(org.apache.flink.configuration.Configuration) Pod(io.fabric8.kubernetes.api.model.Pod) File(java.io.File) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) TimeUnit(java.util.concurrent.TimeUnit) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Collections(java.util.Collections) NodeAddress(io.fabric8.kubernetes.api.model.NodeAddress) IntOrString(io.fabric8.kubernetes.api.model.IntOrString)

Example 33 with Endpoint

use of io.fabric8.annotations.Endpoint in project flink by apache.

the class Fabric8FlinkKubeClient method getRestEndpoint.

@Override
public Optional<Endpoint> getRestEndpoint(String clusterId) {
    Optional<KubernetesService> restService = getService(KubernetesService.ServiceType.REST_SERVICE, clusterId);
    if (!restService.isPresent()) {
        return Optional.empty();
    }
    final Service service = restService.get().getInternalResource();
    final int restPort = getRestPortFromExternalService(service);
    final KubernetesConfigOptions.ServiceExposedType serviceExposedType = ServiceType.classify(service);
    // Return the external service.namespace directly when using ClusterIP.
    if (serviceExposedType.isClusterIP()) {
        return Optional.of(new Endpoint(ExternalServiceDecorator.getNamespacedExternalServiceName(clusterId, namespace), restPort));
    }
    return getRestEndPointFromService(service, restPort);
}
Also used : KubernetesService(org.apache.flink.kubernetes.kubeclient.resources.KubernetesService) Service(io.fabric8.kubernetes.api.model.Service) ExecutorService(java.util.concurrent.ExecutorService) KubernetesConfigOptions(org.apache.flink.kubernetes.configuration.KubernetesConfigOptions) KubernetesService(org.apache.flink.kubernetes.kubeclient.resources.KubernetesService)

Example 34 with Endpoint

use of io.fabric8.annotations.Endpoint in project fabric8 by fabric8io.

the class SessionServicesAreReady method isEndpointAvailable.

/**
 * Checks if there is an endpoint for the service available.
 * @param s The target service.
 * @return  Returns true if a connection to at least one of the endpoints is possible.
 */
private boolean isEndpointAvailable(Service s) {
    String serviceStatus = null;
    boolean result = false;
    String sid = getName(s);
    String namespace = session.getNamespace();
    Endpoints endpoints = kubernetesClient.endpoints().inNamespace(namespace).withName(sid).get();
    ServiceSpec spec = s.getSpec();
    if (endpoints != null && spec != null) {
        List<EndpointSubset> subsets = endpoints.getSubsets();
        if (subsets != null) {
            for (EndpointSubset subset : subsets) {
                List<EndpointAddress> addresses = subset.getAddresses();
                if (addresses != null) {
                    for (EndpointAddress address : addresses) {
                        String ip = address.getIp();
                        String addr = ip;
                        /*
    TODO v1beta2...
                            String addr = endpoit.substring(0, endpoit.indexOf(":"));
                            Integer port = Integer.parseInt(endpoit.substring(endpoit.indexOf(":") + 1));
*/
                        List<ServicePort> ports = spec.getPorts();
                        for (ServicePort port : ports) {
                            Integer portNumber = port.getPort();
                            if (portNumber != null && portNumber > 0) {
                                if (configuration.isWaitForServiceConnectionEnabled()) {
                                    try (Socket socket = new Socket()) {
                                        socket.connect(new InetSocketAddress(ip, portNumber), (int) configuration.getWaitForServiceConnectionTimeout());
                                        serviceStatus = "Service: " + sid + " is ready. Provider:" + addr + ".";
                                        return true;
                                    } catch (Exception e) {
                                        serviceStatus = "Service: " + sid + " is not ready! in namespace " + namespace + ". Error: " + e.getMessage();
                                    } finally {
                                        session.getLogger().warn(serviceStatus);
                                    }
                                } else {
                                    serviceStatus = "Service: " + sid + " is ready. Not testing connecting to it!. Provider:" + addr + ".";
                                    session.getLogger().warn(serviceStatus);
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    session.getLogger().warn("Service: " + sid + " has no valid endpoints");
    return result;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) InetSocketAddress(java.net.InetSocketAddress) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) EndpointSubset(io.fabric8.kubernetes.api.model.EndpointSubset) Socket(java.net.Socket) EndpointAddress(io.fabric8.kubernetes.api.model.EndpointAddress)

Example 35 with Endpoint

use of io.fabric8.annotations.Endpoint in project fabric8 by fabric8io.

the class ServicePodsAssert method hasEndpointOrReadyPod.

/**
 * Asserts that either this service has a valid Endpoint or that a pod is Ready for a period of time
 */
public ServicePodsAssert hasEndpointOrReadyPod(long notReadyTimeoutMS, long readyPeriodMS) {
    EndpointsList list = client.endpoints().withLabels(getLabels(actual)).list();
    if (list != null) {
        List<Endpoints> items = list.getItems();
        if (items.size() > 0) {
            return this;
        }
    }
    pods().isPodReadyForPeriod(notReadyTimeoutMS, readyPeriodMS);
    return this;
}
Also used : EndpointsList(io.fabric8.kubernetes.api.model.EndpointsList) Endpoints(io.fabric8.kubernetes.api.model.Endpoints)

Aggregations

Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)12 Service (io.fabric8.kubernetes.api.model.Service)9 ArrayList (java.util.ArrayList)9 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)7 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)7 URL (java.net.URL)7 BeforeTest (org.testng.annotations.BeforeTest)7 Test (org.testng.annotations.Test)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 IOException (java.io.IOException)5 MalformedURLException (java.net.MalformedURLException)5 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)4 ServiceDiscoveryException (org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 EndpointsList (io.fabric8.kubernetes.api.model.EndpointsList)3 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)3 File (java.io.File)3 Collection (java.util.Collection)3 Configuration (io.fabric8.annotations.Configuration)2 Endpoint (io.fabric8.annotations.Endpoint)2