apply

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 38 Imported by: 1 module, 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 apply is the single loader that turns manifest documents into Postgres rows. It has two halves: Plan diffs the submitted documents against the stored rows and reports what would change; Execute authorizes every mutating entry and then writes them, in dependency order.

The boot seed is Plan+Execute over a directory with no authorizer and no prune (see app/seed), so a seed and a POST /apply of the same tree produce the same rows and the same diff vocabulary.

Deliberately out of scope: transactions across kinds (stores are per-kind with their own tx; a mid-way store failure is reported with the partial applied list and re-applying converges), field-level ownership beyond the dirty flag, and reconcile loops.

Index

Constants

This section is empty.

Variables

View Source
var ErrSelectorRequired = errors.New("apply: prune requires a selector")

ErrSelectorRequired is returned when Prune is set without a selector — an unfiltered prune would delete every user row the manifest omits.

View Source
var KindRoutes = map[string]struct{ Plural, Singular string }{
	"Team":           {"teams", "team"},
	"Project":        {"projects", "project"},
	"Provider":       {"providers", "provider"},
	"Host":           {"hosts", "host"},
	"RateLimit":      {"rate-limits", "rate-limit"},
	"HostKey":        {"host-keys", "host-key"},
	"Model":          {"models", "model"},
	"Pricing":        {"pricings", "pricing"},
	"HostBinding":    {"host-bindings", "host-binding"},
	"Policy":         {"policies", "policy"},
	"Group":          {"groups", "group"},
	"Role":           {"roles", "role"},
	"ServiceAccount": {"service-accounts", "service-account"},
	"Key":            {"keys", "key"},
	"RoleBinding":    {"role-bindings", "role-binding"},
	"PolicyBinding":  {"policy-bindings", "policy-binding"},
	"Overlay":        {"models.overlay", "model"},
	"User":           {"users", "user"},
}

KindRoutes maps a manifest kind to the API plural and singular the RBAC verb and the export query use. Kinds absent from a manifest (Setting) are not listed.

Functions

func Authorize

func Authorize(ctx context.Context, p *Result, authzr authz.Authorizer) error

Authorize checks every entry against authzr and returns an *AuthzError for the first denied write. A nil authorizer skips the pass — the boot seed has no actor. Callers that only plan (dry run) run it too, so a caller who may write nothing never sees the diff.

Non-writing entries (unchanged, skip-dirty) are authorized under get and reported as forbidden when denied — the name comes from the caller's own bundle, so saying "this one was ignored" leaks nothing, while dropping it silently made a scoped apply look complete when half of it was skipped.

Types

type Action

type Action string

Action is what a plan entry would do to one row.

const (
	ActionCreate    Action = "create"
	ActionUpdate    Action = "update"
	ActionUnchanged Action = "unchanged"
	// ActionSkipDirty is an operator-edited row that differs from the
	// document and the run refused to overwrite. Reported as drift; nothing
	// is written.
	ActionSkipDirty Action = "skip-dirty"
	ActionDelete    Action = "delete"
	// ActionConflict is a row another writer changed between Plan and
	// Execute. Nothing is written for it; re-planning picks up the change.
	ActionConflict Action = "conflict"
	// ActionForbidden is a row in the bundle the caller may not read.
	// Nothing is written for it and nothing about the stored row is
	// reported — the name is the caller's own.
	ActionForbidden Action = "forbidden"
)

type AuthzError

type AuthzError struct {
	Entry Entry
	Err   error
}

AuthzError reports the row that failed authorization. Nothing was written.

func (*AuthzError) Error

func (e *AuthzError) Error() string

func (*AuthzError) Unwrap

func (e *AuthzError) Unwrap() error

type Counts

type Counts struct {
	Create    int `json:"create"`
	Update    int `json:"update"`
	Unchanged int `json:"unchanged"`
	SkipDirty int `json:"skipDirty"`
	Delete    int `json:"delete"`
	Conflict  int `json:"conflict"`
	Forbidden int `json:"forbidden"`
}

