Documentation
¶
Overview ¶
Package hookapi is the daemon's local HTTP-over-unix-socket protocol. Two servers share one set of handlers: the control server (CLI, all endpoints) and the hook servers (only the runner hook endpoints; one per runner, see RunnerHooks, each directory bind-mounted into that runner alone).
Index ¶
- Constants
- Variables
- func ListenControl(path, group string) (net.Listener, error)
- func NewControlServer(h Handlers) *http.Server
- func NewHookServer(h Handlers) *http.Server
- func RequestFromEnv(getenv func(string) string) scheduler.HookRequest
- func RunHook(ctx context.Context, kind string, getenv func(string) string, stderr io.Writer, ...) int
- func Serve(ctx context.Context, srv *http.Server, ln net.Listener) error
- type Client
- func (c *Client) Drain(ctx context.Context, on bool) error
- func (c *Client) JobCompleted(ctx context.Context, req scheduler.HookRequest) error
- func (c *Client) JobStarted(ctx context.Context, req scheduler.HookRequest) (scheduler.HookResponse, error)
- func (c *Client) Kill(ctx context.Context, runner string) error
- func (c *Client) Status(ctx context.Context, out any) error
- type DrainRequest
- type Handlers
- type HookOptions
- type RunnerHooks
- type StatusError
Constants ¶
const ( EnvSocket = "RUNNER_WARDEN_SOCKET" EnvRunner = "RUNNER_WARDEN_RUNNER" )
Environment variables read by the hook command.
const ( KindJobStarted = "job-started" KindJobCompleted = "job-completed" )
Hook kinds.
const ( PathJobStarted = "/v1/hook/job-started" PathJobCompleted = "/v1/hook/job-completed" PathStatus = "/v1/status" PathDrain = "/v1/drain" PathKillFmt = "/v1/runners/%s/kill" )
Endpoint paths.
const DefaultMaxHookConns = 256
DefaultMaxHookConns caps simultaneous connections across all runner hook sockets, so a misbehaving runner cannot exhaust the daemon.
const FailOpenAfter = 30 * time.Second
FailOpenAfter is how long the daemon may stay unreachable before the hook lets the job proceed.
const HookSocketName = "hook.sock"
HookSocketName is the socket file inside a runner's hook directory.
Variables ¶
var ErrConflict = errors.New("hookapi: conflict")
ErrConflict lets a handler refuse a request that clashes with one already in flight (HTTP 409).
var ErrNotFound = errors.New("hookapi: not found")
ErrNotFound lets a Kill handler report an unknown runner (HTTP 404).
Functions ¶
func ListenControl ¶
ListenControl opens the control socket: mode 0600 (root only) when group is empty, otherwise 0660 and owned by that group.
func NewControlServer ¶
NewControlServer serves every endpoint.
func NewHookServer ¶
NewHookServer serves only the runner hook endpoints.
func RequestFromEnv ¶
func RequestFromEnv(getenv func(string) string) scheduler.HookRequest
RequestFromEnv builds the hook request from the runner's environment.
func RunHook ¶
func RunHook(ctx context.Context, kind string, getenv func(string) string, stderr io.Writer, o HookOptions) int
RunHook implements `runner-warden hook <kind>` and returns the exit code: 0 to let the job run (granted, or fail open), 1 on an explicit rejection (with a line on stderr). job-completed is best effort and always exits 0.
Types ¶
type Client ¶
type Client struct {
Socket string
// contains filtered or unexported fields
}
Client talks to a unix-socket server.
func NewClient ¶
NewClient builds a client for the socket path. There is no overall timeout: the job-started call blocks until admission; use the context.
func (*Client) JobCompleted ¶
JobCompleted notifies the daemon the job finished.
func (*Client) JobStarted ¶
func (c *Client) JobStarted(ctx context.Context, req scheduler.HookRequest) (scheduler.HookResponse, error)
JobStarted asks for admission; it blocks until the verdict.
type DrainRequest ¶
type DrainRequest struct {
Drain bool `json:"drain"`
}
DrainRequest is the body of POST /v1/drain.
type Handlers ¶
type Handlers struct {
Hook func(ctx context.Context, req scheduler.HookRequest) (scheduler.HookResponse, error)
HookCompleted func(ctx context.Context, req scheduler.HookRequest) error
Status func(ctx context.Context) (any, error)
Drain func(on bool) error
Kill func(ctx context.Context, runner string) error
}
Handlers are implemented by the daemon; nil funcs answer 501.
type HookOptions ¶
type HookOptions struct {
Log *slog.Logger
Now func() time.Time
Sleep func(context.Context, time.Duration)
FailOpenAfter time.Duration // default FailOpenAfter
Retry time.Duration // default 1s
}
HookOptions are injectable for tests.
type RunnerHooks ¶
type RunnerHooks struct {
// contains filtered or unexported fields
}
RunnerHooks serves one hook socket per runner: <base>/<runner>/hook.sock. The runner dir is owned by the runner uid with mode 0700 and the socket is 0600, so only that runner (and root) can connect. The handler overrides the runner name in every request with the socket's own, so a runner cannot speak for another, and allows one job-started waiter per runner.
func NewRunnerHooks ¶
func NewRunnerHooks(base string, uid int, h Handlers, maxConns int) *RunnerHooks
NewRunnerHooks serves hook sockets under base, owned by uid. maxConns <= 0 means DefaultMaxHookConns.
func (*RunnerHooks) Close ¶
func (r *RunnerHooks) Close()
Close stops every socket but leaves the dirs: running runners are adopted (and their sockets recreated) by the next daemon.
func (*RunnerHooks) Dir ¶
func (r *RunnerHooks) Dir(runner string) string
Dir is the directory (bind-mounted into the runner) holding the socket.
func (*RunnerHooks) SocketPath ¶
func (r *RunnerHooks) SocketPath(runner string) string
SocketPath is the runner's hook socket.
func (*RunnerHooks) Start ¶
func (r *RunnerHooks) Start(runner string) (string, error)
Start creates and serves the runner's socket, replacing any previous one.
func (*RunnerHooks) Stop ¶
func (r *RunnerHooks) Stop(runner string)
Stop closes the runner's socket (cutting waiting hooks) and removes its dir.
type StatusError ¶
type StatusError struct{ Code int }
StatusError is a non-200 answer.
func (*StatusError) Error ¶
func (e *StatusError) Error() string