Search in sources :

Example 1 with PodOperationsImpl

use of io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl in project syndesis by syndesisio.

the class KubernetesSupport method watchLog.

/*
     * Feeds the controller of the given podName to the callback handler for processing.
     *
     * We do this instead of using the watchLog() feature of the k8s client lib because it really sucks due to:
     *  1. You can't configure the timestamps option or the sinceTime option.  Need to resume log downloads.
     *  2. It seems to need extra threads..
     *  3. It might be hiding some of the http failure conditions.
     *
     */
protected void watchLog(String podName, Consumer<InputStream> handler, String sinceTime, Executor executor) throws IOException {
    try {
        PodOperationsImpl pod = (PodOperationsImpl) client.pods().withName(podName);
        StringBuilder url = new StringBuilder().append(pod.getResourceUrl().toString()).append("/log?pretty=false&follow=true&timestamps=true");
        if (sinceTime != null) {
            url.append("&sinceTime=");
        }
        Request request = new Request.Builder().url(new URL(url.toString())).get().build();
        OkHttpClient clone = okHttpClient.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
        clone.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                LOG.info("Failure occurred getting  controller for pod: {},", podName, e);
                handler.accept(null);
            }

            @Override
            public void onResponse(final Call call, final Response response) throws IOException {
                executor.execute(() -> {
                    try {
                        if (response.code() == 200) {
                            handler.accept(response.body().byteStream());
                        } else {
                            LOG.info("Failure occurred while processing controller for pod: {}, http status: {}, details: {}", podName, response.code(), response.body().string());
                            handler.accept(null);
                        }
                    } catch (IOException e) {
                        LOG.error("Unexpected Error", e);
                    }
                });
            }
        });
    } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException t) {
        throw new IOException("Unexpected Error", t);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl) URL(java.net.URL)

Example 2 with PodOperationsImpl

use of io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl 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)

Aggregations

PodOperationsImpl (io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl)2 IOException (java.io.IOException)2 OkHttpClient (okhttp3.OkHttpClient)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 HttpClientUtils (io.fabric8.kubernetes.client.utils.HttpClientUtils)1 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)1 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)1 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)1 NamespacedOpenShiftClient (io.fabric8.openshift.client.NamespacedOpenShiftClient)1 ListResult (io.syndesis.common.model.ListResult)1 IntegrationOverview (io.syndesis.common.model.integration.IntegrationOverview)1 IntegrationHandler (io.syndesis.server.endpoint.v1.handler.integration.IntegrationHandler)1 IntegrationSupportHandler (io.syndesis.server.endpoint.v1.handler.integration.support.IntegrationSupportHandler)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1