Custom Discovery Handlers
Akri has implemented discovery via several protocols with sample brokers and applications to demonstrate usage. However, there may be protocols you would like to use to discover resources that have not been implemented as Discovery Handlers yet. To enable the discovery of resources via a new protocol, you will implement a Discovery Handler (DH), which does discovery on behalf of the Agent. A Discovery Handler is anything that implements the DiscoveryHandler service and Registration client defined in the Akri's discovery gRPC proto file. These DHs run as their own Pods and are expected to register with the Agent, which hosts the Registration service defined in the gRPC interface.
This document will walk you through the development steps to implement a Discovery Handler. If you would rather walk through an example, see Akri's extensibility demo, which walks through creating a Discovery Handler that discovers HTTP based devices. This document will also cover the steps to get your Discovery Handler added to Akri, should you wish to contribute it back.
Before continuing, you may wish to reference the Akri architecture and Akri agent documentation. They will provide a good understanding of Akri, how it works, and what components it is composed of.
A Discovery Handler can be written in any language using protobuf; however, Akri has provided a template for accelerating the development of Rust Discovery Handlers. This document will walk through both of those options. If using the Rust template, still read through the non-Rust section to gain context on the Discovery Handler interface.

Creating a Discovery Handler using Akri's Discovery Handler proto file

This section covers how to use Akri's discovery gRPC proto file to create a Discovery Handler in the language of your choosing. It consists of three steps:
  1. 1.
    Registering your Discovery Handler with the Akri Agent
  2. 2.
    Specifying device filtering in a Configuration
  3. 3.
    Implementing the DiscoveryHandler service

Registering with the Akri Agent

Discovery Handlers and Agents run on each worker Node in a cluster. A Discovery Handler should register with the Agent running on its Node at the Agent's registration socket, which defaults to /var/lib/akri/agent-registration.sock. The directory can be changed when installing Akri by setting agent.host.discoveryHandlers. For example, to request that the Agent's Registration service live at ~/akri/sockets/agent-registration.sock set agent.host.discoveryHandlers=~/akri/sockets when installing Akri. The Agent hosts the Registration service defined in Akri's discovery interface on this socket.
When registering with the Agent, a Discovery Handler specifies its name (the one that will later be specified in Configurations), the endpoint of its Discovery Handler service, and whether the devices it discovers are shared (visible to multiple nodes).
1
message RegisterDiscoveryHandlerRequest {
2
// Name of the `DiscoveryHandler`. This name is specified in an
3
// Akri Configuration, to request devices discovered by this `DiscoveryHandler`.
4
string name = 1;
5
// Endpoint for the registering `DiscoveryHandler`
6
string endpoint = 2;
7
// Specifies the type of endpoint.
8
enum EndpointType {
9
UDS = 0;
10
NETWORK = 1;
11
}
12
EndpointType endpoint_type = 3;
13
// Specifies whether this device could be used by multiple nodes (e.g. an IP camera)
14
// or can only be ever be discovered by a single node (e.g. a local USB device)
15
bool shared = 4;
16
}
Copied!
Also note, that a Discovery Handler must also specify an EndpointType of either UDS or Network in the RegisterDiscoveryHandlerRequest. While Discovery Handlers must register with the Agent's Registration service over UDS, a DiscoveryHandler service can run over UDS or an IP based endpoint. However, the current convention is to use UDS for both registration and discovery.

Specifying device filtering in a Configuration

Discovery Handlers are passed information about what subset of devices to discover from a Configuration's discoveryDetails. Akri's Configuration CRD takes in DiscoveryHandlerInfo, which is defined structurally in Rust as follows:
1
#[derive(Serialize, Deserialize, Clone, Debug)]
2
#[serde(rename_all = "camelCase")]
3
pub struct DiscoveryHandlerInfo {
4
pub name: String,
5
#[serde(default)]
6
pub discovery_details: String,
7
}
Copied!
When creating a Discovery Handler, you must decide what name to give it and add any details you would like your Discovery Handler to receive in the discovery_details string. The Agent passes this string to Discovery Handlers as part of a DiscoverRequest. A Discovery Handler must then parse this string -- Akri's built in Discovery Handlers store an expected structure in it as serialized YAML -- to determine what to discover, filter out of discovery, and so on.
For example, a Configuration that uses the ONVIF Discovery Handler, which allows filtering IP cameras by IP address, MAC address, and scopes, looks like the following.
1
apiVersion: akri.sh/v0
2
kind: Configuration
3
metadata:
4
name: http
5
spec:
6
discoveryHandler:
7
name: onvif
8
discoveryDetails: |+
9
ipAddresses:
10
action: Exclude
11
items:
12
- 10.0.0.1
13
- 10.0.0.2
14
macAddresses:
15
action: Exclude
16
items: []
17
scopes:
18
action: Include
19
items:
20
- onvif://www.onvif.org/name/GreatONVIFCamera
21
- onvif://www.onvif.org/name/AwesomeONVIFCamera
22
discoveryTimeoutSeconds: 2
Copied!
The discoveryHandler.name must match RegisterDiscoveryHandlerRequest.name the Discovery Handler uses when registering with the Agent. Once you know what will be passed to your Discovery Handler, its time to implement the discovery functionality.

