-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into new-default-features
- Loading branch information
Showing
5 changed files
with
94 additions
and
48 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
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 |
---|---|---|
@@ -1,42 +1,64 @@ | ||
use futures::{pin_mut, TryStreamExt}; | ||
use k8s_openapi::api::{core::v1::ObjectReference, events::v1::Event}; | ||
use k8s_openapi::{ | ||
api::{core::v1::ObjectReference, events::v1::Event}, | ||
apimachinery::pkg::apis::meta::v1::Time, | ||
chrono::Utc, | ||
}; | ||
use kube::{ | ||
api::Api, | ||
runtime::{watcher, WatchStreamExt}, | ||
Client, | ||
Api, Client, ResourceExt, | ||
}; | ||
use tracing::info; | ||
|
||
/// limited variant of `kubectl events` that works on current context's namespace | ||
/// | ||
/// requires a new enough cluster that apis/events.k8s.io/v1 work (kubectl uses corev1::Event) | ||
/// for old style usage of core::v1::Event see node_watcher | ||
#[derive(clap::Parser)] | ||
struct App { | ||
/// Filter by object and kind | ||
/// | ||
/// Using --for=Pod/blog-xxxxx | ||
/// Note that kind name is case sensitive | ||
#[arg(long)] | ||
r#for: Option<String>, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
tracing_subscriber::fmt::init(); | ||
let client = Client::try_default().await?; | ||
let app: App = clap::Parser::parse(); | ||
|
||
let events: Api<Event> = Api::all(client); | ||
let ew = watcher(events, watcher::Config::default()).applied_objects(); | ||
let events: Api<Event> = Api::default_namespaced(client); | ||
let mut conf = watcher::Config::default(); | ||
if let Some(forval) = app.r#for { | ||
if let Some((kind, name)) = forval.split_once('/') { | ||
conf = conf.fields(&format!("regarding.kind={kind},regarding.name={name}")); | ||
} | ||
} | ||
let event_stream = watcher(events, conf).default_backoff().applied_objects(); | ||
pin_mut!(event_stream); | ||
|
||
pin_mut!(ew); | ||
while let Some(event) = ew.try_next().await? { | ||
handle_event(event)?; | ||
println!("{0:<6} {1:<15} {2:<55} {3}", "AGE", "REASON", "OBJECT", "MESSAGE"); | ||
while let Some(ev) = event_stream.try_next().await? { | ||
let age = ev.creation_timestamp().map(format_creation).unwrap_or_default(); | ||
let reason = ev.reason.unwrap_or_default(); | ||
let obj = ev.regarding.map(format_objref).flatten().unwrap_or_default(); | ||
let note = ev.note.unwrap_or_default(); | ||
println!("{0:<6} {1:<15} {2:<55} {3}", age, reason, obj, note); | ||
} | ||
Ok(()) | ||
} | ||
|
||
// This function lets the app handle an added/modified event from k8s | ||
fn handle_event(ev: Event) -> anyhow::Result<()> { | ||
info!( | ||
"{}: {} ({})", | ||
ev.regarding.map(fmt_obj_ref).unwrap_or_default(), | ||
ev.reason.unwrap_or_default(), | ||
ev.note.unwrap_or_default(), | ||
); | ||
Ok(()) | ||
fn format_objref(oref: ObjectReference) -> Option<String> { | ||
Some(format!("{}/{}", oref.kind?, oref.name?)) | ||
} | ||
|
||
fn fmt_obj_ref(oref: ObjectReference) -> String { | ||
format!( | ||
"{}/{}", | ||
oref.kind.unwrap_or_default(), | ||
oref.name.unwrap_or_default() | ||
) | ||
fn format_creation(time: Time) -> String { | ||
let dur = Utc::now().signed_duration_since(time.0); | ||
match (dur.num_days(), dur.num_hours(), dur.num_minutes()) { | ||
(days, _, _) if days > 0 => format!("{days}d"), | ||
(_, hours, _) if hours > 0 => format!("{hours}h"), | ||
(_, _, mins) => format!("{mins}m"), | ||
} | ||
} |
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 |
---|---|---|
@@ -1,35 +1,56 @@ | ||
use anyhow::{anyhow, Result}; | ||
use futures::{AsyncBufReadExt, TryStreamExt}; | ||
use k8s_openapi::api::core::v1::Pod; | ||
use kube::{ | ||
api::{Api, LogParams}, | ||
Client, | ||
}; | ||
use std::env; | ||
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, short = 's')] | ||
since: Option<i64>, | ||
|
||
/// Include timestamps in the log output | ||
#[arg(long, default_value = "false")] | ||
timestamps: bool, | ||
|
||
pod: String, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
async fn main() -> anyhow::Result<()> { | ||
tracing_subscriber::fmt::init(); | ||
let app: App = clap::Parser::parse(); | ||
let client = Client::try_default().await?; | ||
|
||
let mypod = env::args() | ||
.nth(1) | ||
.ok_or_else(|| anyhow!("Usage: log_follow <pod>"))?; | ||
info!("Fetching logs for {:?}", mypod); | ||
|
||
info!("Fetching logs for {:?}", app.pod); | ||
let pods: Api<Pod> = Api::default_namespaced(client); | ||
let mut logs = pods | ||
.log_stream(&mypod, &LogParams { | ||
follow: true, | ||
tail_lines: Some(1), | ||
.log_stream(&app.pod, &LogParams { | ||
follow: app.follow, | ||
container: app.container, | ||
tail_lines: app.tail, | ||
since_seconds: app.since, | ||
timestamps: app.timestamps, | ||
..LogParams::default() | ||
}) | ||
.await? | ||
.lines(); | ||
|
||
while let Some(line) = logs.try_next().await? { | ||
info!("{}", line); | ||
println!("{}", line); | ||
} | ||
Ok(()) | ||
} |
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