forked from kube-rs/kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod_attach.rs
116 lines (101 loc) · 3.69 KB
/
pod_attach.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#[macro_use] extern crate log;
use std::io::Write;
use futures::{join, stream, StreamExt, TryStreamExt};
use k8s_openapi::api::core::v1::Pod;
use kube::{
api::{
Api, AttachParams, AttachedProcess, DeleteParams, ListParams, PostParams, ResourceExt, WatchEvent,
},
Client,
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "info,kube=debug");
env_logger::init();
let client = Client::try_default().await?;
let namespace = std::env::var("NAMESPACE").unwrap_or_else(|_| "default".into());
info!("Creating a Pod that outputs numbers for 15s");
let p: Pod = serde_json::from_value(serde_json::json!({
"apiVersion": "v1",
"kind": "Pod",
"metadata": { "name": "example" },
"spec": {
"containers": [{
"name": "example",
"image": "alpine",
"command": ["sh", "-c", "for i in `seq 15`; do if [ $i -lt 7 ]; then echo \"o $i\"; else echo \"e $i\" 1>&2; fi; sleep 1; done;"],
}],
}
}))?;
let pods: Api<Pod> = Api::namespaced(client, &namespace);
// 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());
}
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());
break;
}
}
_ => {}
}
}
let ap = AttachParams::default();
// Attach to see numbers printed on stdout.
let attached = pods.attach("example", &ap).await?;
// Separate stdout/stderr outputs
separate_outputs(attached).await;
// Combining stdout and stderr output.
// combined_output(proc).await;
// Delete it
pods.delete("example", &DeleteParams::default())
.await?
.map_left(|pdel| {
assert_eq!(pdel.name(), "example");
});
Ok(())
}
#[allow(dead_code)]
async fn separate_outputs(mut attached: AttachedProcess) {
let stdout = tokio_util::io::ReaderStream::new(attached.stdout().unwrap());
let stdouts = stdout.for_each(|res| async {
if let Ok(bytes) = res {
let out = std::io::stdout();
out.lock().write_all(&bytes).unwrap();
}
});
let stderr = tokio_util::io::ReaderStream::new(attached.stderr().unwrap());
let stderrs = stderr.for_each(|res| async {
if let Ok(bytes) = res {
let out = std::io::stderr();
out.lock().write_all(&bytes).unwrap();
}
});
join!(stdouts, stderrs);
if let Some(status) = attached.await {
println!("{:?}", status);
}
}
#[allow(dead_code)]
async fn combined_output(mut attached: AttachedProcess) {
let stdout = tokio_util::io::ReaderStream::new(attached.stdout().unwrap());
let stderr = tokio_util::io::ReaderStream::new(attached.stderr().unwrap());
let outputs = stream::select(stdout, stderr).for_each(|res| async {
if let Ok(bytes) = res {
let out = std::io::stdout();
out.lock().write_all(&bytes).unwrap();
}
});
outputs.await;
if let Some(status) = attached.await {
println!("{:?}", status);
}
}