Documentation
¶
Overview ¶
Package spec parses and validates a `preview.yml` into a resolved Stack.
Design notes that matter:
- `${VAR}` interpolation happens ONCE, here, against a known env. Nothing downstream re-interpolates, so a value containing `${...}` can never be re-expanded by accident.
- `stack` resolves to the single identity string every axis namespaces off. It is exported to hooks as $STACK so a hook never has to reconstruct it.
- Axis order is significant: provisioned in declaration order, destroyed in REVERSE.
The TS module kept a module-global `warnings` array; here they are a field on the result, which is what a goroutine-per-request server needs anyway.
Presence, not nullness, is what every optional key is tested on: the TS checked `=== undefined`, and a key written with no value (`compose:` and nothing after it, `kind: ~`) is PRESENT and null — "`compose` must be a mapping", "kind: expected a string, got object" — never the default. Hence `raw.Get(k)` with only `ok` inspected, everywhere but `version`, whose `?? 1` does read a null as the default.
Index ¶
- func EscapeHostRegexp(host string) string
- func Interpolate(input string, vars map[string]string, where string, host *HostValues) (string, error)
- func IsSpecError(err error) bool
- func ParseDuration(v string) (int64, bool)
- func ResolvePreviewDomain(vars map[string]string, declared []string, warn func(string)) string
- func SubdomainEnv(routes []SubdomainRoute) map[string]string
- func SubdomainVarName(profile string) string
- func WildcardRule(host string, depth Depth) string
- type Axis
- type ComposeSpec
- type Depth
- type Error
- type HostValues
- type Kind
- type Orchestrator
- type Requirement
- type SleepPolicy
- type Stack
- type SubdomainConfig
- type SubdomainRoute
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EscapeHostRegexp ¶
EscapeHostRegexp escapes a hostname for embedding in a Go regexp. regexp.QuoteMeta escapes the same fourteen bytes as the JS class `[.*+?^${}()|[\]\\]` (verified against the Go source), so the wake-router line and every generated HostRegexp stay byte-identical.
func Interpolate ¶
func Interpolate(input string, vars map[string]string, where string, host *HostValues) (string, error)
Interpolate substitutes `${VAR}` from vars and `${vars.X}`/`${secrets.X}` from host. An unknown or EMPTY variable is an error rather than "": silently expanding `${PR}` to "" yields a stack named `pr-` that collides across every PR. Missing names are reported in first-seen order.
func ParseDuration ¶
ParseDuration is `90s`, `30m`, `2h`, `3d`, `1h30m` → ms; 0 and false for anything else. Integers only; a bare number is refused because "sleep after 30" cannot be read without guessing the unit.
func ResolvePreviewDomain ¶
ResolvePreviewDomain picks the domain generated hostnames anchor to: a DECLARED PREVIEW_DOMAIN, then a declared DOMAIN, then the ambient ones — a declaration beats an ambient value, because a stray DOMAIN in a shell would otherwise anchor every rule on the wrong domain. Both set and disagreeing is a warning, not an error.
The chain is `??`, not `||`: the first candidate that is PRESENT wins even when it is "" (a declared `PREVIEW_DOMAIN: ""` yields no domain rather than falling through to DOMAIN), and "" is then returned as "none". A map key present with "" is how Go spells that distinction.
func SubdomainEnv ¶
func SubdomainEnv(routes []SubdomainRoute) map[string]string
SubdomainEnv is the env handed to compose so a label can interpolate `${PSTACK_WILD_<P>}`.
func SubdomainVarName ¶
SubdomainVarName is `backend` → `PSTACK_WILD_BACKEND`. Dashes become underscores, which makes `admin-api` and `admin_api` collide — the resolver checks for that.
func WildcardRule ¶
WildcardRule is the Traefik rule for one wildcard host. Anchored at both ends; the bare host is deliberately NOT matched — that is the exact-host router's job.
Types ¶
type Axis ¶
type Axis struct {
Name string
Up string // Provision. Must be idempotent — `up` is re-run on every redeploy.
Down string // Destroy. Best-effort: a non-zero exit is reported but does not abort teardown.
AssertGone string // Exit 0 ⇒ the resource is GONE. The leak gate.
AssertLive string // Exit 0 ⇒ the resource EXISTS. Run after `up` to fail fast.
}
Axis is one provisioned thing.
type ComposeSpec ¶
type ComposeSpec struct {
File string
Orchestrator Orchestrator
Profiles []string // never nil
Overlays []string // never nil
Subdomains []SubdomainRoute
}
ComposeSpec is the `compose:` section.
type Error ¶
type Error struct{ Msg string }
Error is a spec that will not parse, or will not resolve. Always the caller's problem: the API maps it to 400 with the prefix `spec: `, the CLI to exit 3.
type HostValues ¶
HostValues are the control plane's `${vars.*}` / `${secrets.*}`. Nil under the bare CLI.
type Kind ¶
type Kind string
Kind is what a deployment IS: `shared` (a host singleton previews borrow — no axes, guarded down) or `isolated` (one tenant, has axes, leaves nothing behind). Explicit, never inferred.
const ( Isolated Kind = "isolated" )
type Orchestrator ¶
type Orchestrator string
Orchestrator is how a compose project is driven.
const ( Compose Orchestrator = "compose" Swarm Orchestrator = "swarm" )
type Requirement ¶
Requirement is a precondition checked by name before `up`.
type SleepPolicy ¶
SleepPolicy is the `sleep:` section; zero means absent.
type Stack ¶
type Stack struct {
Version int
Kind Kind
// Stack is the resolved identity, e.g. `pr-123`. Exported to every hook as $STACK.
Stack string
Compose *ComposeSpec
Sleep *SleepPolicy
Requires []Requirement
Axes []Axis
// Env is the fully-resolved env handed to compose and every hook.
//
// DANGER — seeded from the WHOLE ambient environment before the spec's own `env:` is layered
// on, so it holds every secret this process holds, PSTACK_TOKEN included. Never serialise it,
// never log it, never return it from a response. Use DeclaredEnv to show a spec's variables.
Env map[string]string
// DeclaredEnv is the variable names the SPEC ITSELF declared under `env:`, in order.
DeclaredEnv []string
// Warnings are the non-fatal observations `pstack validate` prints.
Warnings []string
}
Stack is a fully resolved spec.
type SubdomainConfig ¶
type SubdomainConfig struct {
Profile string
Depth Depth
// Host is the explicit host when the spec gave one (a pointer: `host: ""` was given, and the TS
// `??` kept it), nil to derive `<profile>-<stack>.<domain>`.
Host *string
}
SubdomainConfig is one `compose.subdomains` entry before the host is resolved.
func ParseSubdomains ¶
func ParseSubdomains(raw any, where string, interp func(v, at string) (string, error)) ([]SubdomainConfig, error)
ParseSubdomains accepts two shapes: a list of profile names (depth one, host derived) or a mapping of profile → depth | { depth?, host? }. Call it only when the key is present: an absent `compose.subdomains` is no subdomains, but a present null is the "must be a list…" error below.
type SubdomainRoute ¶
type SubdomainRoute struct {
Profile string `json:"profile"`
Host string `json:"host"`
Depth Depth `json:"depth"`
VarName string `json:"varName"`
Rule string `json:"rule"`
}
SubdomainRoute is one resolved wildcard.
func ResolveSubdomains ¶
func ResolveSubdomains(configs []SubdomainConfig, stack string, profiles []string, domain string) ([]SubdomainRoute, error)
ResolveSubdomains turns configs into routes. A missing domain is a hard error: a rule anchored on an empty suffix matches nothing, and a router that silently never fires is worse than a refusal.