Implementing the DiscoveryHandler service

The service should have all the functionality desired for discovering devices via your protocol and filtering for only the desired set. Each device a Discovery Handler discovers is represented by the Device type, as shown in a subset of the discovery proto file below. A Discovery Handler sets a unique id for the device, device connection information that needs to be set as environment variables in Pods that request the device in properties, and any mounts or devices that should be available to requesting Pods.
1
message DiscoverResponse {
2
// List of discovered devices
3
repeated Device devices = 1;
4
}
5
6
message Device {
7
// Identifier for this device
8
string id = 1;
9
// Properties that identify the device. These are stored in the device's instance
10
// and set as environment variables in the device's broker Pods. May be information
11
// about where to find the device such as an RTSP URL or a device node (e.g. `/dev/video1`)
12
map<string, string> properties = 2;
13
// Optionally specify mounts for Pods that request this device as a resource
14
repeated Mount mounts = 3;
15
// Optionally specify device information to be mounted for Pods that request this device as a resource
16
repeated DeviceSpec device_specs = 4;
17
}
Copied!
Note, discover creates a streamed connection with the Agent, where the Agent gets the receiving end of the channel and the Discovery Handler sends device updates via the sending end of the channel. If the Agent drops its end, the Discovery Handler should stop discovery and attempt to re-register with the Agent. The Agent may drop its end due to an error or a deleted Configuration.

Creating a Discovery Handler in Rust using a template

Rust Discovery Handler development can be kick-started using Akri's Discovery Handler template and cargo-generate.
Install cargo-generate and use the tool to pull down Akri's template, specifying the name of the project with the --name parameter.
1
cargo generate --git https://github.com/project-akri/akri-discovery-handler-template.git --name akri-discovery-handler
Copied!
This template abstracts away the work of registering with the Agent and creating the Discovery Handler service. All you need to do is specify the Discovery Handler name, whether discovered devices are sharable, implement discovery, and build the Discovery Handler.
  1. 1.
    Specifying the Discovery Handler name and whether devices are sharable
    Inside the newly created akri-discovery-handler project, navigate to main.rs. It contains all the logic to register our DiscoveryHandler with the Akri Agent. We only need to specify the DiscoveryHandler name and whether the devices discovered by our DiscoveryHandler can be shared. This is the name the Discovery Handler uses when registering with the Agent. It is later specified in a Configuration to tell the Agent which Discovery Handler to use. For example, in Akri's udev Discovery Handler, name is set to udev and shared to false as all devices are locally attached to nodes. The Discovery Handler name also resolves to the name of the socket the template serves the Discovery Handler on.
  2. 2.
    Implementing discovery
    A DiscoveryHandlerImpl Struct has been created (in discovery_handler.rs) that minimally implements the DiscoveryHandler service. Fill in the discover function, which returns the list of discovered devices.
  3. 3.
    Build the Discovery Handler container
    Build your Discovery Handler and push it to your container registry. To do so, we simply need to run this step from the base folder of the Akri repo:
    1
    HOST="ghcr.io"
    2
    USER=[[GITHUB-USER]]
    3
    DH="discovery-handler"
    4
    TAGS="v1"
    5
    6
    DH_IMAGE="${HOST}/${USER}/${DH}"
    7
    DH_IMAGE_TAGGED="${DH_IMAGE}:${TAGS}"
    8
    9
    docker build \
    10
    --tag=${DH_IMAGE_TAGGED} \
    11
    --file=./Dockerfile.discovery-handler \
    12
    . && \
    13
    docker push ${DH_IMAGE_TAGGED}
    Copied!
    Save the name of your image. We will pass it into our Akri installation command when we are ready to deploy our Discovery Handler.

Deploy Akri with your custom Discovery Handler

