Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for LogParams::since_time #1342

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading