honeydipper

Driver Developer’s Guide

This document is intended for Honeydipper driver developers. Some programming experience is expected. Theoretically, we can use any programming language, even bash, to develop a driver for honeydipper. For now, there is a go library named github.com/honeydipper/honeydipper/v4/pkg/dipper that makes it easier to do this in golang.

Basics

By Example

Below is a simple driver that does nothing but restarting itself every 20 seconds.

package main

import (
  "flag"
  "github.com/honeydipper/honeydipper/v4/pkg/dipper"
  "os"
  "time"
)

var driver *dipper.Driver

func main() {
  flag.Parse()

  driver = dipper.NewDriver(os.Args[1], "dummy")
  if driver.Service == "receiver" {
    driver.Start = waitAndSendDummyEvent
  }
  driver.Run()
}

func waitAndSendDummyEvent(msg *dipper.Message) {
  go func() {
    time.Sleep(20 * time.Second)
    driver.SendMessage(&dipper.Message{
      Channel: "eventbus",
      Subject: "message",
      Payload: map[string]interface{}{"data": []string{"line 1", "line 2"}},
    })
    driver.State = "cold"
    driver.Ping(msg)
  }()
}

The first thing that a driver does is to parse the command line arguments so the service name can be retrieved through os.Args[1]. Following that, the driver creates a helper object with dipper.NewDriver. The helper object provides hooks for driver to define the functions to be executed at various stage in the life cycle of the driver. A call to the Run() method will start the event loop to receive communication from the daemon.

There are 4 types of hooks offered by the helper objects.

Note that the waitAndSendDummyEvent method is assigned to Start hook. The Start hook needs to return immediately, so the method launches another event loop in a go routine and return the control to the helper object. The second event loop is where the driver actually receives events externally and use driver.SendMessage to relay to the service.

In this example, the dummy driver just manifest a fake event with json data as

{"data": ["line 1", "line 2"]}

The method also sets its status to “cold”, meaning cold restart needed, and uses the Ping command to send its own state to the daemon, so it can be restarted.

Driver lifecycle and states

The driver transitions through the following states during its lifecycle:

loaded → alive → draining → drained
                 → stopping → completed

State Descriptions

State Description
loaded Initial state when the driver binary is known but not yet started
alive The driver is running and actively processing messages. Reached after successful command:start
draining The driver is being asked to finish in-flight work before shutting down (e.g., during config reload). The daemon waits for drained
drained The driver has finished all in-flight work and acknowledged the drain request. The daemon can now safely stop the driver
stopping The driver is shutting down. Reached after command:stop is received
completed The driver process has exited gracefully

State Transitions

  1. loaded → alive: After command:start is sent, the Start hook runs (if defined). The driver has 10 seconds to report alive or it is killed.
  2. alive → draining: During a config change where the driver binary stays the same (hot reload), the daemon sends command:options with new config. The driver should drain gracefully.
  3. alive → stopping: When the daemon decides to stop the driver (cold reload or shutdown), it sends command:stop. The Stop hook runs if defined.
  4. draining → drained: The driver signals it has finished in-flight work.
  5. draining / stopping → completed: The driver process exits.

Configuration Reload States

Scenario Behavior
Hot reload (same metadata, driver alive) Only command:options is sent with new options. No state change.
Cold reload (different metadata or driver dead) Old driver is gracefully stopped (draining → drained → completed). New driver is started fresh.
Crash recovery Up to 3 retries with 30-second backoff. All services must report drained or completed before new config is applied.
Graceful shutdown Drain (15s timeout) → Stop (5s timeout) → Process exit

Example: Handling State Transitions

driver.Start = func(msg *dipper.Message) {
  go func() {
    // Set up connections, listeners, etc.
    driver.State = "alive"
    driver.Ping(msg)
    
    // Main event loop
    for {
      select {
      case <-driver.Done():
        // Clean up resources
        driver.State = "completed"
        return
      }
    }
  }()
}

driver.Reload = func(msg *dipper.Message) {
  // Reload configuration without full restart
  loadNewOptions(msg)
  // No state change required for hot reload
}

Wire Protocol

Drivers communicate with the daemon through stdin/stdout using a binary wire protocol. Each message consists of a text header followed by binary labels and payload.

Message Format

{channel} {subject} {numLabels} {payloadSize}\n
  label1 {len1}\n{value1}
  label2 {len2}\n{value2}
  ...
  {payload bytes}

Envelope Fields

Field Type Description
channel String The message channel (e.g., eventbus, rpc, state, command)
subject String The message subject (e.g., message, call, return, start, stop)
numLabels Integer Number of labels attached to this message
payloadSize Integer Size of the payload in bytes

Label Format

Each label is encoded as a label definition line followed by the label value bytes:

{labelName} {labelSize}\n
{labelValueBytes}

Raw Message Example

A complete raw message on the wire might look like this (shown in pseudo-format):

eventbus message 2 47\n
  label1 6\nvalue1
  status 7\nsuccess
  {"data": ["line 1", "line 2"]}

Channels and Subjects

Currently, the protocol uses these channels:

Channel Subjects Usage
eventbus message, command, return, return_interrupted, agent_command, agent_continue, agent_response, agent_workflow Engine workflow processing
rpc call, return Inter-driver RPC calls
state cold, alive, draining, drained, completed Driver lifecycle management
command options, start, stop Daemon-to-driver commands

Messages

Every message has an envelope, a list of labels and a payload. The envelope is a string ends with a newline, with fields separated by space(s). A valid envelope has following fields in the exact order:

Following the envelop are a list of labels, each label is made up with a label definition line and a list of bytes as label value. The label definition includes

The payload is usually a byte array with certain encoding. As of now, the only encoding we use is “json”. An example of sending a message to the daemon:

driver.SendMessage(&dipper.Message{
  Channel: "eventbus",
  Subject: "message",
  Labels: map[string]string{
    "label1": "value1",
  },
  Payload: map[string]interface{}{"data": []string{"line 1", "line 2"}},
  IsRaw: false, # default
})

The payload data will be encoded automatically. You can also send raw message if use IsRaw as true, meaning that the driver will not attempt to encode the data for you, instead it will use the payload as bytes array directly. In case you need to encode the message yourself, there are two methods, dipper.SerializePayload accepts a *dipper.Message and put the encoded content back into the message, or dipper.SerializeContent which accepts bytes array and return the data structure as map.

When a message is received through the Run() event loop, it will be passed to various handlers as a *dipper.Message struct with raw bytes as payload. You can call dipper.DeserializeContent which accepts a byte array to decode the byte array, and you can also use dipper.DeserializePayload which accepts a *dipper.Message and place the decoded payload right back into the message.

Currently, we are categorizing the messages into 4 different channels:

Key Eventbus Subjects

Subject Direction Description
message Driver → Daemon Incoming event from a driver receiver
command Daemon → Driver Function execution request (from operator)
return Driver → Daemon Function execution result
return_interrupted Daemon → Driver Interrupted function (re-queued by operator)
agent_command Daemon → Driver Agent tool call execution request
agent_continue Driver → Daemon Agent response for session
agent_response Driver → Daemon Agent streaming response
agent_workflow Driver → Daemon Agent-triggered workflow event

RPC

Drivers can make and offer RPC calls to each other. Daemon can also make RPC calls to the drivers. This greatly extends Honeydipper ability to conduct complicated operations. Each driver only need to handle the portion of the work it intends to solve, and outsourcing auxiliary work to other drivers which have the corresponding capabilities.

For example, kubernetes driver interacts with kubernetes clusters, but the task of obtaining the credentials and endpoints is outsourced to the vendor drivers, such as gcloud-gke, through a RPC call getKubeCfg. Another example is how Honeydipper handles encrypted content. Honeydipper supports eyaml style of encrypted content in configurations, and the cipher text is prefixed with a driver name. The decryption driver, gcloud-kms as an example, must offer a RPC call decrypt.

RPC Call Variants

The driver helper provides several RPC call methods with different behaviors:

