View Source K8s.Client.Mint.HTTPAdapter (k8s v2.0.0-rc.2)

The Mint client implementation. This module handles both, HTTP requests and websocket connections and offers 3 functions for each: request/5, stream/5 and stream_to/6 or respecively websocket_request/3, websocket_stream/3 and websocket_stream_to/4.

processes

Processes

The module creates a process per connection to the Kubernetes API server. It supports HTTP/2 for HTTP requests, but not for websockets. So while an open connection can process multiple HTTP/2 requests, it can only process one single websocket connection. Therefore, each websocket connection is handled in its own process. For HTTP/2 requests, the module K8s.Client.Mint.ConnectionRegistry serves as registry to open connections and register them.

state

State

The module keeps track of the Mint.HTTP connection struct and a map of pending requests for that connection, indexed by the Mint.Types.request_ref(). Depending on the type of the request, the tracked request is either one of three structs:

  • K8s.Client.Mint.Request - for HTTP/2 requests
  • K8s.Client.Mint.UpgradeRequest - for websocket upgrade requests
  • K8s.Client.Mint.WebSocketRequest - open websocket connections

Besides some type specific fields, all of these structs maintain a response field which is a map of response parts, indexed by type of the part (e.g. :headers, :status, :data). In the case of websockets, the incoming chunks are parsed and split by channel, so the type will e :stdout, :stderr, :error.

request-types

Request Types

As mentioned above, there's three ways to make a request.

requests-request-5-and-websocket_request-3

Requests - request/5 and websocket_request/3

Requests are synchronous (blocking) calls to the GenServer. It's not until the requeset is :done resp. the websocket is closed that the GenServer will reply with the complete request's response map.

streams-stream-5-and-websocket_stream-3

Streams - stream/5 and websocket_stream/3

These functions immediately return an Elixir Stream. Running the stream blocks until response parts are received and streams them thereafter.

streamto-stream_to-6and-websocket_stream_to-4

StreamTo - stream_to/6and websocket_stream_to/4

These functions take an extra stream_to argument and return a {:ok, send_to_websocket} tuple. They stream the response parts to the process defined by stream_to. send_to_websocket is a function and serves as a way to send data through the websocket back to Kubernetes.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Makes a synchronous HTTP request to the server.

Opens a connection to Kubernetes, defined by uri and opts, and starts the GenServer.

Same as request/5 but returns a stream of response chunks.

Same as request/5 but streams the response chunks to the process defined by stream_to

Upgrades the connection to a websocket and waits for the other end to close it. Once it is closed, all the received chunks are returned as a map.

Upgrades the connection to a websocket and returns a stream of the received chunks.

Upgrades the connection to a websocket and streams received chunks to the process defined by stream_to. In the case of a sucessful upgrade, this function returns a {:ok, send_to_websocket} tuple where send_to_websocket is a function that can be called to send data to the websocket.

Link to this section Types

@type connection_args_t() ::
  {scheme :: atom(), host :: binary(), port :: integer(), opts :: keyword()}
@type t() :: %K8s.Client.Mint.HTTPAdapter{conn: term(), requests: term()}

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

connection_args(uri, opts)

View Source
@spec connection_args(
  URI.t(),
  keyword()
) :: connection_args_t()
Link to this function

request(pid, method, path, headers, body)

View Source
@spec request(
  pid(),
  method :: binary(),
  path :: binary(),
  Mint.Types.headers(),
  body :: iodata() | nil | :stream
) :: K8s.Client.Provider.response_t()

Makes a synchronous HTTP request to the server.

Link to this function

start_link(connection_args)

View Source
@spec start_link({URI.t(), keyword()}) :: GenServer.on_start()
@spec start_link(connection_args_t()) :: GenServer.on_start()

Opens a connection to Kubernetes, defined by uri and opts, and starts the GenServer.

Link to this function

stream(pid, method, path, headers, body)

View Source
@spec stream(
  pid(),
  method :: binary(),
  path :: binary(),
  Mint.Types.headers(),
  body :: iodata() | nil | :stream
) :: K8s.Client.Provider.stream_response_t()

Same as request/5 but returns a stream of response chunks.

Link to this function

stream_to(pid, method, path, headers, body, stream_to)

View Source
@spec stream_to(
  pid(),
  method :: binary(),
  path :: binary(),
  Mint.Types.headers(),
  body :: iodata() | nil | :stream,
  stream_to :: pid()
) :: K8s.Client.Provider.stream_to_response_t()

Same as request/5 but streams the response chunks to the process defined by stream_to

Link to this function

websocket_request(pid, path, headers)

View Source
@spec websocket_request(
  pid(),
  path :: binary(),
  Mint.Types.headers()
) :: K8s.Client.Provider.websocket_response_t()

Upgrades the connection to a websocket and waits for the other end to close it. Once it is closed, all the received chunks are returned as a map.

Link to this function

websocket_stream(pid, path, headers)

View Source
@spec websocket_stream(
  pid(),
  path :: binary(),
  Mint.Types.headers()
) :: K8s.Client.Provider.stream_response_t()

Upgrades the connection to a websocket and returns a stream of the received chunks.

Link to this function

websocket_stream_to(pid, path, headers, stream_to)

View Source
@spec websocket_stream_to(
  pid(),
  path :: binary(),
  Mint.Types.headers(),
  stream_to :: pid()
) :: K8s.Client.Provider.stream_to_response_t()

Upgrades the connection to a websocket and streams received chunks to the process defined by stream_to. In the case of a sucessful upgrade, this function returns a {:ok, send_to_websocket} tuple where send_to_websocket is a function that can be called to send data to the websocket.