diff --git a/lib/livebook/k8s_api.ex b/lib/livebook/k8s_api.ex index aba4d7032..8550e901f 100644 --- a/lib/livebook/k8s_api.ex +++ b/lib/livebook/k8s_api.ex @@ -10,7 +10,7 @@ defmodule Livebook.K8sAPI do @spec create_pod(kubeconfig(), map()) :: {:ok, data} | error() when data: %{name: String.t()} def create_pod(kubeconfig, manifest) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:namespace/pods/:name") + req = pod_req(kubeconfig) case Kubereq.create(req, manifest) do {:ok, %{status: 201, body: %{"metadata" => %{"name" => name}}}} -> @@ -27,7 +27,7 @@ defmodule Livebook.K8sAPI do @spec get_pod(kubeconfig(), String.t(), String.t()) :: {:ok, data} | error() when data: %{ip: String.t()} def get_pod(kubeconfig, namespace, name) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:namespace/pods/:name") + req = pod_req(kubeconfig) case Kubereq.get(req, namespace, name) do {:ok, %{status: 200, body: pod}} -> @@ -43,7 +43,7 @@ defmodule Livebook.K8sAPI do """ @spec delete_pod(kubeconfig(), String.t(), String.t()) :: :ok | error() def delete_pod(kubeconfig, namespace, name) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:namespace/pods/:name") + req = pod_req(kubeconfig) case Kubereq.delete(req, namespace, name) do {:ok, %{status: 200}} -> :ok @@ -56,7 +56,7 @@ defmodule Livebook.K8sAPI do """ @spec await_pod_ready(kubeconfig(), String.t(), String.t()) :: :ok | error() def await_pod_ready(kubeconfig, namespace, name) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:namespace/pods/:name") + req = pod_req(kubeconfig) callback = fn :deleted -> @@ -72,7 +72,7 @@ defmodule Livebook.K8sAPI do end # Wait up to 30 minutes - case Kubereq.wait_until(req, namespace, name, callback, 1_800_000) do + case Kubereq.wait_until(req, namespace, name, callback, timeout: 1_800_000) do {:error, :watch_timeout} -> {:error, %{message: "timed out waiting for Pod to become ready", status: nil}} @@ -91,7 +91,7 @@ defmodule Livebook.K8sAPI do """ @spec watch_pod_events(kubeconfig(), String.t(), String.t()) :: {:ok, Enumerable.t()} | error() def watch_pod_events(kubeconfig, namespace, name) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:namespace/events/:name") + req = Req.new() |> Kubereq.attach(kubeconfig: kubeconfig, api_version: "v1", kind: "Event") Kubereq.watch(req, namespace, field_selectors: [ @@ -119,7 +119,8 @@ defmodule Livebook.K8sAPI do @spec list_namespaces(kubeconfig()) :: {:ok, data} | error() when data: list(%{name: String.t()}) def list_namespaces(kubeconfig) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:name") + req = + Req.new() |> Kubereq.attach(kubeconfig: kubeconfig, api_version: "v1", kind: "Namespace") case Kubereq.list(req, nil) do {:ok, %{status: 200, body: %{"items" => items}}} -> @@ -141,7 +142,7 @@ defmodule Livebook.K8sAPI do @spec create_pvc(kubeconfig(), map()) :: {:ok, data} | error() when data: %{name: String.t()} def create_pvc(kubeconfig, manifest) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:namespace/persistentvolumeclaims/:name") + req = pvc_req(kubeconfig) case Kubereq.create(req, manifest) do {:ok, %{status: 201, body: %{"metadata" => %{"name" => name}}}} -> @@ -158,7 +159,7 @@ defmodule Livebook.K8sAPI do @spec list_pvcs(kubeconfig(), String.t()) :: {:ok, data} | error() when data: list(%{name: String.t(), deleted_at: String.t() | nil}) def list_pvcs(kubeconfig, namespace) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:namespace/persistentvolumeclaims/:name") + req = pvc_req(kubeconfig) case Kubereq.list(req, namespace) do {:ok, %{status: 200, body: %{"items" => items}}} -> @@ -182,7 +183,7 @@ defmodule Livebook.K8sAPI do """ @spec delete_pvc(kubeconfig(), String.t(), String.t()) :: :ok | error() def delete_pvc(kubeconfig, namespace, name) do - req = Kubereq.new(kubeconfig, "api/v1/namespaces/:namespace/persistentvolumeclaims/:name") + req = pvc_req(kubeconfig) case Kubereq.delete(req, namespace, name) do {:ok, %{status: 200}} -> :ok @@ -196,7 +197,13 @@ defmodule Livebook.K8sAPI do @spec list_storage_classes(kubeconfig()) :: {:ok, data} | error() when data: list(%{name: String.t()}) def list_storage_classes(kubeconfig) do - req = Kubereq.new(kubeconfig, "apis/storage.k8s.io/v1/storageclasses/:name") + req = + Req.new() + |> Kubereq.attach( + kubeconfig: kubeconfig, + api_version: "storage.k8s.io/v1", + kind: "StorageClass" + ) case Kubereq.list(req, nil) do {:ok, %{status: 200, body: %{"items" => items}}} -> @@ -252,7 +259,13 @@ defmodule Livebook.K8sAPI do } } - req = Kubereq.new(kubeconfig, "apis/authorization.k8s.io/v1/selfsubjectaccessreviews") + req = + Req.new() + |> Kubereq.attach( + kubeconfig: kubeconfig, + api_version: "authorization.k8s.io/v1", + kind: "SelfSubjectAccessReview" + ) case Kubereq.create(req, access_review) do {:ok, %Req.Response{status: 201, body: body}} -> @@ -289,4 +302,13 @@ defmodule Livebook.K8sAPI do defp result_to_error({:error, exception}) do {:error, %{message: "reason: #{Exception.message(exception)}", status: nil}} end + + defp pod_req(kubeconfig) do + Req.new() |> Kubereq.attach(kubeconfig: kubeconfig, api_version: "v1", kind: "Pod") + end + + defp pvc_req(kubeconfig) do + Req.new() + |> Kubereq.attach(kubeconfig: kubeconfig, api_version: "v1", kind: "PersistentVolumeClaim") + end end diff --git a/mix.exs b/mix.exs index 9886b21f1..20c20b861 100644 --- a/mix.exs +++ b/mix.exs @@ -117,7 +117,7 @@ defmodule Livebook.MixProject do {:mint_web_socket, "~> 1.0.0"}, {:protobuf, "~> 0.13.0"}, {:dns_cluster, "~> 0.1.2"}, - {:kubereq, "~> 0.2.1"}, + {:kubereq, "~> 0.3.0"}, {:yaml_elixir, "~> 2.11"}, {:phoenix_live_reload, "~> 1.2", only: :dev}, {:floki, ">= 0.27.0", only: :test}, diff --git a/mix.lock b/mix.lock index 88f34faf6..791b7f85a 100644 --- a/mix.lock +++ b/mix.lock @@ -21,7 +21,7 @@ "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, "jose": {:hex, :jose, "1.11.10", "a903f5227417bd2a08c8a00a0cbcc458118be84480955e8d251297a425723f83", [:mix, :rebar3], [], "hexpm", "0d6cd36ff8ba174db29148fc112b5842186b68a90ce9fc2b3ec3afe76593e614"}, "jsx": {:hex, :jsx, "3.1.0", "d12516baa0bb23a59bb35dccaf02a1bd08243fcbb9efe24f2d9d056ccff71268", [:rebar3], [], "hexpm", "0c5cc8fdc11b53cc25cf65ac6705ad39e54ecc56d1c22e4adb8f5a53fb9427f3"}, - "kubereq": {:hex, :kubereq, "0.2.1", "d07156482dc1b12302bf6f496939854b69df4b1b4f170e457b279f6112e5113a", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:pluggable, "~> 1.0", [hex: :pluggable, repo: "hexpm", optional: false]}, {:req, "~> 0.5.0", [hex: :req, repo: "hexpm", optional: false]}, {:yaml_elixir, "~> 2.0", [hex: :yaml_elixir, repo: "hexpm", optional: false]}], "hexpm", "5b9126414fa3671f8f3acc8362841f8b2e423b88a6ec5912f81246617c8ac69d"}, + "kubereq": {:hex, :kubereq, "0.3.0", "8146cbb33f2afced18b77dbb266de68ebb3781a962585d88f81898b21100f5fd", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:pluggable, "~> 1.0", [hex: :pluggable, repo: "hexpm", optional: false]}, {:req, "~> 0.5.0", [hex: :req, repo: "hexpm", optional: false]}, {:yaml_elixir, "~> 2.0", [hex: :yaml_elixir, repo: "hexpm", optional: false]}], "hexpm", "e42918f421368a7b3fcf029b0a560b141c0a70df2e9671c5d4dd30669b5ec7e5"}, "makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"}, "makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"}, "makeup_erlang": {:hex, :makeup_erlang, "1.0.1", "c7f58c120b2b5aa5fd80d540a89fdf866ed42f1f3994e4fe189abebeab610839", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "8a89a1eeccc2d798d6ea15496a6e4870b75e014d1af514b1b71fa33134f57814"},