Skip to content

Commit

Permalink
Add support for LogParams::since_time (#1342)
Browse files Browse the repository at this point in the history
* Add support for `LogParams::since_time`

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 <sszynrae@gmail.com>

* add test + fix existing

Signed-off-by: clux <sszynrae@gmail.com>

* avoid `Time` wrapping

Signed-off-by: clux <sszynrae@gmail.com>

* stray import

Signed-off-by: clux <sszynrae@gmail.com>

---------

Signed-off-by: clux <sszynrae@gmail.com>
  • Loading branch information
clux authored Nov 20, 2023
1 parent 0abd2bd commit 89adbf2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 9 additions & 2 deletions examples/log_stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use futures::{AsyncBufReadExt, TryStreamExt};
use k8s_openapi::api::core::v1::Pod;
use k8s_openapi::{
api::core::v1::Pod,
chrono::{DateTime, Utc},
};
use kube::{
api::{Api, LogParams},
Client,
Expand All @@ -19,8 +22,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 +49,7 @@ async fn main() -> anyhow::Result<()> {
container: app.container,
tail_lines: app.tail,
since_seconds: app.since,
since_time: app.since_time,
timestamps: app.timestamps,
..LogParams::default()
})
Expand Down
26 changes: 26 additions & 0 deletions kube-core/src/subresource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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<chrono::DateTime<chrono::Utc>>,
/// 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 +70,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.to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
qp.append_pair("sinceTime", &ser_since);
}

if let Some(tl) = &lp.tail_lines {
Expand Down Expand Up @@ -347,6 +355,7 @@ impl Request {
#[cfg(test)]
mod test {
use crate::{request::Request, resource::Resource};
use chrono::{DateTime, TimeZone, Utc};
use k8s::core::v1 as corev1;
use k8s_openapi::api as k8s;

Expand All @@ -362,12 +371,29 @@ mod test {
pretty: true,
previous: true,
since_seconds: Some(3600),
since_time: None,
tail_lines: Some(4096),
timestamps: true,
};
let req = Request::new(url).logs("mypod", &lp).unwrap();
assert_eq!(req.uri(), "/api/v1/namespaces/ns/pods/mypod/log?&container=nginx&follow=true&limitBytes=10485760&pretty=true&previous=true&sinceSeconds=3600&tailLines=4096&timestamps=true");
}

#[test]
fn logs_since_time() {
let url = corev1::Pod::url_path(&(), Some("ns"));
let date: DateTime<Utc> = Utc.with_ymd_and_hms(2023, 10, 19, 13, 14, 26).unwrap();
let lp = LogParams {
since_seconds: None,
since_time: Some(date),
..Default::default()
};
let req = Request::new(url).logs("mypod", &lp).unwrap();
assert_eq!(
req.uri(),
"/api/v1/namespaces/ns/pods/mypod/log?&sinceTime=2023-10-19T13%3A14%3A26Z" // cross-referenced with kubectl
);
}
}

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit 89adbf2

Please sign in to comment.