io.fabric8.kubernetes.client.dsl.LogWatch Java Examples

The following examples show how to use io.fabric8.kubernetes.client.dsl.LogWatch. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KubernetesLauncher.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Log the last lines of containers logs
 */
private void logLastLines(@CheckForNull List<ContainerStatus> containers, String podId, String namespace, KubernetesSlave slave,
        Map<String, Integer> errors, KubernetesClient client) {
    if (containers != null) {
        for (ContainerStatus containerStatus : containers) {
            String containerName = containerStatus.getName();
            PrettyLoggable<String, LogWatch> tailingLines = client.pods().inNamespace(namespace).withName(podId)
                    .inContainer(containerStatus.getName()).tailingLines(30);
            String log = tailingLines.getLog();
            if (!StringUtils.isBlank(log)) {
                String msg = errors != null ? String.format(" exited with error %s", errors.get(containerName)) : "";
                LOGGER.log(Level.SEVERE, "Error in provisioning; agent={0}, template={1}. Container {2}{3}. Logs: {4}",
                        new Object[]{slave, slave.getTemplate(), containerName, msg, tailingLines.getLog()});
            }
        }
    }
}
 
Example #2
Source File: BuildOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  try {
    URL url = new URL(URLUtils.join(getResourceUrl().toString(), getLogParameters() + "&follow=true"));
    Request request = new Request.Builder().url(url).get().build();
    final LogWatchCallback callback = new LogWatchCallback(out);
    OkHttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
    clone.newCall(request).enqueue(callback);
    callback.waitUntilReady();
    return callback;
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(forOperationType("watchLog"), t);
  }
}
 
Example #3
Source File: PodLogExample.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  if (args.length < 1) {
    System.out.println("Usage: podName [master] [namespace]");
    return;
  }
  String podName = args[0];
  String namespace = "default";
  String master = "https://localhost:8443/";

  if (args.length > 1) {
    master = args[1];
  }
  if (args.length > 2) {
    namespace = args[2];
  }

  System.out.println("Log of pod " + podName + " in " + namespace + " is:");
  System.out.println("----------------------------------------------------------------");

  Config config = new ConfigBuilder().withMasterUrl(master).build();
  try (KubernetesClient client = new DefaultKubernetesClient(config);
       LogWatch watch = client.pods().inNamespace(namespace).withName(podName).tailingLines(10).watchLog(System.out)) {
    Thread.sleep(5 * 1000);
  } catch (Exception e) {
    logger.error(e.getMessage(), e);
  }
}
 
Example #4
Source File: StatefulSetOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  List<PodResource<Pod, DoneablePod>> podResources = doGetLog(false);
  if (podResources.size() > 1) {
    throw new KubernetesClientException("Watching logs is not supported for multicontainer jobs");
  } else if (podResources.size() == 1) {
    return podResources.get(0).watchLog(out);
  }
  return null;
}
 
Example #5
Source File: ReplicationControllerOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  List<PodResource<Pod, DoneablePod>> podResources = doGetLog(false);
  if (podResources.size() > 1) {
    throw new KubernetesClientException("Watching logs is not supported for multicontainer jobs");
  } else if (podResources.size() == 1) {
    return podResources.get(0).watchLog(out);
  }
  return null;
}
 
Example #6
Source File: ReplicaSetOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  List<PodResource<Pod, DoneablePod>> podResources = doGetLog(false);
  if (podResources.size() > 1) {
    throw new KubernetesClientException("Watching logs is not supported for multicontainer jobs");
  } else if (podResources.size() == 1) {
    return podResources.get(0).watchLog(out);
  }
  return null;
}
 
Example #7
Source File: DeploymentConfigOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  List<RollableScalableResource<ReplicaSet, DoneableReplicaSet>> podResources = doGetLog();
  if (podResources.size() > 1) {
    throw new KubernetesClientException("Watching logs is not supported for multicontainer jobs");
  } else if (podResources.size() == 1) {
    return podResources.get(0).watchLog(out);
  }
  return null;
}
 
Example #8
Source File: JobOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  List<PodResource<Pod, DoneablePod>> podResources = doGetLog(false);
  if (podResources.size() > 1) {
    throw new KubernetesClientException("Watching logs is not supported for multicontainer jobs");
  } else if (podResources.size() == 1) {
    return podResources.get(0).watchLog(out);
  }
  return null;
}
 
Example #9
Source File: KubernetesFacade.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
public LogWatch watchLogs(String podName) {
    LogWatch watch = client.pods().withName(podName).watchLog();
    synchronized (closeables) {
        closeables.add(watch);
    }
    return watch;
}
 
Example #10
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  try {
    URL url = new URL(URLUtils.join(getResourceUrl().toString(), getLogParameters() + "&follow=true"));
    Request request = new Request.Builder().url(url).get().build();
    final LogWatchCallback callback = new LogWatchCallback(out);
    OkHttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
    clone.newCall(request).enqueue(callback);
    callback.waitUntilReady();
    return callback;
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(forOperationType("watchLog"), t);
  }
}
 
