Skip to content

Commit

Permalink
Merge pull request #364 from clux/attach-tweaks
Browse files Browse the repository at this point in the history
AttachParams tweaks
  • Loading branch information
clux authored Jan 3, 2021
2 parents 681ca66 + 17aaf8b commit e4d999b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
54 changes: 23 additions & 31 deletions examples/pod_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,29 @@ async fn main() -> anyhow::Result<()> {
}
}

// Piping current stdin/stdout
{
let mut attached = pods
.exec(
"example",
vec!["sh"],
&AttachParams::default()
.stdin(true)
.stdout(true)
.stderr(false)
.tty(true),
)
.await?;
let mut stdin_writer = attached.stdin().unwrap();
let mut stdout_reader = attached.stdout().unwrap();
// > For interactive uses, it is recommended to spawn a thread dedicated to user input and use blocking IO directly in that thread.
// > https://docs.rs/tokio/0.2.24/tokio/io/fn.stdin.html
let mut stdin = tokio::io::stdin();
let mut stdout = tokio::io::stdout();
// pipe current stdin to the stdin writer from ws
tokio::spawn(async move {
tokio::io::copy(&mut stdin, &mut stdin_writer).await;
});
// pipe stdout from ws to current stdout
tokio::spawn(async move {
tokio::io::copy(&mut stdout_reader, &mut stdout).await;
});
// When done, type `exit\n` to end it, so the pod is deleted.
let status = attached.await;
println!("{:?}", status);
}
// Do an interactive exec to a blog pod with the `sh` command
let ap = AttachParams::interactive_tty();
let mut attached = pods.exec("example", vec!["sh"], &ap).await?;

// The received streams from `AttachedProcess`
let mut stdin_writer = attached.stdin().unwrap();
let mut stdout_reader = attached.stdout().unwrap();

// > For interactive uses, it is recommended to spawn a thread dedicated to user input and use blocking IO directly in that thread.
// > https://docs.rs/tokio/0.2.24/tokio/io/fn.stdin.html
let mut stdin = tokio::io::stdin();
let mut stdout = tokio::io::stdout();
// pipe current stdin to the stdin writer from ws
tokio::spawn(async move {
tokio::io::copy(&mut stdin, &mut stdin_writer).await.unwrap();
});
// pipe stdout from ws to current stdout
tokio::spawn(async move {
tokio::io::copy(&mut stdout_reader, &mut stdout).await.unwrap();
});
// When done, type `exit\n` to end it, so the pod is deleted.
let status = attached.await;
println!("{:?}", status);

// Delete it
println!("deleting");
Expand Down
27 changes: 19 additions & 8 deletions kube/src/api/subresource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,50 +279,61 @@ impl Default for AttachParams {

#[cfg(feature = "ws")]
impl AttachParams {
/// Default parameters for an tty exec with stdin and stdout
pub fn interactive_tty() -> Self {
Self {
stdin: true,
stdout: true,
stderr: false,
tty: true,
..Default::default()
}
}

/// Specify the container to execute in.
pub fn container<T: Into<String>>(&mut self, container: T) -> &mut Self {
pub fn container<T: Into<String>>(mut self, container: T) -> Self {
self.container = Some(container.into());
self
}

/// Set `stdin` field.
pub fn stdin(&mut self, enable: bool) -> &mut Self {
pub fn stdin(mut self, enable: bool) -> Self {
self.stdin = enable;
self
}

/// Set `stdout` field.
pub fn stdout(&mut self, enable: bool) -> &mut Self {
pub fn stdout(mut self, enable: bool) -> Self {
self.stdout = enable;
self
}

/// Set `stderr` field.
pub fn stderr(&mut self, enable: bool) -> &mut Self {
pub fn stderr(mut self, enable: bool) -> Self {
self.stderr = enable;
self
}

/// Set `tty` field.
pub fn tty(&mut self, enable: bool) -> &mut Self {
pub fn tty(mut self, enable: bool) -> Self {
self.tty = enable;
self
}

/// Set `max_stdin_buf_size` field.
pub fn max_stdin_buf_size(&mut self, size: usize) -> &mut Self {
pub fn max_stdin_buf_size(mut self, size: usize) -> Self {
self.max_stdin_buf_size = Some(size);
self
}

/// Set `max_stdout_buf_size` field.
pub fn max_stdout_buf_size(&mut self, size: usize) -> &mut Self {
pub fn max_stdout_buf_size(mut self, size: usize) -> Self {
self.max_stdout_buf_size = Some(size);
self
}

/// Set `max_stderr_buf_size` field.
pub fn max_stderr_buf_size(&mut self, size: usize) -> &mut Self {
pub fn max_stderr_buf_size(mut self, size: usize) -> Self {
self.max_stderr_buf_size = Some(size);
self
}
Expand Down

0 comments on commit e4d999b

Please sign in to comment.