Counts summarises a plan.

type DuplicateError

type DuplicateError struct {
	Kind, Name string
}

DuplicateError reports two submitted documents naming the same row. Last one wins would silently discard the first.

func (*DuplicateError) Error

func (e *DuplicateError) Error() string

type Entry

type Entry struct {
	Kind string `json:"kind"`
	Name string `json:"name"`
	ID   string `json:"id,omitempty"`
	// Action is what would happen (or happened) to this row.
	Action Action `json:"action"`
	// ChangedFields lists the authorable JSON paths that differ, in the
	// same vocabulary an audit row's change.fields uses.
	ChangedFields []string `json:"changedFields,omitempty"`
	// IDMismatch reports a metadata.id in the document that does not match
	// the row its name resolved to. Ids stay server-owned: the document's
	// id is ignored and the mismatch reported.
	IDMismatch string `json:"idMismatch,omitempty"`
	// contains filtered or unexported fields
}

Entry is one planned row change.

func Execute

func Execute(ctx context.Context, p *Result, authzr authz.Authorizer) ([]Entry, error)

Execute authorizes every mutating entry up front and only then writes. A denied row aborts the whole run with an *AuthzError and no writes (all-or-nothing at the authorization step). Writes are sequential in plan order; a store failure returns a *StoreError carrying what already landed. A nil authorizer skips authorization — the boot seed has no actor.

func (Entry) Authorized

func (e Entry) Authorized() (action, kind string, owner meta.Owner)

Authorized returns the RBAC action the entry was authorized under, the resource kind it names, and the row's owner — what an audit row for the change records.

type GovernanceError

type GovernanceError struct {
	Kind, Name string
	Err        error
}

GovernanceError reports a row whose mutation the governance settings refuse. Nothing was planned or written.

func (*GovernanceError) Error

func (e *GovernanceError) Error() string

func (*GovernanceError) Unwrap

func (e *GovernanceError) Unwrap() error

type InvalidError

type InvalidError struct {
	Kind, Name string
	Err        error
}

InvalidError reports a document whose translated row failed its kind's Validate. Nothing was planned or written.

func (*InvalidError) Error

func (e *InvalidError) Error() string

func (*InvalidError) Unwrap

func (e *InvalidError) Unwrap() error

type LicenseError

type LicenseError struct{ Kind, Name, Feature string }

LicenseError reports a document declaring a feature this deployment is not licensed for. Unwraps to license.ErrRequired, which the HTTP layer maps to 403 with the license_required body.

func (*LicenseError) Error

func (e *LicenseError) Error() string

func (*LicenseError) Unwrap

func (e *LicenseError) Unwrap() error

type Options

type Options struct {
	Stores *Stores

	// Force writes over rows an operator edited via the control API
	// (meta.Dirty), resetting them to the manifest. Default false: dirty
	// rows that differ are reported as skip-dirty and left alone.
	Force bool

	// Prune deletes rows that match Selector and are absent from the
	// submitted set. Requires Selector; never touches system-, provider-,
	// or host-owned rows.
	Prune    bool
	Selector string

	// Gov supplies the governance:<kind> sections every planned mutation is
	// checked against. Nil skips the check — the boot seed runs before the
	// settings cache exists.
	Gov settings.Reader

	// License gates the features a manifest may declare. Nil skips the
	// gate: the loaders below the control API (the boot seed, the CLI's
	// seed subcommand) load an operator's own tree from disk, which is not
	// the surface the licence sells. The control plane always supplies one.
	License license.Checker

	// Authz is the authorizer Execute will use. Plan reads it only to know
	// whether there is one at all: without an authorizer the caller is a
	// loader running as the deployment itself (boot seed, CLI), which may
	// re-parent a row the same way an admin can. The control plane always
	// supplies one, so a scoped caller still cannot chown across scopes.
	Authz authz.Authorizer

	// Directory writes the auth module's kinds. Nil builds one over Stores
	// whose grant check probes Authz; the control plane passes its own, which
	// also ends an account's sessions when apply changes it.
	Directory *directory.Directory

	// EnvPasswords lets a User document read its password from this
	// process's environment (password.valueFrom.env). Only `relay seed`
	// sets it: over the API it would let a caller read the server's
	// variables into an account's password.
	EnvPasswords bool
}