Method Parameters Returns Blocking Use Case
Call(feature, method, params) feature, method, map params Yes Yes (10s timeout) Standard synchronous RPC
CallNoWait(feature, method, params) feature, method, map params No No Fire-and-forget notifications
CallRaw(feature, method, rawBytes) feature, method, raw bytes Yes Yes (10s timeout) Raw binary data transfer
CallRawNoWait(feature, method, rawBytes) feature, method, raw bytes No No Raw fire-and-forget
CallWithMessage(msg) Pre-built *dipper.Message Yes Yes (10s timeout) Full control over message format

For example, calling the gcloud-kms driver for decryption:

decrypted, err := driver.CallRaw("driver:gcloud-kms", "decrypt", encrypted)

Non-blocking call to emit a metric:

err := driver.CallNoWait("emitter", "counter_increment", map[string]interface{}{
  name: "honeydipper.driver.invoked",
  tags: []string{
    "driver:mydriver",
  },
})

Using raw data transfer:

rawResult, err := driver.CallRaw("driver:image-processor", "compress", imageBytes)

Offering RPC Methods

To offer a RPC method for the system to call, create the function that accept a single parameter *dipper.Message. Add the method to RPCHandlers map, for example:

driver.RPCHandler["mymethod"] = MyFunc

func MyFunc(m *dipper.Message) {
  ...
}

Feel free to panic in your method, the wrapper will send an error response to the caller if that happens. To return data to the caller use the channel Reply on the incoming message. For example:

func MyFunc(m *dipper.Message) {
  dipper.DeserializePayload(m)
  if m.Payload != nil {
    panic(errors.New("not expecting any parameter"))
  }
  m.Reply <- dipper.Message{
    Payload: map[string]interface{}{"mydata": "myvalue"},
  }
}

Interruptible Handlers

RPC handlers can be marked as interruptible to support long-running operations that may be cancelled (e.g., when a workflow is cancelled or a driver is being drained). When an interruptible handler’s context is cancelled instead of completing normally, it sends a return/interrupted response instead of a normal return.

Use case: A driver implements a long-running data sync operation via RPC. If the operator service is restarting or the workflow was cancelled, the handler can be interrupted gracefully:

// Mark a handler as interruptible through the interruptible handler map
driver.RPCHandler["long_running_sync"] = handleSync
driver.RPCHandler["interruptible:long_running_sync"] = handleSync

func handleSync(m *dipper.Message) {
  ctx := driver.GetContext() // Get the cancellable context
  
  for _, item := range items {
    select {
    case <-ctx.Done():
      // Context cancelled — send interrupted response
      m.Reply <- dipper.Message{
        Labels: map[string]string{"error": "interrupted"},
      }
      return
    default:
      processItem(item)
    }
  }
  
  m.Reply <- dipper.Message{
    Payload: map[string]interface{}{"status": "completed"},
  }
}

When the operator receives a return/interrupted message, it re-queues the command so it can be retried later when the system is healthy.

Driver Options

As mentioned earlier, the driver receives the options / configurations from the daemon automatically through the helper object. As the data is stored in hashmap, the helper method driver.GetOption will accept a path and return an Interface() object. The path consists of the dot-delimited key names. If the returned data is also a map, you can use dipper.GetMapData or dipper.GetMapDataStr to retrieve information from them as well. If you are sure the data is a string, you can use driver.GetOptionStr to directly receive it as string.

The helper functions follow the golang convention of returning the value along with a bool to indicate if it is acceptable or not. See below for example.

  NewAddr, ok := driver.GetOptionStr("data.Addr")
  if !ok {
    NewAddr = ":8080"
  }

  hooksObj, ok := driver.GetOption("dynamicData.collapsedEvents")
  ...
  somedata, ok := dipper.GetMapDataStr(hooksObj, "key1.subkey")
  ...

There is always a data section in the driver options, which comes from the configuration file, e.g.:

---
...
drivers:
  webhook:
    Addr: :880
...

Collapsed Events