Example #11
Source File: KubernetesHandler.java    From apollo with Apache License 2.0 5 votes vote down vote up
public LogWatch getLogWatch(String podName, String containerName) {

        return kubernetesClient
                .pods()
                .inNamespace(environment.getKubernetesNamespace())
                .withName(podName)
                .inContainer(containerName)
                .tailingLines(500)
                .watchLog();
    }
 
Example #12
Source File: KubernetesHelper.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static void printLogsAsync(LogWatch logWatcher, final String failureMessage, final CountDownLatch terminateLatch, final KitLogger log) {
    final InputStream in = logWatcher.getOutput();
    Thread thread = new Thread() {
        @Override
        public void run() {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
                while (true) {
                    String line = reader.readLine();
                    if (line == null) {
                        return;
                    }
                    if (terminateLatch.getCount() <= 0L) {
                        return;
                    }
                    log.info("[[s]]%s", line);
                }
            } catch (IOException e) {
                // Check again the latch which could be already count down to zero in between
                // so that an IO exception occurs on read
                if (terminateLatch.getCount() > 0L) {
                    log.error("%s : %s", failureMessage, e);
                }
            }
        }
    };
    thread.start();
}
 
Example #13
Source File: KubernetesClientUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static void printLogsAsync(LogWatch logWatcher, final String failureMessage, final CountDownLatch terminateLatch, final KitLogger log) {
    final InputStream in = logWatcher.getOutput();
    Thread thread = new Thread() {
        @Override
        public void run() {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
                while (true) {
                    String line = reader.readLine();
                    if (line == null) {
                        return;
                    }
                    if (terminateLatch.getCount() <= 0L) {
                        return;
                    }
                    log.info("[[s]]%s", line);
                }
            } catch (IOException e) {
                // Check again the latch which could be already count down to zero in between
                // so that an IO exception occurs on read
                if (terminateLatch.getCount() > 0L) {
                    log.error("%s : %s", failureMessage, e);
                }
            }
        }
    };
    thread.start();
}
 
Example #14
Source File: PodLogService.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private void watchLog(final LogWatch logWatcher, String podName, final String failureMessage, String ctrlCMessage, String containerName) {
    context.getNewPodLog().info("Tailing log of pod: " + podName + containerNameMessage(containerName));
    context.getNewPodLog().info("Press Ctrl-C to " + ctrlCMessage);
    context.getNewPodLog().info("");

    KubernetesHelper.printLogsAsync(logWatcher, failureMessage, this.logWatchTerminateLatch, log);
}
 
Example #15
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public TailPrettyLoggable<String, LogWatch> sinceTime(String sinceTimestamp) {
    return new PodOperationsImpl(getContext().withSinceTimestamp(sinceTimestamp));
}
 
Example #16
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public BytesLimitTerminateTimeTailPrettyLoggable<String, LogWatch> usingTimestamps() {
  return new PodOperationsImpl(getContext().withTimestamps(true));
}
 
Example #17
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public TimeTailPrettyLoggable<String, LogWatch> terminated() {
    return new PodOperationsImpl(getContext().withTerminatedStatus(true));
}
 
Example #18
Source File: BuildOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public BytesLimitTerminateTimeTailPrettyLoggable<String, LogWatch> usingTimestamps() {
  return new BuildOperationsImpl(getContext().withTimestamps(true));
}
 
Example #19
Source File: DeploymentConfigOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public LogWatch watchLog() {
  return watchLog(null);
}
 
Example #20
Source File: BuildOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public LogWatch watchLog() {
  return watchLog(null);
}
 
Example #21
Source File: BuildOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Loggable<String, LogWatch> withPrettyOutput() {
  return new BuildOperationsImpl(getContext().withPrettyOutput(true));
}
 
Example #22
Source File: BuildOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public TimeTailPrettyLoggable<String, LogWatch> terminated() {
  return new BuildOperationsImpl(getContext().withTerminatedStatus(true));
}
 
Example #23
Source File: BuildOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public TailPrettyLoggable<String, LogWatch> sinceTime(String sinceTimestamp) {
  return new BuildOperationsImpl(getContext().withSinceTimestamp(sinceTimestamp));
}
 
Example #24
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public PrettyLoggable<String, LogWatch> tailingLines(int withTailingLines) {
    return new PodOperationsImpl(getContext().withTailingLines(withTailingLines));
}
 
Example #25
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Loggable<String, LogWatch> withPrettyOutput() {
    return new PodOperationsImpl(getContext().withPrettyOutput(true));
}
 
Example #26
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public ContainerResource<String, LogWatch, InputStream, PipedOutputStream, OutputStream, PipedInputStream, String, ExecWatch, Boolean, InputStream, Boolean> inContainer(String containerId) {
    return new PodOperationsImpl(getContext().withContainerId(containerId));
}
 
Example #27
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public LogWatch watchLog() {
  return watchLog(null);
}
 
Example #28
Source File: ReplicationControllerOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public LogWatch watchLog() {
  return watchLog(null);
}
 
Example #29
Source File: StatefulSetOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public LogWatch watchLog() {
  return watchLog(null);
}
 
Example #30
Source File: ReplicaSetOperationsImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public LogWatch watchLog() {
  return watchLog(null);
}