Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Attach #24

Merged
merged 1 commit into from
Jul 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2017 The Kubernetes Authors.
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.kubernetes.client.examples;

import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.Attach;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.models.V1PodList;
import io.kubernetes.client.util.Config;

import com.google.common.io.ByteStreams;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;

/**
* A simple example of how to use the Java API
*
* Easiest way to run this:
* mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.AttachExample"
*
* From inside $REPO_DIR/examples
*/
public class AttachExample {
public static void main(String[] args) throws IOException, ApiException, InterruptedException {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);

Attach attach = new Attach();
final Attach.AttachResult result = attach.attach("default", "nginx-2371676037-czqx3", true);

new Thread(new Runnable() {
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
OutputStream output = result.getStandardInputStream();
try {
while (true) {
String line = in.readLine();
output.write(line.getBytes());
output.write('\n');
output.flush();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
public void run() {
try {
ByteStreams.copy(result.getStandardOutputStream(), System.out);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();

Thread.sleep(10*1000);

System.exit(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
/**
* A simple example of how to use the Java API
*
* Requires kubectl proxy running
*
* Easiest way to run this:
* mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.Example"
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*
Copyright 2017 The Kubernetes Authors.
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.kubernetes.client.examples;

import io.kubernetes.client.ApiClient;
Expand All @@ -19,10 +31,8 @@
/**
* A simple example of how to use the Java API
*
* Requires kubectl proxy running
*
* Easiest way to run this:
* mvn exec:java -Dex.mainClass="io.kubernetes.client.examples.Example"
* mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.Example"
*
* From inside $REPO_DIR/kubernetes
*/
Expand All @@ -32,19 +42,15 @@ public static void main(String[] args) throws IOException, ApiException, Interru
Configuration.setDefaultApiClient(client);

Exec exec = new Exec();
final Process proc = exec.exec("default", "nginx-2371676037-czqx3", new String[] {"sh", "-c", "echo foo"}, true);
boolean tty = System.console() != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to fix in this PR, but I suggest we create the pod too so the example is self contained. here is the python example: https://github.com/kubernetes-incubator/client-python/blob/master/examples/exec.py

// final Process proc = exec.exec("default", "nginx-2371676037-czqx3", new String[] {"sh", "-c", "echo foo"}, true, tty);
final Process proc = exec.exec("default", "nginx-2371676037-czqx3", new String[] {"sh"}, true, tty);


new Thread(new Runnable() {
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
OutputStream output = proc.getOutputStream();
try {
while (true) {
String line = in.readLine();
output.write(line.getBytes());
output.write('\n');
output.flush();
}
ByteStreams.copy(System.in, proc.getOutputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
Expand All @@ -62,6 +68,12 @@ public void run() {
}).start();

proc.waitFor();
try {
// Wait for buffers to flush.
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}

System.exit(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package io.kubernetes.client.examples;

import com.squareup.okhttp.ws.WebSocket;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.util.Config;
Expand All @@ -21,13 +22,14 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;

public class WebSocketsExample {
public static void main(String... args) throws ApiException, IOException {
final ApiClient client = Config.defaultClient();
WebSockets.stream(args[0], "GET", client, new WebSockets.SocketListener() {
public void open(String protocol, Closeable close) {}
public void open(String protocol, WebSocket socket) {}
public void close() {
// Trigger shutdown of the dispatcher's executor so this process can exit cleanly.
client.getHttpClient().getDispatcher().getExecutorService().shutdown();
Expand Down
1 change: 1 addition & 0 deletions util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@
<slf4jVersion>1.7.7</slf4jVersion>
</properties>
</project>

169 changes: 169 additions & 0 deletions util/src/main/java/io/kubernetes/client/Attach.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
Copyright 2017 The Kubernetes Authors.
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.kubernetes.client;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


import io.kubernetes.client.Configuration;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.util.WebSockets;
import io.kubernetes.client.util.WebSocketStreamHandler;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Reader;

import org.apache.commons.lang.StringUtils;

public class Attach {
private ApiClient apiClient;

/**
* Simple Attach API constructor, uses default configuration
*/
public Attach() {
this(Configuration.getDefaultApiClient());
}

/**
* Attach API Constructor
* @param apiClient The api client to use.
*/
public Attach(ApiClient apiClient) {
this.apiClient = apiClient;
}

/**
* Get the API client for these Attach operations.
* @returns The API client that will be used.
*/
public ApiClient getApiClient() {
return apiClient;
}

/**
* Set the API client for subsequent Attach operations.
* @param apiClient The new API client to use.
*/
public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}

private String makePath(String namespace, String name, String container, boolean stdin, boolean tty) {
return "/api/v1/namespaces/" +
namespace +
"/pods/" +
name +
"/attach?" +
"stdin=" + stdin +
"&tty=" + tty +
(container != null ? "&container=" + container : "");
}

/**
* Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
* the first container in the Pod.
*
* @param namespace The namespace of the Pod
* @param name The name of the Pod
* @param command The command to run
* @param stdin If true, pass a stdin stream into the container
*/
public AttachResult attach(String namespace, String name, boolean stdin) throws ApiException, IOException {
return attach(namespace, name, null, stdin, false);
}

/**
* Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
* the first container in the Pod.
*
* @param pod The pod where the command is run.
* @param stdin If true, pass a stdin stream into the container
*/
public AttachResult attach(V1Pod pod, boolean stdin) throws ApiException, IOException {
return attach(pod, stdin, false);
}


/**
* Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
* the first container in the Pod.
*
* @param pod The pod where the command is run.
* @param stdin If true, pass a stdin stream into the container
* @param tty If true, stdin is a tty.
*/
public AttachResult attach(V1Pod pod, boolean stdin, boolean tty) throws ApiException, IOException {
return attach(pod, null, stdin, tty);
}

/**
* Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
* the first container in the Pod.
*
* @param pod The pod where the command is run.
* @param container The container in the Pod where the command is run.
* @param stdin If true, pass a stdin stream into the container.
* @param tty If true, stdin is a TTY (only applies if stdin is true)
*/
public AttachResult attach(V1Pod pod, String container, boolean stdin, boolean tty) throws ApiException, IOException {
return attach(pod.getMetadata().getNamespace(), pod.getMetadata().getName(), container, stdin, tty);
}

/**
* Attach to a running AttachResult in a container. If there are multiple containers in the pod, uses
* the first container in the Pod.
*
* @param namespace The namespace of the Pod
* @param name The name of the Pod
* @param container The container in the Pod where the command is run.
* @param stdin If true, pass a stdin stream into the container.
* @param tty If true, stdin is a TTY (only applies if stdin is true)
*/
public AttachResult attach(String namespace, String name, String container, boolean stdin, boolean tty) throws ApiException, IOException {
String path = makePath(namespace, name, container, stdin, tty);

WebSocketStreamHandler handler = new WebSocketStreamHandler();
AttachResult result = new AttachResult(handler);
WebSockets.stream(path, "GET", apiClient, handler);

return result;
}

/**
* AttachResult contains the result of an Attach call, it includes streams for stdout
* stderr and stdin.
*/
public static class AttachResult {
private WebSocketStreamHandler handler;


public AttachResult(WebSocketStreamHandler handler) throws IOException {
this.handler = handler;
}

public OutputStream getStandardInputStream() {
return handler.getOutputStream(0);
}

public InputStream getStandardOutputStream() throws IOException {
return handler.getInputStream(1);
}

public InputStream getErrorStream() throws IOException {
return handler.getInputStream(2);
}
}
}