Skip to main content
Version: 0.12.0

The program

The greeter answers one question, greeter.hello, with a greeting for the name it is sent. It is one file in each language, and it does what every Plugin does in the same order: it declares itself, starts, says how it is, answers what it is sent, and closes its Session when it is asked to stop. It replaces the program the template wrote in plugin/; in Python, the file is the one you made by hand.

plugin/main.go
// Command greeter is a Plugin that answers one question: who it is greeting.
package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/yoke-project/yoke-sdk-go/plugin"
)

func declaration() plugin.Declaration {
return plugin.Declaration{
ID: "com.example.greeter",
Queries: []string{"greeter.hello"},
Capabilities: []plugin.Capability{
{Name: "query.hello.answer", Governs: plugin.Object{Query: "greeter.hello"}},
},
}
}

func main() {
// The Manifest is generated from the declaration, so the two cannot disagree.
if len(os.Args) == 2 && os.Args[1] == "--manifest" {
os.Stdout.Write(declaration().Manifest())
return
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
u, err := plugin.Start(ctx, declaration())
if err != nil {
fmt.Fprintln(os.Stderr, "greeter:", err)
os.Exit(1)
}
u.Health(0, "ready to greet")
for {
select {
case event, open := <-u.Events():
if !open {
return
}
if q, ok := event.(plugin.Question); ok && q.Type == "greeter.hello" {
u.Answer(q, []byte("hello, "+string(q.Payload)))
}
case <-ctx.Done():
// Asked to stop: close the Session in order, and leave once it has ended.
u.Close()
<-u.Done()
return
}
}
}

What each part does​

The five programs are made of the same parts, named the way each language names things:

PartWhat it does
the declarationthe identity, the question the Plugin answers, and the capability that governs it. Everything a Plugin may be asked or may send is declared, and each object is governed by exactly one capability (the Manifest, capabilities)
--manifestprints the Manifest the declaration generates. The program is the only source of it, so the file shipped beside the program cannot disagree with what the program registers
the startreads what the Core handed the process at launch, binds the unit's own socket, registers, and opens the Session. A refusal is returned or thrown, and nothing is tried again (what a unit arrives with, admission)
the first health reporta grade on the scale an event's severity uses, 0 to 99 and higher more serious, and a line for a person. The library repeats the last report at every beat and sends none before the first, so a Plugin that never reports loses its liveness and is ended (the families)
the eventswhat the Core sends, in the order it sent it: a question to answer, a command to acknowledge, a stream to start or stop, a refusal, and the end of the Session
the answercorrelated to its question by the library. The payload is the Plugin's own bytes: the Core carries it and never reads it
the closeon SIGTERM, the Core's way of asking a unit to stop: the Plugin closes the Session, waits for its end, and exits. A unit still running when the deployment's stop window has passed is killed (the Session)

What a Plugin does not do is as deliberate. It does not choose its socket, retry a refused start, reconnect, or grant itself anything. The library does the order the contract requires — binding before registering, the heartbeat, the end of the Session — so the program holds only the declaration and what to do with each event.

A Plugin started by hand is not admitted. It has no token, because the Core did not launch it, and the start fails. It runs under a Core, as the next page shows.

Build it, and keep its Manifest​

From greeter/, the composer runs the build yoke-plugin.yaml states, for this machine's architecture unless --arch names another, and leaves the executable in build/<arch>/:

yoke-compose build
build/x86_64/greeter --manifest > plugin/com.example.greeter.yaml
build/x86_64/greeter

The executable depends on nothing on the host: the template builds it with CGO_ENABLED=0, linked statically.

The build runs where build.in says, with what yoke-plugin.yaml states and nothing else of the composer's: the architecture, the language's own name for it, and $YOKE_OUT, where the program is written. Building for another architecture than this machine's is the language's own business — Go and Rust cross-compile; C and C++ need a CMake toolchain file — and a build that cannot refuses rather than write the wrong program.

The Manifest is kept in the source, beside the program. It is what the program prints, written again whenever the declaration changes. Kept there, it can be read before anything is built — by you, by whoever takes the Plugin, by the operator who grants it — and the template's continuous integration, .github/workflows/manifest.yml, builds the program at every change, has it print its Manifest and fails where it differs from the one kept.

Every family writes the same Manifest. The YAML is spelled slightly differently — how far a list is indented, whether a string is quoted — and it says the same thing:

plugin/com.example.greeter.yaml
manifest: 1
id: com.example.greeter
protocol: 1
queries:
- id: greeter.hello
capabilities:
- name: query.hello.answer
governs:
query: greeter.hello

The executable and the Manifest are the Plugin. Every way of running it, or of handing it to somebody, starts from these two files.

The same, by hand

Without the composer, the program is built in its own directory, with the language's own tools, and its Manifest written beside it. The files are where the end of starting it put them: main.go, src/main.rs, greeter.c and greeter.cpp at the directory's root rather than under plugin/.

go mod tidy
CGO_ENABLED=0 go build -o greeter .
./greeter --manifest > com.example.greeter.yaml

The executable is greeter, and it depends on nothing on the host: with CGO_ENABLED=0 it is linked statically.