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

Fix connection op for pods/log #255

Merged
merged 7 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions lib/k8s/client/mint/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ defmodule K8s.Client.Mint.Request do
def map_frame({:binary, <<1, msg::binary>>}), do: {:stdout, msg}
def map_frame({:binary, <<2, msg::binary>>}), do: {:stderr, msg}
def map_frame({:binary, <<3, msg::binary>>}), do: {:error, msg}
def map_frame({:binary, <<msg::binary>>}), do: {:stdout, msg}

@spec map_outgoing_frame({:stdin, binary()} | {:close, integer(), binary()} | :close | :exit) ::
{:ok, :close | {:text, binary} | {:close, integer(), binary()}}
Expand Down
20 changes: 17 additions & 3 deletions lib/k8s/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ defmodule K8s.Operation do
apply: "application/apply-patch+yaml"
}

@allowed_connect_params [
:command,
:container,
:follow,
:limitBytes,
:pretty,
:previous,
:sinceSeconds,
:stderr,
:stdin,
:stdout,
:tailLines,
:timestamps,
:tty
]

defstruct method: nil,
verb: nil,
api_version: nil,
Expand Down Expand Up @@ -212,9 +228,7 @@ defmodule K8s.Operation do

verb === :connect ->
[stdin: true, stdout: true, stderr: true, tty: false]
|> Keyword.merge(
Keyword.take(opts, [:stdin, :stdout, :stderr, :tty, :command, :container])
)
|> Keyword.merge(Keyword.take(opts, @allowed_connect_params))

true ->
[]
Expand Down
39 changes: 38 additions & 1 deletion test/k8s/client/runner/base_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ defmodule K8s.Client.Runner.BaseIntegrationTest do

@tag :integration
@tag :websocket
test "runs :connect operations and returns stdout", %{
test "runs :connect operations for pods/exec and returns stdout", %{
conn: conn,
labels: labels,
test_id: test_id
Expand Down Expand Up @@ -303,6 +303,43 @@ defmodule K8s.Client.Runner.BaseIntegrationTest do
assert response.stdout =~ "ok"
end

@tag :integration
@tag :websocket
@tag :wip
@tail_lines 5
test "runs :connect operations for pods/log and returns stdout", %{
conn: conn,
labels: labels,
test_id: test_id
} do
{:ok, created_pod} =
build_pod("k8s-ex-#{test_id}", labels)
|> K8s.Client.create()
|> K8s.Client.put_conn(conn)
|> K8s.Client.run()

{:ok, _} =
K8s.Client.wait_until(conn, K8s.Client.get(created_pod),
find: ["status", "containerStatuses", Access.filter(&(&1["ready"] == true))],
eval: &match?([_ | _], &1),
timeout: 60
)

{:ok, response} =
K8s.Client.connect(
created_pod["apiVersion"],
"pods/log",
[namespace: K8s.Resource.namespace(created_pod), name: K8s.Resource.name(created_pod)],
tailLines: @tail_lines
)
|> K8s.Client.put_conn(conn)
|> K8s.Client.run()

assert String.printable?(response.stdout)
lines = String.split(response.stdout, "\n", trim: true)
assert @tail_lines == length(lines)
end

@tag :integration
@tag :websocket
test "runs :connect operations and returns errors", %{
Expand Down