Now that you have created a Discovery Handler, deploy Akri and see how it discovers the devices and creates Akri Instances for each Device.
Optional: If you've previous installed Akri and wish to reset, you may:
1
sudo helm delete akri
Copied!
Akri has provided Helm templates for custom Discovery Handlers and their Configurations. These templates are provided as a starting point. They may need to be modified to meet the needs of a Discovery Handler. When installing Akri, specify that you want to deploy a custom Discovery Handler as a DaemonSet by setting custom.discovery.enabled=true. Specify the container for that DaemonSet as the Discovery Handler that you built above by setting custom.discovery.image.repository=$DH_IMAGE and custom.discovery.image.repository=$TAGS. To automatically deploy a custom Configuration, set custom.configuration.enabled=true. Customize the Configuration's discovery_details string to contain any filtering information: custom.configuration.discoveryDetails=<filtering info>.
Also set the name the Discovery Handler will register under (custom.configuration.discoveryHandlerName) and a name for the Discovery Handler and Configuration (custom.discovery.name and custom.configuration.name). All these settings come together as the following Akri installation command:
Note: Be sure to consult the user guide to see whether your Kubernetes distribution needs any additional configuration.
1
helm repo add akri-helm-charts https://project-akri.github.io/akri/
2
helm install akri akri-helm-charts/akri \
3
$AKRI_HELM_CRICTL_CONFIGURATION \
4
--set imagePullSecrets[0].name="crPullSecret" \
5
--set custom.discovery.enabled=true \
6
--set custom.discovery.image.repository=$DH_IMAGE \
7
--set custom.discovery.image.tag=$TAGS \
8
--set custom.discovery.name=akri-<name>-discovery \
9
--set custom.configuration.enabled=true \
10
--set custom.configuration.name=akri-<name> \
11
--set custom.configuration.discoveryHandlerName=<name> \
12
--set custom.configuration.discoveryDetails=<filtering info>
Copied!
Note: if your Discovery Handler's discoveryDetails cannot be easily set using Helm, generate a Configuration file and modify it as needed. configuration.enabled`.)
Note: See the cluster setup steps for information on how to set the crictl configuration variable AKRI_HELM_CRICTL_CONFIGURATION
1
helm install akri akri-helm-charts/akri \
2
$AKRI_HELM_CRICTL_CONFIGURATION \
3
--set imagePullSecrets[0].name="crPullSecret" \
4
--set custom.discovery.enabled=true \
5
--set custom.discovery.image.repository=$DH_IMAGE \
6
--set custom.discovery.image.tag=$TAGS \
7
--set custom.discovery.name=akri-<name>-discovery \
8
--set custom.configuration.enabled=true \
9
--set custom.configuration.name=akri-<name> \
10
--set custom.configuration.discoveryHandlerName=<name> \
11
--set custom.configuration.discoveryDetails=to-modify \
12
--set rbac.enabled=false \
13
--set controller.enabled=false \
14
--set agent.enabled=false > configuration.yaml
Copied!
After modifying the file, apply it to the cluster using standard kubectl:
1
kubectl apply -f configuration.yaml
Copied!
Watch as the Agent, Controller, and Discovery Handler Pods are spun up and as Instances are created for each of the discovery devices.
1
watch kubectl get pods,akrii
Copied!
Inspect the Instances' brokerProperties. They will be set as environment variables in Pods that request the Instance's/device's resource.
1
kubectl get akrii -o wide
Copied!
If you simply wanted Akri to expose discovered devices to the cluster as Kubernetes resources, you could stop here. If you have a workload that could utilize one of these resources, you could manually deploy pods that request them as resources. Alternatively, you could have Akri automatically deploy workloads to discovered devices. We call these workloads brokers. To quickly see this, deploy empty nginx pods to discovered resources, by updating our Configuration to include a broker PodSpec.
1
helm upgrade akri akri-helm-charts/akri \
2
$AKRI_HELM_CRICTL_CONFIGURATION \
3
--set imagePullSecrets[0].name="crPullSecret" \
4
--set custom.discovery.enabled=true \
5
--set custom.discovery.image.repository=$DH_IMAGE \
6
--set custom.discovery.image.tag=$TAGS \
7
--set custom.discovery.name=akri-<name>-discovery \
8
--set custom.configuration.enabled=true \
9
--set custom.configuration.name=akri-<name> \
10
--set custom.configuration.discoveryHandlerName=<name> \
11
--set custom.configuration.discoveryDetails=<filtering info> \
12
--set custom.brokerPod.image.repository=nginx
13
watch kubectl get pods,akrii
Copied!
The empty nginx brokers do not do anything with the devices they've requested. Exec into the Pods to confirm that the Device.properties (Instance's brokerProperties) were set as environment variables.
1
sudo kubectl exec -i <broker pod name> -- /bin/sh -c "printenv"
Copied!

Create a broker

Now that you can discover new devices, see our documentation on creating brokers to utilize discovered devices.

Contributing your Discovery Handler back to Akri

Now that you have a working Discovery Handler and broker, we'd love for you to contribute your code to Akri. The following steps will need to be completed to do so:
  1. 1.
    Create an Issue with a feature request for this Discovery Handler.
  2. 2.
    Create a proposal and put in PR for it to be added to the proposals folder.
  3. 3.
    Implement your Discovery Handler and a document named /akri/docs/<name>-configuration.md on how to create a Configuration that uses your Discovery Handler.
  4. 4.
    Create a pull request, that includes Discovery Handler and Dockerfile in the Discovery Handler modules and build directories, respectively. Be sure to also update the minor version of Akri. See contributing to learn more about our versioning strategy.
For a Discovery Handler to be considered fully implemented the following must be included in the PR.
  1. 1.
    A new DiscoveryHandler implementation
  2. 2.
    A sample broker for the new resource.
  3. 3.
    A sample Configuration that uses the new protocol in the form of a Helm template and values.
  4. 4.
    (Optional) A sample end application that utilizes the services exposed by the Configuration
  5. 5.
    Dockerfile[s] for broker [and sample app] and associated update to the makefile
  6. 6.
    Github workflow[s] for broker [and sample app] to build containers and push to Akri container repository.
  7. 7.
    Documentation on how to use the new sample Configuration, like the udev Configuration document
Last modified 1mo ago