io.alauda.kubernetes.client.dsl.internal.PodOperationsImpl Maven / Gradle / Ivy
/**
* Copyright (C) 2018 Alauda
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.alauda.kubernetes.client.dsl.internal;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Reader;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import io.alauda.kubernetes.api.model.DoneablePod;
import io.alauda.kubernetes.api.model.Pod;
import io.alauda.kubernetes.api.model.PodList;
import io.alauda.kubernetes.client.Config;
import io.alauda.kubernetes.client.KubernetesClientException;
import io.alauda.kubernetes.client.LocalPortForward;
import io.alauda.kubernetes.client.PortForward;
import io.alauda.kubernetes.client.dsl.base.HasMetadataOperation;
import io.alauda.kubernetes.client.dsl.PodResource;
import io.alauda.kubernetes.client.dsl.ContainerResource;
import io.alauda.kubernetes.client.dsl.ExecListenable;
import io.alauda.kubernetes.client.dsl.ExecListener;
import io.alauda.kubernetes.client.dsl.ExecWatch;
import io.alauda.kubernetes.client.dsl.Execable;
import io.alauda.kubernetes.client.dsl.LogWatch;
import io.alauda.kubernetes.client.dsl.Loggable;
import io.alauda.kubernetes.client.dsl.PrettyLoggable;
import io.alauda.kubernetes.client.dsl.TailPrettyLoggable;
import io.alauda.kubernetes.client.dsl.TimeTailPrettyLoggable;
import io.alauda.kubernetes.client.dsl.BytesLimitTerminateTimeTailPrettyLoggable;
import io.alauda.kubernetes.client.dsl.TtyExecErrorable;
import io.alauda.kubernetes.client.dsl.TtyExecOutputErrorable;
import io.alauda.kubernetes.client.dsl.TtyExecable;
import io.alauda.kubernetes.client.utils.URLUtils;
import okhttp3.*;
public class PodOperationsImpl extends HasMetadataOperation> implements PodResource {
private static final String[] EMPTY_COMMAND = {"/bin/sh", "-i"};
private final String containerId;
private final InputStream in;
private final OutputStream out;
private final OutputStream err;
private final PipedOutputStream inPipe;
private final PipedInputStream outPipe;
private final PipedInputStream errPipe;
private final boolean withTTY;
private final boolean withTerminatedStatus;
private final boolean withTimestamps;
private final String sinceTimestamp;
private final Integer sinceSeconds;
private final Integer withTailingLines;
private final boolean withPrettyOutput;
private final ExecListener execListener;
private final Integer limitBytes;
public PodOperationsImpl(OkHttpClient client, Config config, String namespace) {
this(client, config, null, namespace, null, true, null, null, false, -1, new TreeMap(), new TreeMap(), new TreeMap(), new TreeMap(), new TreeMap());
}
public PodOperationsImpl(OkHttpClient client, Config config, String apiVersion, String namespace, String name, Boolean cascading, Pod item, String resourceVersion, Boolean reloadingFromServer, long gracePeriodSeconds, Map labels, Map labelsNot, Map labelsIn, Map labelsNotIn, Map fields) {
this(client, config, apiVersion, namespace, name, cascading, item, resourceVersion, reloadingFromServer, gracePeriodSeconds, labels, labelsNot, labelsIn, labelsNotIn, fields,
null, null, null, null, null, null, null, false, false, false, null, null, null, false, null, null);
}
public PodOperationsImpl(OkHttpClient client, Config config, String apiVersion, String namespace, String name, Boolean cascading, Pod item, String resourceVersion, Boolean reloadingFromServer, long gracePeriodSeconds, Map labels, Map labelsNot, Map labelsIn, Map labelsNotIn, Map fields, String containerId, InputStream in, PipedOutputStream inPipe, OutputStream out, PipedInputStream outPipe, OutputStream err, PipedInputStream errPipe, boolean withTTY, boolean withTerminatedStatus, boolean withTimestamps, String sinceTimestamp, Integer sinceSeconds, Integer withTailingLines, boolean withPrettyOutput, ExecListener execListener, Integer limitBytes) {
super(client, config, null, apiVersion, "pods", namespace, name, cascading, item, resourceVersion, reloadingFromServer, gracePeriodSeconds, labels, labelsNot, labelsIn, labelsNotIn, fields);
this.containerId = containerId;
this.in = in;
this.inPipe = inPipe;
this.out = out;
this.outPipe = outPipe;
this.err = err;
this.errPipe = errPipe;
this.withTTY = withTTY;
this.withTerminatedStatus = withTerminatedStatus;
this.withTimestamps = withTimestamps;
this.sinceTimestamp = sinceTimestamp;
this.sinceSeconds = sinceSeconds;
this.withTailingLines = withTailingLines;
this.withPrettyOutput = withPrettyOutput;
this.execListener = execListener;
this.limitBytes =limitBytes;
}
protected String getLogParameters() {
StringBuilder sb = new StringBuilder();
sb.append("log?pretty=").append(withPrettyOutput);
if (containerId != null && !containerId.isEmpty()) {
sb.append("&container=").append(containerId);
}
if (withTerminatedStatus) {
sb.append("&previous=true");
}
if (sinceSeconds != null) {
sb.append("&sinceSeconds=").append(sinceSeconds);
} else if (sinceTimestamp != null) {
sb.append("&sinceTime=").append(sinceTimestamp);
}
if (withTailingLines != null) {
sb.append("&tailLines=").append(withTailingLines);
}
if (limitBytes != null) {
sb.append("&limitBytes=").append(limitBytes);
}
return sb.toString();
}
protected ResponseBody doGetLog(){
try {
URL url = new URL(URLUtils.join(getResourceUrl().toString(), getLogParameters()));
Request.Builder requestBuilder = new Request.Builder().get().url(url);
Request request = requestBuilder.build();
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
assertResponseCode(request, response);
return body;
} catch (Throwable t) {
throw KubernetesClientException.launderThrowable(forOperationType("doGetLog"), t);
}
}
@Override
public String getLog() {
try(ResponseBody body = doGetLog()) {
return body.string();
} catch (IOException e) {
throw KubernetesClientException.launderThrowable(forOperationType("getLog"), e);
}
}
/**
* Returns an unclosed Reader. It's the caller responsibility to close it.
* @return Reader
*/
@Override
public Reader getLogReader() {
return doGetLog().charStream();
}
@Override
public String getLog(Boolean isPretty) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, isPretty, execListener, limitBytes).getLog();
}
@Override
public LogWatch watchLog() {
return watchLog(null);
}
@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);
}
}
@Override
public PortForward portForward(int port, ReadableByteChannel in, WritableByteChannel out) {
try {
return new PortForwarderWebsocket(client).forward(getResourceUrl(), port, in, out);
} catch (Throwable t) {
throw KubernetesClientException.launderThrowable(t);
}
}
@Override
public LocalPortForward portForward(int port) {
try {
return new PortForwarderWebsocket(client).forward(getResourceUrl(), port);
} catch (Throwable t) {
throw KubernetesClientException.launderThrowable(t);
}
}
@Override
public LocalPortForward portForward(int port, int localPort) {
try {
return new PortForwarderWebsocket(client).forward(getResourceUrl(), port, localPort);
} catch (Throwable t) {
throw KubernetesClientException.launderThrowable(t);
}
}
@Override
public ContainerResource inContainer(String containerId) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public ExecWatch exec(String... command) {
StringBuilder sb = new StringBuilder();
String[] actualCommands = command.length >= 1 ? command : EMPTY_COMMAND;
sb.append("exec?command=");
boolean first = true;
for (String cmd : actualCommands) {
if (first) {
first = false;
} else {
sb.append("&command=");
}
sb.append(cmd);
}
if (containerId != null && !containerId.isEmpty()) {
sb.append("&container=").append(containerId);
}
if (withTTY) {
sb.append("&tty=true");
}
if (in != null || inPipe != null) {
sb.append("&stdin=true");
}
if (out != null || outPipe != null) {
sb.append("&stdout=true");
}
if (err != null || errPipe != null) {
sb.append("&stderr=true");
}
try {
URL url = new URL(URLUtils.join(getResourceUrl().toString(), sb.toString()));
Request.Builder r = new Request.Builder().url(url).get();
OkHttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
final ExecWebSocketListener execWebSocketListener = new ExecWebSocketListener(in, out, err, inPipe, outPipe, errPipe, execListener);
clone.newWebSocket(r.build(), execWebSocketListener);
execWebSocketListener.waitUntilReady();
return execWebSocketListener;
} catch (Throwable t) {
throw KubernetesClientException.launderThrowable(forOperationType("exec"), t);
}
}
@Override
public TtyExecOutputErrorable readingInput(InputStream in) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TtyExecOutputErrorable writingInput(PipedOutputStream inPipe) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TtyExecOutputErrorable redirectingInput() {
return writingInput(new PipedOutputStream());
}
@Override
public TtyExecErrorable writingOutput(OutputStream out) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TtyExecErrorable readingOutput(PipedInputStream outPipe) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TtyExecErrorable redirectingOutput() {
return readingOutput(new PipedInputStream());
}
@Override
public TtyExecable writingError(OutputStream err) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TtyExecable readingError(PipedInputStream errPipe) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TtyExecable redirectingError() {
return readingError(new PipedInputStream());
}
@Override
public ExecListenable withTTY() {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, true, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public Loggable withPrettyOutput() {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, true, execListener, limitBytes);
}
@Override
public PrettyLoggable tailingLines(int withTailingLines) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TailPrettyLoggable sinceTime(String sinceTimestamp) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TailPrettyLoggable sinceSeconds(int sinceSeconds) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public TimeTailPrettyLoggable terminated() {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, true, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public Execable usingListener(ExecListener execListener) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
@Override
public BytesLimitTerminateTimeTailPrettyLoggable limitBytes(int limitBytes) {
return new PodOperationsImpl(client, getConfig(), apiVersion, namespace, name, isCascading(), getItem(), getResourceVersion(), isReloadingFromServer(), getGracePeriodSeconds(), getLabels(), getLabelsNot(), getLabelsIn(), getLabelsNotIn(), getFields(), containerId, in, inPipe, out, outPipe, err, errPipe, withTTY, withTerminatedStatus, withTimestamps, sinceTimestamp, sinceSeconds, withTailingLines, withPrettyOutput, execListener, limitBytes);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy