use std::fmt::Debug;
use crate::{
params::{DeleteParams, PostParams},
request::{Request, JSON_MIME},
Error, Result,
};
pub use k8s_openapi::api::autoscaling::v1::{Scale, ScaleSpec, ScaleStatus};
#[derive(Default, Clone, Debug)]
pub struct LogParams {
pub container: Option<String>,
pub follow: bool,
pub limit_bytes: Option<i64>,
pub pretty: bool,
pub previous: bool,
pub since_seconds: Option<i64>,
pub tail_lines: Option<i64>,
pub timestamps: bool,
}
impl Request {
pub fn logs(&self, name: &str, lp: &LogParams) -> Result<http::Request<Vec<u8>>> {
let target = format!("{}/{}/log?", self.url_path, name);
let mut qp = form_urlencoded::Serializer::new(target);
if let Some(container) = &lp.container {
qp.append_pair("container", container);
}
if lp.follow {
qp.append_pair("follow", "true");
}
if let Some(lb) = &lp.limit_bytes {
qp.append_pair("limitBytes", &lb.to_string());
}
if lp.pretty {
qp.append_pair("pretty", "true");
}
if lp.previous {
qp.append_pair("previous", "true");
}
if let Some(ss) = &lp.since_seconds {
qp.append_pair("sinceSeconds", &ss.to_string());
}
if let Some(tl) = &lp.tail_lines {
qp.append_pair("tailLines", &tl.to_string());
}
if lp.timestamps {
qp.append_pair("timestamps", "true");
}
let urlstr = qp.finish();
let req = http::Request::get(urlstr);
req.body(vec![]).map_err(Error::HttpError)
}
}
#[derive(Default, Clone)]
pub struct EvictParams {
pub delete_options: Option<DeleteParams>,
pub post_options: PostParams,
}
impl Request {
pub fn evict(&self, name: &str, ep: &EvictParams) -> Result<http::Request<Vec<u8>>> {
let target = format!("{}/{}/eviction?", self.url_path, name);
let pp = &ep.post_options;
pp.validate()?;
let mut qp = form_urlencoded::Serializer::new(target);
if pp.dry_run {
qp.append_pair("dryRun", "All");
}
let urlstr = qp.finish();
let data = serde_json::to_vec(&serde_json::json!({
"delete_options": ep.delete_options,
"metadata": { "name": name }
}))?;
let req = http::Request::post(urlstr).header(http::header::CONTENT_TYPE, JSON_MIME);
req.body(data).map_err(Error::HttpError)
}
}
#[cfg(feature = "ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]
#[derive(Debug)]
pub struct AttachParams {
pub container: Option<String>,
pub stdin: bool,
pub stdout: bool,
pub stderr: bool,
pub tty: bool,
pub max_stdin_buf_size: Option<usize>,
pub max_stdout_buf_size: Option<usize>,
pub max_stderr_buf_size: Option<usize>,
}
#[cfg(feature = "ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]
impl Default for AttachParams {
fn default() -> Self {
Self {
container: None,
stdin: false,
stdout: true,
stderr: true,
tty: false,
max_stdin_buf_size: None,
max_stdout_buf_size: None,
max_stderr_buf_size: None,
}
}
}
#[cfg(feature = "ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]
impl AttachParams {
pub fn interactive_tty() -> Self {
Self {
stdin: true,
stdout: true,
stderr: false,
tty: true,
..Default::default()
}
}
pub fn container<T: Into<String>>(mut self, container: T) -> Self {
self.container = Some(container.into());
self
}
pub fn stdin(mut self, enable: bool) -> Self {
self.stdin = enable;
self
}
pub fn stdout(mut self, enable: bool) -> Self {
self.stdout = enable;
self
}
pub fn stderr(mut self, enable: bool) -> Self {
self.stderr = enable;
self
}
pub fn tty(mut self, enable: bool) -> Self {
self.tty = enable;
self
}
pub fn max_stdin_buf_size(mut self, size: usize) -> Self {
self.max_stdin_buf_size = Some(size);
self
}
pub fn max_stdout_buf_size(mut self, size: usize) -> Self {
self.max_stdout_buf_size = Some(size);
self
}
pub fn max_stderr_buf_size(mut self, size: usize) -> Self {
self.max_stderr_buf_size = Some(size);
self
}
fn validate(&self) -> Result<()> {
if !self.stdin && !self.stdout && !self.stderr {
return Err(Error::RequestValidation(
"AttachParams: one of stdin, stdout, or stderr must be true".into(),
));
}
if self.stderr && self.tty {
return Err(Error::RequestValidation(
"AttachParams: tty and stderr cannot both be true".into(),
));
}
Ok(())
}
fn append_to_url_serializer(&self, qp: &mut form_urlencoded::Serializer<String>) {
if self.stdin {
qp.append_pair("stdin", "true");
}
if self.stdout {
qp.append_pair("stdout", "true");
}
if self.stderr {
qp.append_pair("stderr", "true");
}
if self.tty {
qp.append_pair("tty", "true");
}
if let Some(container) = &self.container {
qp.append_pair("container", container);
}
}
}
#[cfg(feature = "ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]
impl Request {
pub fn attach(&self, name: &str, ap: &AttachParams) -> Result<http::Request<Vec<u8>>> {
ap.validate()?;
let target = format!("{}/{}/attach?", self.url_path, name);
let mut qp = form_urlencoded::Serializer::new(target);
ap.append_to_url_serializer(&mut qp);
let req = http::Request::get(qp.finish());
req.body(vec![]).map_err(Error::HttpError)
}
}
#[cfg(feature = "ws")]
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]
impl Request {
pub fn exec<I, T>(&self, name: &str, command: I, ap: &AttachParams) -> Result<http::Request<Vec<u8>>>
where
I: IntoIterator<Item = T>,
T: Into<String>,
{
ap.validate()?;
let target = format!("{}/{}/exec?", self.url_path, name);
let mut qp = form_urlencoded::Serializer::new(target);
ap.append_to_url_serializer(&mut qp);
for c in command.into_iter() {
qp.append_pair("command", &c.into());
}
let req = http::Request::get(qp.finish());
req.body(vec![]).map_err(Error::HttpError)
}
}