Kazan v0.11.0 Kazan.Watcher View Source

Watches for changes on a resource. This works by creating a HTTPoison ASync request. The request will eventually timeout, however the Watcher handles recreating the request when this occurs and requesting new events that have occurred since the last resource_version received.

To use:

  1. Create a request in the normal way that supports the watch parameter. Alternatively create a watch request.
request = Kazan.Apis.Core.V1.list_namespace!() # No need to add watch: true

or

request = Kazan.Apis.Core.V1.watch_namespace_list!()
  1. Start a watcher using the request, and passing the initial resource version and a pid to which to send received messages.
Kazan.Watcher.start_link(request, resource_version: rv, send_to: self())

If no resource_version is passed then the watcher initially makes a normal request to get the starting value. This will only work if a non watch request is used. i.e. Kazan.Apis.Core.V1.list_namespace!() rather than Kazan.Apis.Core.V1.watch_namespace_list!()

  1. In your client code you receive messages for each %Watcher.Event{}. You can pattern match on the object type if you have multiple watchers configured. For example, if the client is a GenServer
  # type is `:added`, `:deleted`, `:modified` or `:gone`
  def handle_info(%Watcher.Event{object: object, from: watcher_pid, type: type}, state) do
    case object do
      %Kazan.Apis.Core.V1.Namespace{} = namespace ->
        process_namespace_event(type, namespace)

      %Kazan.Apis.Batch.V1.Job{} = job ->
        process_job_event(type, job)
    end
    {:noreply, state}
  end
  1. In the case that a :gone is received, then this indicates that Kubernetes has sent a 410 error. In this case the Watcher will automatically terminate and the consumer must clear its cache, reload any cached resources and restart the watcher.

A Watcher can be terminated by calling stop_watch/1.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor

Starts a watch request to the kube server

Stops the watch and terminates the process

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function start_link(request, opts) View Source

Starts a watch request to the kube server

The server should be set in the kazan config or provided in the options.

Options

  • send_to - A pid to which events are sent. Defaults to self().
  • resource_version - The version from which to start watching.
  • name - An optional name for the watcher. Displayed in logs.
  • log - the level to log. When false, disables watcher logging.
  • Other options are passed directly to Kazan.Client.run/2

Stops the watch and terminates the process