-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathlog_stream.rs
63 lines (54 loc) · 1.48 KB
/
log_stream.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
use futures::{AsyncBufReadExt, TryStreamExt};
use k8s_openapi::{
api::core::v1::Pod,
chrono::{DateTime, Utc},
};
use kube::{
api::{Api, LogParams},
Client,
};
use tracing::*;
/// limited variant of kubectl logs
#[derive(clap::Parser)]
struct App {
#[arg(long, short = 'c')]
container: Option<String>,
#[arg(long, short = 't')]
tail: Option<i64>,
#[arg(long, short = 'f')]
follow: bool,
/// Since seconds
#[arg(long, conflicts_with = "since_time")]
since: Option<i64>,
/// Since time
#[arg(long, conflicts_with = "since")]
since_time: Option<DateTime<Utc>>,
/// Include timestamps in the log output
#[arg(long, default_value = "false")]
timestamps: bool,
pod: String,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let app: App = clap::Parser::parse();
let client = Client::try_default().await?;
info!("Fetching logs for {:?}", app.pod);
let pods: Api<Pod> = Api::default_namespaced(client);
let mut logs = pods
.log_stream(&app.pod, &LogParams {
follow: app.follow,
container: app.container,
tail_lines: app.tail,
since_seconds: app.since,
since_time: app.since_time,
timestamps: app.timestamps,
..LogParams::default()
})
.await?
.lines();
while let Some(line) = logs.try_next().await? {
println!("{}", line);
}
Ok(())
}