Options configures a plan.

type ReservedNameError

type ReservedNameError struct{ Kind, Name string }

ReservedNameError reports a document claiming a name relay owns.

func (*ReservedNameError) Error

func (e *ReservedNameError) Error() string

type Result

type Result struct {
	Entries []Entry `json:"entries"`
	Counts  Counts  `json:"counts"`
	// contains filtered or unexported fields
}

Result is the full ordered set of entries one apply would run — the plan. (The verb Plan is the function that builds it.)

func Plan

func Plan(ctx context.Context, docs []manifest.Document, opts Options) (*Result, error)

Plan diffs docs against the stored rows and returns what would change. It writes nothing.

type Rows

type Rows struct {
	Providers  []*provider.Provider
	Hosts      []*host.Host
	RateLimits []*ratelimit.RateLimit
	HostKeys   []*hostkey.HostKey
	Models     []*model.Model
	Pricings   []*pricing.Pricing
	Bindings   []*binding.Binding
	Policies   []*policy.Policy
	Keys       []*key.Key
	Teams      []*team.Team
	Projects   []*project.Project

	ServiceAccounts []*serviceaccount.ServiceAccount
	Groups          []*group.Group
	Roles           []*role.Role
	RoleBindings    []*rolebinding.RoleBinding
	PolicyBindings  []*policybinding.PolicyBinding
	Overlays        []*overlay.Overlay

	Users []*user.User
}

Rows is every existing row of every kind, loaded once so planning, pruning, and export share a single read of Postgres.

func Load

func Load(ctx context.Context, s *Stores) (*Rows, error)

Load lists every kind once.

type StoreError

type StoreError struct {
	Entry   Entry
	Applied []Entry
	Err     error
}

StoreError reports a write that failed part-way through. Applied lists the entries that landed before it.

func (*StoreError) Error

func (e *StoreError) Error() string

func (*StoreError) Unwrap

func (e *StoreError) Unwrap() error

type Stores

type Stores struct {
	Provider    *provider.Store
	Host        *host.Store
	RateLimit   *ratelimit.Store
	HostKey     *hostkey.Store
	Model       *model.Store
	Policy      *policy.Store
	Pricing     *pricing.Store
	HostBinding *binding.Store
	Key         *key.Store
	Team        *team.Store
	Project     *project.Store

	ServiceAccount *serviceaccount.Store
	Group          *group.Store
	Role           *role.Store
	RoleBinding    *rolebinding.Store
	PolicyBinding  *policybinding.Store
	Overlay        *overlay.Store

	// User resolves the usernames group members, subjects and user-principal
	// keys name, and holds the accounts User documents write.
	User *user.Store
}

Stores bundles every store a plan reads or writes. Declared here rather than reused from app/catalog: app/catalog depends on the boot seed, which depends on this package.

func NewStores

func NewStores(pool *pgxpool.Pool, masterKey []byte) *Stores

NewStores wires every store against pool. masterKey enables stored-mode HostKey rows; nil disables them.

type UnsupportedKindError

type UnsupportedKindError struct{ Kind string }

UnsupportedKindError reports a document apply cannot write. Settings and identity rows have their own endpoints; ignoring them would make a bundle look applied when half of it was dropped.

func (*UnsupportedKindError) Error

func (e *UnsupportedKindError) Error() string

Jump to

Keyboard shortcuts

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