Usually an event receiver driver just fires raw events to the daemon; it doesn’t have to know what the daemon is expecting. There are some exceptions, for example, the webhook driver needs to know if the daemon is expecting some kind of webhook so it can decide what response to send to the web request sender, 200, 404 etc. A collapsed event is an event definition that has all the conditions, including the conditions from events that the current event is inheriting from. Dipper sends the collapsed events to the driver in the options with key name “dynamicData.collapsedEvents”. Drivers can use the collapsed events to setup the filtering of the events before sending them to daemon. Not only does this allow the driver to generate meaningful feedback to the external requesters, but it also serves as the first line of defence against DDoS attacks on the daemon.

Below is an example of using the collapsed events data in webhook driver:

func loadOptions(m *dipper.Message) {
  hooksObj, ok := driver.GetOption("dynamicData.collapsedEvents")
  if !ok {
    log.Panicf("[%s] no hooks defined for webhook driver", driver.Service)
  }
  hooks, ok = hooksObj.(map[string]interface{})
  if !ok {
    log.Panicf("[%s] hook data should be a map of event to conditions", driver.Service)
  }
  ...
}

func hookHandler(w http.ResponseWriter, r *http.Request) {
  eventData := extractEventData(w, r)

  matched := false
  for SystemEvent, hook := range hooks {
    for _, condition := range hook.([]interface{}) {
      if dipper.CompareAll(eventData, condition) {
        matched = true
        break
      }
    }
    if matched {
      break
    }
  }

  if matched {
    ...
  } else {
    ...
  }
}

The helper function dipper.CompareAll will try to match your event data to the conditions. Daemon uses the same function to determine if a rawEvent is triggering events defined in systems.

Provide Commands

A command is a raw function that provides response to an event. The workflow engine service sends “eventbus:command” messages to the operator service, and operator service will map the message to the corresponding driver and raw function, then forward the message to the corresponding driver with all the parameters as a “collapsed function”. The driver helper provides ways to map raw actions to the function and handle the communications to back to the daemon.

A command handler is very much like the RPC handler mentioned earlier. All you need to do is add it to the driver.CommandProvider.Commands map. The command handler function should always return a value or panic. If it exists without a return, it can block invoking workflow until it times out. If you don’t have any data to return, just send a blank message back like below.

func main() {
  ...
  driver.Commands["wait10min"] = wait10min
  ...
}

func wait10min(m *dipper.Message) {
  go func() {
    time.Sleep(10 * time.Minute)
    m.Reply <- dipper.Message{}
  }()
}

Note that the reply is sent in a go routine; it is useful if you want to make your code asynchronous.

Publishing and packaging

To make it easier for users to adopt your driver, and use it efficiently, you can create a public git repo and let users load some predefined configurations to jump start the integration. The configuration in the repo should usually include:

For example, I created a hypothetical integration for a z-wave switch, the configuration might look like:

---
daemon:
  drivers:
    myzwave:
      name: myzwave
      data:
        Type: go
        Package: github.com/example/cmd/myzwave
  features:
    receiver:
      - "driver:myzwave"
    operator:
      - "driver:myzwave"

system:
  lightwitch:
    data:
      token: "placeholder"
    triggers:
      driver: myzwave
      rawEvent: turned_on
      conditions:
        device_id: "placeholder"
        token: "{{ .sysData.token }}"
    functions:
      driver: myzwave
      rawAction: turn_on
      parameters:
        device_id: "placeholder"
        token: "{{ .sysData.token }}"

workflows:
  all_lights_on:
    - content: foreach_parallel
      data:
        items:
          - list
          - of
          - device_ids
          - to_be_override
        work:
          - type: function
            content:
              target:
                system: lightswitch
                function: turn_on
              parameters:
                device_id: '{{ `{{ .ctx.current }}` }}'

Assuming the configuration is in github.com/example/myzwave-config/init.yaml, the users only need to load the below snippet into their bootstrap repo to load your driver and configurations, and start to customizing.

repos:
  ...
  - repo: https://github.com/example/myzwave-config
  ...