使用kubernetes-client exec()

时间:2017-06-06 13:19:49

标签: java kubernetes fabric8

我使用kubernetes-client尝试从pod中复制目录,但是我对来自stdout的输入流做错了。我尝试java.io.IOException: Pipe broken时遇到read()异常。我很确定根本没有数据流。我想知道我是否需要在单独的线程上读取InputStream?

流创建如下:

public InputStream copyFiles(String containerId,
                             String folderName) {

    ExecWatch exec = client.pods().withName(containerId).redirectingOutput().exec("tar -C " + folderName + " -c");

    // We need to wrap the InputStream so that when the stdout is closed, then the underlying ExecWatch is closed
    // also. This will cleanup any Websockets connections.
    ChainedCloseInputStreamWrapper inputStreamWrapper = new ChainedCloseInputStreamWrapper(exec.getOutput(), exec);

    return inputStreamWrapper;
}

并且在此函数中处理InputStream

void copyVideos(final String containerId) {
    TarArchiveInputStream tarStream = new TarArchiveInputStream(containerClient.copyFiles(containerId, "/videos/"));
    TarArchiveEntry entry;
    boolean videoWasCopied = false;
    try {
        while ((entry = tarStream.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }
            String fileExtension = entry.getName().substring(entry.getName().lastIndexOf('.'));
            testInformation.setFileExtension(fileExtension);
            File videoFile = new File(testInformation.getVideoFolderPath(), testInformation.getFileName());
            File parent = videoFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            OutputStream outputStream = new FileOutputStream(videoFile);
            IOUtils.copy(tarStream, outputStream);
            outputStream.close();
            videoWasCopied = true;
            LOGGER.log(Level.INFO, "{0} Video file copied to: {1}/{2}", new Object[]{getId(),
                    testInformation.getVideoFolderPath(), testInformation.getFileName()});
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, getId() + " Error while copying the video", e);
        ga.trackException(e);
    } finally {
        if (!videoWasCopied) {
            testInformation.setVideoRecorded(false);
        }
    }

}

一旦InputStream关闭,InputStream Wrapper类就在那里关闭ExecWatch,它看起来像这样:

private static class ChainedCloseInputStreamWrapper extends InputStream {

    private InputStream delegate;
    private Closeable resourceToClose;

    public ChainedCloseInputStreamWrapper(InputStream delegate, Closeable resourceToClose) {
        this.delegate = delegate;
        this.resourceToClose = resourceToClose;
    }

    @Override
    public int read() throws IOException {
        return delegate.read();
    }

    public int available() throws IOException {
        return delegate.available();
    }

    public void close() throws IOException {
        logger.info("Shutdown called!");
        delegate.close();

        // Close our dependent resource
        resourceToClose.close();
    }

    public boolean equals(Object o) {
        return delegate.equals(o);
    }

    public int hashCode() {
        return delegate.hashCode();
    }

    public int read(byte[] array) throws IOException {
        return delegate.read(array);
    }

    public int read(byte[] array,
                    int n,
                    int n2) throws IOException {
        return delegate.read(array, n, n2);
    }

    public long skip(long n) throws IOException {
        return delegate.skip(n);
    }

    public void mark(int n) {
        delegate.mark(n);
    }

    public void reset() throws IOException {
        delegate.reset();
    }

    public boolean markSupported() {
        return delegate.markSupported();
    }

    public String toString() {
        return delegate.toString();
    }

}

1 个答案:

答案 0 :(得分:0)

原来我的tar命令错了,所以导致失败并且stdout PipeInputStream死锁。我设法找到了解决僵局的方法。但失败的主要原因是我忘了告诉焦油实际做某事!我至少需要一个“。”包括当前目录。