hookapi

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	EnvSocket = "RUNNER_WARDEN_SOCKET"
	EnvRunner = "RUNNER_WARDEN_RUNNER"
)

Environment variables read by the hook command.

View Source
const (
	KindJobStarted   = "job-started"
	KindJobCompleted = "job-completed"
)

Hook kinds.

View Source
const (
	PathJobStarted   = "/v1/hook/job-started"
	PathJobCompleted = "/v1/hook/job-completed"
	PathStatus       = "/v1/status"
	PathDrain        = "/v1/drain"
	PathKillFmt      = "/v1/runners/%s/kill"
)

Endpoint paths.

View Source
const DefaultMaxHookConns = 256

DefaultMaxHookConns caps simultaneous connections across all runner hook sockets, so a misbehaving runner cannot exhaust the daemon.

View Source
const FailOpenAfter = 30 * time.Second

FailOpenAfter is how long the daemon may stay unreachable before the hook lets the job proceed.

View Source
const HookSocketName = "hook.sock"

HookSocketName is the socket file inside a runner's hook directory.

Variables

View Source
var ErrConflict = errors.New("hookapi: conflict")

ErrConflict lets a handler refuse a request that clashes with one already in flight (HTTP 409).

View Source
var ErrNotFound = errors.New("hookapi: not found")

ErrNotFound lets a Kill handler report an unknown runner (HTTP 404).

Functions

func ListenControl

func ListenControl(path, group string) (net.Listener, error)

ListenControl opens the control socket: mode 0600 (root only) when group is empty, otherwise 0660 and owned by that group.

func NewControlServer

func NewControlServer(h Handlers) *http.Server

NewControlServer serves every endpoint.

func NewHookServer

func NewHookServer(h Handlers) *http.Server

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.

func Serve

func Serve(ctx context.Context, srv *http.Server, ln net.Listener) error

Serve serves srv on ln until ctx ends, then shuts down (hooks still waiting are cut after a short grace). It returns nil on a clean shutdown.

Types

type Client

type Client struct {
	Socket string
	// contains filtered or unexported fields
}

Client talks to a unix-socket server.

func NewClient

func NewClient(socket string) *Client

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) Drain

func (c *Client) Drain(ctx context.Context, on bool) error

Drain turns drain mode on or off.

func (*Client) JobCompleted

func (c *Client) JobCompleted(ctx context.Context, req scheduler.HookRequest) error

JobCompleted notifies the daemon the job finished.

func (*Client) JobStarted

JobStarted asks for admission; it blocks until the verdict.

func (*Client) Kill

func (c *Client) Kill(ctx context.Context, runner string) error

Kill stops a runner; a *StatusError with Code 404 means it is unknown.

func (*Client) Status

func (c *Client) Status(ctx context.Context, out any) error

Status fetches the status document into out.

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL