View Source Kubereq.PodExec (kubereq v0.4.0)
Establish a connection to a Pod and execute a command in a container.
The connection is kept alive until the websocket is closed by the counterpart.
The bytes received from the container are sent to the process passed via the
:into
option. Bytes sent to this process via send_stdin/2
are forwarded to
the container.
Examples
When the command terminates, the websocket is automatically closed and the process terminates.
req = Req.new() |> Kubereq.attach()
Kubereq.PodExec.start_link(
req: req,
namespace: "default",
name: "my-pod",
container: "main",
into: self(),
command: ["/bin/sh", "-c", "echo foo"],
stdin: true,
stdout: true,
stderr: true
tty: false,
)
# Messages in inbox: {:stdout, "foo\n"}, {:close, 1000, ""}
Passing the path to a shell as command will keep the socket open. Together
with :stdin
, :stdout
, :stderr
and :tty
, this can be used to implement
an interactive shell:
req = Req.new() |> Kubereq.attach()
{:ok, dest} = Kubereq.PodExec.start_link(
req: req,
namespace: "default",
name: "my-pod",
container: "main",
into: self(),
command: ["/bin/sh"],
stdin: true,
stdout: true,
stderr: true
tty: false,
)
# Message in inbox: {:stdout, "sh-5.2# "}
Kubereq.PodExec.send_stdin(dest, "echo foo")
# Message in inbox: {:stdout, "echo foo\r\nfoo\r\nsh-5.2# "}
Arguments
:req
- AReq.Request
struct with Kubereq attached.:namespace
- The namespace the Pod runs in:name
- The name of the Pod:container
(optional) - The container for which to stream logs. Defaults to only container if there is one container in the pod. Fails if not defined for pods with multiple pods.:into
- Destination for messages received from the pod. Can be apid
or a{pid, ref}
tuple.:command
- Command is the remote command to execute. Not executed within a shell.:stdin
(optional) - Redirect the standard input stream of the pod for this call. Defaults totrue
.:stdin
(optional) - Redirect the standard output stream of the pod for this call. Defaults totrue
.:stderr
(optional) - Redirect the standard error stream of the pod for this call. Defaults totrue
.:tty
(optional) - Iftrue
indicates that a tty will be allocated for the exec call. Defaults tofalse
.:opts
(optional) - Additional options passed toReq
Summary
Functions
Close the connection and terminate the process.
See Fresh.open?/1
.
Send the given data
to the container.
Functions
@spec close(dest :: :gen_statem.server_ref()) :: :ok
Close the connection and terminate the process.
See Fresh.close/3
.
See Fresh.open?/1
.
@spec send_stdin(dest :: :gen_statem.server_ref(), data :: binary()) :: :ok
Send the given data
to the container.