Used by kubegen
to build Resource based
Kubernetes API clients using Req with kubereq
.
While this library can be used directly, it is easier to let
kubegen
generate the API client modules
for you. The resulting clients are then using kubereq
to get the prepared
Req.Request
struct and make the requests to the Kubernetes API Server.
The package can be installed by adding kubereq
to your list of dependencies in
mix.exs
:
def deps do
[
{:kubereq, "~> 0.4.0"}
]
end
The docs can be found at https://hexdocs.pm/kubereq.
This library can be used with plain Req
but the functions in this module
provide an easier API to people used to kubectl
and friends.
While you can use this library with plain Req
functions (see below), it is
easier to prepare a Req
request for a specific resource and then use the
functions defined in the Kubereq
module.
sa_req = Req.new() |> Kubereq.attach(api_version: "v1", kind: "ServiceAccount")
Kubereq.get(sa_req, "my-namespace", "default")
Kubereq.list(sa_req, "my-namespace")
Or use the functions right away, defining the resource through options:
req = Req.new() |> Kubereq.attach()
Kubereq.get(req, "my-namespace", "default", api_version: "v1", kind: "ServiceAccount")
# get the "status" subresource of the default namespace
Kubereq.get(req, "my-namespace", api_version: "v1", kind: "Namespace", subresource: "status")
For resources defined by Kubernetes, the api_version
can be omitted:
Req.new()
|> Kubereq.attach(kind: "Namespace")
|> Kubereq.get("my-namespace")
Inestead of using the function in Kubereq
, you can use
Kubereq.Kubeconfig.Default
to create connection to the cluster and then use
plain Req.request()
to make the request.
req = Req.new() |> Kubereq.attach()
Req.request!(req,
api_version: "v1",
kind: "ServiceAccount",
operation: :get,
path_params: [namespace: "default", name: "default"]
)
You can pass your own Kubeconfigloader pipeline when attaching:
req = Req.new() |> Kubereq.attach(kubeconfig: {Kubereq.Kubeconfig.File, path: "/path/to/kubeconfig.yaml"})
Req.request!(req,
api_version: "v1",
kind: "ServiceAccount",
operation: :get,
path_params: [namespace: "default", name: "default"]
)
Prepare a Req
struct for a specific resource:
sa_req = Req.new() |> Kubereq.attach(api_version: "v1", kind: "ServiceAccount")
Req.request!(sa_req, operation: :get, path_params: [namespace: "default", name: "default"])
Req.request!(sa_req, operation: :list, path_params: [namespace: "default"])