-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Implement Attach #24
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
examples/src/main/java/io/kubernetes/client/examples/AttachExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,3 +93,4 @@ | |
<slf4jVersion>1.7.7</slf4jVersion> | ||
</properties> | ||
</project> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Header? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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