-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add examples of "attach to pod" functionality
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
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
88 changes: 88 additions & 0 deletions
88
kubernetes-examples/src/main/java/io/fabric8/kubernetes/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,88 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* 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.fabric8.kubernetes.examples; | ||
|
||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.client.dsl.ExecListener; | ||
import io.fabric8.kubernetes.client.dsl.ExecWatch; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* This is an example of attaching to pod to access stdin, stdout, stderr | ||
*/ | ||
public class AttachExample { | ||
|
||
public static void main(String[] args) throws IOException { | ||
if (args.length < 1) { | ||
System.out.println("Usage: podName [namespace]"); | ||
return; | ||
} | ||
|
||
String podName = args[0]; | ||
String namespace = "default"; | ||
|
||
if (args.length > 1) { | ||
namespace = args[1]; | ||
} | ||
|
||
CompletableFuture<Void> sessionFuture = new CompletableFuture<>(); | ||
try ( | ||
KubernetesClient client = new KubernetesClientBuilder().build(); | ||
ExecWatch watch = attach(client, namespace, podName, sessionFuture)) { | ||
System.out.println("Type 'exit' to detach"); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
while (true) { | ||
String input = reader.readLine(); | ||
if (input.equals("exit")) { | ||
return; | ||
} | ||
watch.getInput().write((input + "\n").getBytes(StandardCharsets.UTF_8)); | ||
watch.getInput().flush(); | ||
if (sessionFuture.isDone()) { | ||
System.out.println("Session closed"); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static ExecWatch attach(KubernetesClient client, String namespace, String podName, | ||
CompletableFuture<Void> sessionFuture) { | ||
return client.pods().inNamespace(namespace).withName(podName) | ||
.redirectingInput() | ||
.writingOutput(System.out) | ||
.writingError(System.err) | ||
.withTTY() | ||
.usingListener(new ExecListener() { | ||
@Override | ||
public void onFailure(Throwable t, Response failureResponse) { | ||
sessionFuture.complete(null); | ||
} | ||
|
||
@Override | ||
public void onClose(int code, String reason) { | ||
sessionFuture.complete(null); | ||
} | ||
}) | ||
.attach(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...src/main/java/io/fabric8/kubernetes/examples/kubectl/equivalents/PodAttachEquivalent.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,84 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* 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.fabric8.kubernetes.examples.kubectl.equivalents; | ||
|
||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.client.dsl.ExecListener; | ||
import io.fabric8.kubernetes.client.dsl.ExecWatch; | ||
import lombok.Getter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* This sample code is Java equivalent to `kubectl attach my-pod`. It assumes that | ||
* a Pod with specified name exists in the cluster. | ||
*/ | ||
public class PodAttachEquivalent { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(PodAttachEquivalent.class); | ||
|
||
public static void main(String[] args) throws IOException { | ||
try (KubernetesClient client = new KubernetesClientBuilder().build()) { | ||
MyAttachListener listener = new MyAttachListener(); | ||
ExecWatch watch = client.pods().inNamespace("default").withName("my-pod") | ||
.redirectingInput() | ||
.writingOutput(System.out) | ||
.writingError(System.err) | ||
.withTTY() | ||
.usingListener(listener) | ||
.attach(); | ||
|
||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
while (!listener.getSessionCompletionFuture().isDone()) { | ||
String input = reader.readLine(); | ||
watch.getInput().write((input + "\n").getBytes(StandardCharsets.UTF_8)); | ||
watch.getInput().flush(); | ||
} | ||
|
||
watch.close(); | ||
} | ||
} | ||
|
||
private static class MyAttachListener implements ExecListener { | ||
|
||
@Getter | ||
private final CompletableFuture<Void> sessionCompletionFuture = new CompletableFuture<>(); | ||
|
||
@Override | ||
public void onOpen() { | ||
logger.info("Attached to the pod"); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable t, Response failureResponse) { | ||
logger.warn("Error encountered", t); | ||
sessionCompletionFuture.complete(null); | ||
} | ||
|
||
@Override | ||
public void onClose(int i, String s) { | ||
logger.info("Session closed"); | ||
sessionCompletionFuture.complete(null); | ||
} | ||
} | ||
} |