Keg docs

Writing a service for Keg

A service is a program Keg runs in a container on its node, beside your work. It is told of what happens on the node, it can write events of its own, set timers, and keep data. It can do only what a person allowed when they installed it, item by item.

Two real services are given as examples with this documentation: extensions/nats, which puts the node's events in a NATS stream, and extensions/query, which keeps them in DuckDB and answers SQL. Copy extensions/nats to begin: it is the smaller of the two. Most of what follows was learned writing them.

The folder

A service is a folder with a keg.service.toml, its manifest, and what it runs: a Dockerfile to build, or an image named by digest.

name = "tally"
version = "0.1.0"
build = "."
command = ["node", "index.js"]
port = 8230
listens = ["work.*"]
emits = ["counted", "summary"]
asks = ["timers:set"]
binds = [
  { name = "data", to = "volume" },
  { name = "key", to = "secret:TALLY_KEY" },
]

Each key, and its limit

Key What it is Limit
name The service's name. Its events, subjects and container are named by it. A lowercase letter, then up to 30 lowercase letters, digits and dashes. Not a name Keg uses (keg, work, gate, service, console, checks and others).
version Yours to choose. 1 to 40 characters.
image The image to run, by digest: ref@sha256:<64 hex>. From a registry on the internet, never a private address. Not with build.
build The folder, within the service's own, to build with its Dockerfile. "." is the folder itself. Never outside the folder. Not with image.
command What to run in the image. At most 32 entries.
port The port of its page, if it has one. 1024 or above, and free.
stage The stage of the work it belongs to, shown with it in keg system ls and the console. build, test, ship or all; all if left out.
publish Other ports it listens on. Each is asked for. 1024 or above, free, at most 32.
memory_mb Its memory. 256 if left out, at most 1024.
listens The types of event it is told of: an exact type, or a prefix ending .*. At most 32. Never *, a delivery. type, an app. type but app.bound, check.requested, or its own types.
emits The types it writes, each under its name: counted is written as tally.counted. Lowercase words joined by dots, at most four; at most 32.
asks Grants beyond what the rest gives. Only work:read and timers:set.
binds What it is handed, by a name of its own: volume, secret:<NAME>, or app-events:<app>:<pattern>. An app's events with token = true give the app a token for the service (below). With from_start = true the service is also sent every event of those types the app still keeps, from the first, once a person allows it (the install question says so). Each port of publish allowed is a binding too, named port-<port>. One volume at most; at most 32. A name may not begin port-. token and from_start only on an app's events.
network "internet" asks for the public internet. Left out, it reaches nothing but its socket.
docs A file of its folder that says how an app uses it, such as README.md. Copied as it is installed, and handed to each piece of work whose app it is bound to, at /keg/services/<name>/README.md, for the agent to read. A plain file inside the folder, not a link, at most 256 KiB of text.

The manifest is at most 64 KiB. A key Keg does not know is refused by name: there is no env, volumes, secrets, network other than internet, privileged or user. writes and asks = ["check:run"] are refused too: only Keg's own checks write evidence in this version.

What each ask allows

A person installing it is asked each of these in turn, in Keg's words. Nothing is ticked for them that reaches past the service's socket.

Ask Allows
listens:<pattern> Being told of every event on the node of that type.
emits:<type> Writing <name>.<type> events, which other services may be told of.
asks:work:read Reading every piece of work on the node: its files, brief, changes and notes, and the names of every environment's secrets, never their values.
asks:timers:set Setting timers for its own types (those in emits).
binds:<n>=volume A volume of its own at /data, at most 1 GiB and 131,072 files.
binds:<n>=secret:<NAME> Reading a secret a person sets, of at most 64 KiB.
binds:<n>=app-events:<app>:<pattern> Hearing an app's events of that pattern, with what each carries, from every organization of the app. This crosses organizations, and the service holds all of it. It can be allowed where the app is on this node, and is heard only where the app is deployed with events or jobs. The app is given nothing.
binds:<n>=app-events:<app>:<pattern>+token The same, and the app is given a token for the service when it is next deployed, which reads every organization's content the service holds of it.
publish:<port> Listening on that port of this computer. Any program on this computer can reach it there.
network:internet Reaching the public internet, never this node's private addresses, its other containers or private networks.

What is not allowed, the service cannot do. Write it to work with less: the query service runs without its app bindings on a node with no such app.

docs is not an ask: the install question says "It gives works that use it its instructions". Write there what an app's code needs to use you: what you keep of an app's event (an event reaches you as app.<app>.<type>, the app's own payload under payload), how to ask, and an example that works. An agent reads it as text written by you, and nothing in it overrides what the agent was told or may do.

Talking to the node

The node answers on a socket, /run/keg/kegd.sock, also given as KEG_SOCKET. In JavaScript, require('@keg/app/node') gives connectNode(), with no dependency; copy it into your folder as both examples do, since the build reaches only your folder: sdk/ts/src/node.js and the error.js it requires beside it, into vendor/keg-node.

const { connectNode } = require('./vendor/keg-node/node.js');
const node = connectNode();

// Each delivery, at least once. Return when your work is durable.
node.watch(async (event, delivery) => {
  await store.add(event);
  await node.emit('counted', `tally/${event.subject}`, { type: event.type });
  return { kept: true };
});

// An event of its own, written when its time comes.
await node.setTimer('summary', 'tally/daily', Math.floor(Date.now() / 1000) + 86400);

// What a person allowed it, asked for when it is needed.
const { value } = await node.binding('key');

The protocol underneath, for another language, is in PROTOCOL.md.

The walls

A service runs with:

A service that stops by itself is started again, after 1, 2, 4, 8 and 16 seconds. After five restarts in five minutes it is left failed, with its last line, until a person starts it.

In a check

An app whose events you are bound to may name you in a check (services: [query] beside db: true). Your service is then started a second time, for that check alone, and removed when it ends:

So: start with empty state and secrets you have never seen, without the internet, and say plainly in your log why if you cannot. A check of one that cannot start fails, with your last line. A check names at most four services, and at most two checks on a node have instances at once.

Building it

A node builds services from their folders only once a person turns that on (keg system builds on). A service that names its image by digest installs without it.

Delivery is at least once

The node may give you an event twice: after a crash, a lease that ran out, or a restart. So:

Bounding what you store

Your volume is 1 GiB, and nothing frees it for you.

A published port, or a page

A port of this computer is open to every program on it: other users' programs, any container a person runs with no port of its own, and any web page a browser has open, which can send requests to 127.0.0.1.

Install, look, remove

keg system builds on                  # once, if it is built from its folder
keg system install ./tally            # asks each item; or --allow ID, --allow all
keg system secret tally TALLY_KEY     # each secret it reads
keg system start tally
keg system ls                         # what it was allowed, and how it is
keg system decline tally              # stop offering what was left out
keg events --type tally.counted       # what it wrote
keg events timers                     # what it set
keg system rm tally                   # keeps its volume
keg system rm tally --data            # and its volume

The console's Keg services page does the same: "Install from a folder…" shows each ask, what is approved, and what is not set yet, and sets a secret in a password field (sent to the node, and never shown again); "Stop asking" stops offering what a person left out, until the service is installed again with different asks (the node compares the manifest installed, not its folder; installing it again still asks); Events shows its deliveries, what they returned, and its timers.

Checklist

Keg's documentation. Something wrong or missing? Say so.