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 possibility to wait for delete #262

Merged
merged 1 commit into from
Jul 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
erlang 26.0.1
elixir 1.15.0
erlang 26.0.2
elixir 1.15.2
k3d 5.4.6
kind 0.18.0
kind 0.19.0
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!--------------------- Don't add new entries after this line --------------------->

### Added

- `K8s.Client.wait_until/2` - Allow passing DELETE operations in order to wait for deletion.

## [2.3.0] - 2023-05-14

### Added
Expand Down
39 changes: 34 additions & 5 deletions lib/k8s/client/runner/wait.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ defmodule K8s.Client.Runner.Wait do
```
"""
@spec run(Operation.t(), keyword()) ::
{:ok, map()} | {:error, :timeout | Error.t()}
{:ok, :deleted} | {:ok, map()} | {:error, :timeout | Error.t()}

def run(%Operation{conn: %Conn{} = conn} = op, opts), do: run(conn, op, opts)

@spec run(Conn.t(), Operation.t(), keyword()) ::
{:ok, map()} | {:error, :timeout | Error.t()}
{:ok, :deleted} | {:ok, map()} | {:error, :timeout | Error.t()}
def run(%Conn{} = conn, %Operation{method: :get} = op, opts) do
conditions =
Wait
Expand All @@ -52,8 +53,36 @@ defmodule K8s.Client.Runner.Wait do
end
end

def run(%Conn{} = conn, %Operation{method: :delete} = op, opts) do
case Base.run(conn, op) do
{:ok, _} ->
run(
conn,
struct(op, method: :get),
Keyword.merge(opts,
processor: &get_deleted_processor/2,
find: &Function.identity/1,
eval: :deleted
)
)

error ->
error
end
end

def run(op, _, _),
do: {:error, %Error{message: "Only HTTP GET operations are supported. #{inspect(op)}"}}
do:
{:error,
%Error{message: "Only HTTP GET and DELETE operations are supported. #{inspect(op)}"}}

@spec get_deleted_processor(Conn.t(), Operation.t()) :: {:ok, :deleted} | {:error, :exists}
defp get_deleted_processor(conn, op) do
case Base.run(conn, op) do
{:error, %K8s.Client.APIError{reason: "NotFound"}} -> {:ok, :deleted}
{:ok, _} -> {:error, :exists}
end
end

@spec process_opts(Wait.t() | map) :: {:error, Error.t()} | {:ok, map}
defp process_opts(%Wait{eval: nil}), do: {:error, %Error{message: ":eval is required"}}
Expand Down Expand Up @@ -105,12 +134,12 @@ defmodule K8s.Client.Runner.Wait do
end

@spec satisfied?(map, function | list, any) :: boolean
defp satisfied?(resp = %{}, find, eval) when is_list(find) do
defp satisfied?(resp, find, eval) when is_list(find) do
value = get_in(resp, find)
compare(value, eval)
end

defp satisfied?(resp = %{}, find, eval) when is_function(find) do
defp satisfied?(resp, find, eval) when is_function(find) do
value = find.(resp)
compare(value, eval)
end
Expand Down
21 changes: 21 additions & 0 deletions test/k8s/client/runner/wait_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,25 @@ defmodule K8s.Client.Runner.WaitIntegrationTest do
assert {:ok, result} = K8s.Client.Runner.Wait.run(conn, op, opts)
assert result["status"]["succeeded"] == 1
end

@tag :integration
@tag :wait
@tag :wip
test "wait for deletion", %{
conn: conn,
test_id: test_id,
labels: labels,
timeout: timeout
} do
create_job =
"wait-job-#{test_id}"
|> job(labels: labels)
|> K8s.Client.create()

{:ok, created_job} = K8s.Client.run(conn, create_job)

op = K8s.Client.delete(created_job)

assert {:ok, :deleted} = K8s.Client.Runner.Wait.run(conn, op, timeout: timeout)
end
end
4 changes: 2 additions & 2 deletions test/k8s/client/runner/wait_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ defmodule K8s.Client.Runner.WaitTest do
assert msg == ":eval is required"
end

test "returns an error the operation is not a GET" do
test "returns an error the operation is not a GET or DELETE" do
operation = operation(:post)
{:error, %{message: msg}} = Wait.run(operation, %K8s.Conn{}, find: ["foo"])
assert Regex.match?(~r/Only HTTP GET operations are supported/, msg)
assert Regex.match?(~r/Only HTTP GET and DELETE operations are supported/, msg)
end

test "returns an :ok tuple when the primitive conditions are met" do
Expand Down