forked from kube-rs/kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod_shell.rs
85 lines (75 loc) · 2.91 KB
/
pod_shell.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::api::core::v1::Pod;
use tracing::*;
use kube::{
api::{Api, AttachParams, DeleteParams, ListParams, PostParams, ResourceExt, WatchEvent},
Client,
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let client = Client::try_default().await?;
let p: Pod = serde_json::from_value(serde_json::json!({
"apiVersion": "v1",
"kind": "Pod",
"metadata": { "name": "example" },
"spec": {
"containers": [{
"name": "example",
"image": "alpine",
// Do nothing
"command": ["tail", "-f", "/dev/null"],
}],
}
}))?;
let pods: Api<Pod> = Api::default_namespaced(client);
// Stop on error including a pod already exists or is still being deleted.
pods.create(&PostParams::default(), &p).await?;
// Wait until the pod is running, otherwise we get 500 error.
let lp = ListParams::default().fields("metadata.name=example").timeout(10);
let mut stream = pods.watch(&lp, "0").await?.boxed();
while let Some(status) = stream.try_next().await? {
match status {
WatchEvent::Added(o) => {
info!("Added {}", o.name_any());
}
WatchEvent::Modified(o) => {
let s = o.status.as_ref().expect("status exists on pod");
if s.phase.clone().unwrap_or_default() == "Running" {
info!("Ready to attach to {}", o.name_any());
break;
}
}
_ => {}
}
}
// Do an interactive exec to a blog pod with the `sh` command
let ap = AttachParams::interactive_tty();
let mut attached = pods.exec("example", vec!["sh"], &ap).await?;
// The received streams from `AttachedProcess`
let mut stdin_writer = attached.stdin().unwrap();
let mut stdout_reader = attached.stdout().unwrap();
// > For interactive uses, it is recommended to spawn a thread dedicated to user input and use blocking IO directly in that thread.
// > https://docs.rs/tokio/0.2.24/tokio/io/fn.stdin.html
let mut stdin = tokio::io::stdin();
let mut stdout = tokio::io::stdout();
// pipe current stdin to the stdin writer from ws
tokio::spawn(async move {
tokio::io::copy(&mut stdin, &mut stdin_writer).await.unwrap();
});
// pipe stdout from ws to current stdout
tokio::spawn(async move {
tokio::io::copy(&mut stdout_reader, &mut stdout).await.unwrap();
});
// When done, type `exit\n` to end it, so the pod is deleted.
let status = attached.take_status().unwrap().await;
info!("{:?}", status);
// Delete it
info!("deleting");
pods.delete("example", &DeleteParams::default())
.await?
.map_left(|pdel| {
assert_eq!(pdel.name_any(), "example");
});
Ok(())
}