Skip to content

Commit

Permalink
Add support for LogParams::since_time
Browse files Browse the repository at this point in the history
Tested using

```sh
RUST_LOG=debug cargo run --example log_stream -- coredns-77ccd57875-k46v4 --since-time="2023-11-12T12:23:53Z"
```

on k3d.

Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Nov 12, 2023
1 parent 0abd2bd commit 95b59ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions examples/log_stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use futures::{AsyncBufReadExt, TryStreamExt};
use k8s_openapi::api::core::v1::Pod;
use k8s_openapi::{
api::core::v1::Pod,
apimachinery::pkg::apis::meta::v1::Time,
chrono::{DateTime, Utc},
};
use kube::{
api::{Api, LogParams},
Client,
Expand All @@ -19,8 +23,11 @@ struct App {
follow: bool,

/// Since seconds
#[arg(long, short = 's')]
#[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")]
Expand All @@ -43,6 +50,7 @@ async fn main() -> anyhow::Result<()> {
container: app.container,
tail_lines: app.tail,
since_seconds: app.since,
since_time: app.since_time.map(|st| Time(st)),
timestamps: app.timestamps,
..LogParams::default()
})
Expand Down
9 changes: 9 additions & 0 deletions kube-core/src/subresource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
};

pub use k8s_openapi::api::autoscaling::v1::{Scale, ScaleSpec, ScaleStatus};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::Time;

// ----------------------------------------------------------------------------
// Log subresource
Expand All @@ -30,6 +31,11 @@ pub struct LogParams {
/// If this value precedes the time a pod was started, only logs since the pod start will be returned.
/// If this value is in the future, no logs will be returned. Only one of sinceSeconds or sinceTime may be specified.
pub since_seconds: Option<i64>,
/// An RFC3339 timestamp from which to show logs. If this value
/// precedes the time a pod was started, only logs since the pod start will be returned.
/// If this value is in the future, no logs will be returned.
/// Only one of sinceSeconds or sinceTime may be specified.
pub since_time: Option<Time>,
/// If set, the number of lines from the end of the logs to show.
/// If not specified, logs are shown from the creation of the container or sinceSeconds or sinceTime
pub tail_lines: Option<i64>,
Expand Down Expand Up @@ -65,6 +71,9 @@ impl Request {

if let Some(ss) = &lp.since_seconds {
qp.append_pair("sinceSeconds", &ss.to_string());
} else if let Some(st) = &lp.since_time {
let ser_since = st.0.to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
qp.append_pair("sinceTime", &ser_since);
}

if let Some(tl) = &lp.tail_lines {
Expand Down

0 comments on commit 95b59ba

Please sign in to comment.