Documentation
¶
Overview ¶
Package spec reads, validates, edits, and upgrades experiment.yaml, the source of truth for an experiment.
The file is kept as a YAML node tree so edits preserve the user's comments and key order. Decoding walks that tree directly, which lets every problem be reported with its YAML path instead of stopping at the first one.
Index ¶
- Constants
- Variables
- func Invalid(path, format string, args ...any) error
- func NetinitName(instance string) string
- func PodName(instance string) string
- func SidecarName(instance string) string
- func SidecarServiceName(instance, id string) string
- func TargetEnv(service string) string
- func TerminateTargets(s *Spec) map[string]bool
- func Validate(s *Spec, types TypeLookup) error
- type App
- type Document
- func (d *Document) AddApp(app App) error
- func (d *Document) AddProxy(svc Service) error
- func (d *Document) AddService(name, typ string, upstreams ...string) error
- func (d *Document) Bytes() ([]byte, error)
- func (d *Document) Decode() (*Spec, error)
- func (d *Document) HasName(name string) bool
- func (d *Document) Migrate() (from int, changed bool, err error)
- func (d *Document) Remove(name string) (section string, err error)
- func (d *Document) SetServiceConfig(name string, cfg map[string]string) error
- func (d *Document) SetServiceHost(name string, h Host) error
- func (d *Document) SetVersion(v int)
- func (d *Document) Version() int
- type EnvoyInbound
- type EnvoyOutbound
- type EnvoySidecar
- type FluentBitSidecar
- type Health
- type Host
- type Outlier
- type PgBouncerSidecar
- type ProxyInfo
- type Register
- type Resources
- type Service
- type Sidecar
- type Spec
- type TLS
- type Transparent
- type TypeInfo
- type TypeLookup
- type Upstream
- type Use
- type ValidationError
- type Violation
Constants ¶
const ( SidecarEnvoy = "envoy" SidecarPgBouncer = "pgbouncer" SidecarFluentBit = "fluent-bit" // SidecarConsulAgent is internal: register creates it, and a spec // cannot declare it in sidecars. SidecarConsulAgent = "consul-agent" )
Sidecar types a spec may declare under apps.<name>.sidecars.
const ( EnvoyAdminPort = 15000 // admin API on 127.0.0.1: health and explorers EnvoyOutboundPort = 15001 // transparent mode: redirected outbound traffic EnvoyInboundPort = 15006 // transparent mode: redirected inbound traffic // FluentBitHTTPPort is Fluent Bit's HTTP server (health) on 127.0.0.1. FluentBitHTTPPort = 2020 )
Ports Envoy sidecars bind inside the pod.
const ( DefaultPgBouncerPort = 6432 DefaultPgBouncerPoolMode = "transaction" DefaultPgBouncerPoolSize = 10 DefaultEnvoyLB = "round_robin" DefaultEnvoyTimeout = "5s" DefaultEnvoyRetries = 2 DefaultEgress = EgressAllow // Envoy's own outlier detection defaults (v1.40), written out so the // generated config shows them. DefaultOutlierMaxEjectionPercent = 10 DefaultOutlierInterval = "10s" )
Defaults for sidecar fields a spec leaves out.
const ( EgressAllow = "allow" EgressDeny = "deny" )
Egress policies for an Envoy sidecar in transparent mode.
const ( TLSOn = "on" TLSOff = "off" TLSPassthrough = "passthrough" )
TLS modes an edge may request. A proxy's upstream may also ask for passthrough: the proxy routes the client's TLS session by SNI without decrypting it.
const CurrentVersion = 1
CurrentVersion is the newest spec version this build reads and writes.
const DefaultHealthPort = 8080
DefaultHealthPort is the port of an app's HTTP health check when health sets none.
const DefaultPlainPort = 8080
DefaultPlainPort is the port an app listens on for plaintext from a terminating proxy when the upstream sets none.
const FileName = "experiment.yaml"
FileName is the spec's file name inside a project directory.
const HostRouterName = "bb-host"
HostRouterName is the engine-owned service that routes host access. The name is reserved in the service namespace.
const MaxReplicas = 10
MaxReplicas bounds apps.<name>.replicas.
Variables ¶
var ( IsDNSLabel gate.Rule[string] = func(s string) bool { return dnsLabel.MatchString(s) } IsExperimentOK = IsDNSLabel.And(func(s string) bool { return len(s) <= 40 }) IsServiceOK = IsDNSLabel.And(func(s string) bool { return len(s) <= 63 }) )
Name rules, shared with the core for request validation.
var SidecarTypes = []string{SidecarEnvoy, SidecarPgBouncer, SidecarFluentBit}
SidecarTypes lists the types a spec may declare, in display order.
Functions ¶
func NetinitName ¶
NetinitName is the service name of the init container that installs an instance's transparent-mode traffic rules.
func SidecarName ¶
SidecarName is the name of the sidecar service that registers an app instance: <instance>-agent.
func SidecarServiceName ¶
SidecarServiceName is the service name of a sidecar in an instance's pod.
func TargetEnv ¶
TargetEnv is the variable an app receives for an Envoy outbound target: API_URL for api, ORDER_SERVICE_URL for order-service.
func TerminateTargets ¶
TerminateTargets lists the apps a proxy reaches over a terminate (tls.mode: off) hop. Each runs as a pod with an init container that confines its plaintext port to its private segment.
func Validate ¶
func Validate(s *Spec, types TypeLookup) error
Validate applies every semantic rule to a decoded spec and returns a ValidationError listing all violations, or nil.
Types ¶
type App ¶
type App struct {
Name string
Path string
Command []string
Uses []Use
Ports []int
Health *Health
Register *Register
Resources Resources
// Replicas is the number of instances; zero means one.
Replicas int
// Sidecars share the app's network namespace and a directory with it.
Sidecars []Sidecar
// Host makes the app reachable from the host through the lab's host
// router. Nil means it is not.
Host *Host
}
App is user code built from a directory with a Dockerfile.
func (App) HostPort ¶
HostPort is the port the host router reaches the app on over TLS: host.port, else its Envoy inbound.port, else its register.port. False when the app has no host access or declares none of them.
func (App) Instances ¶
Instances returns the service names of the app's instances: the app's own name with one replica, else <app>-1 … <app>-N.
func (App) IsPod ¶
IsPod reports whether the app runs as a pod: a namespace holder plus the app and its sidecars. Apps with only register keep their own namespace.
func (App) ReplicaCount ¶
ReplicaCount is the number of instances the app runs.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document is experiment.yaml as a YAML node tree. Edits change the tree in place, so comments and key order survive a round trip.
func (*Document) AddProxy ¶
AddProxy appends a proxy service with its config (keys sorted), upstreams, file and static_file. An upstream with only a service is written in the short form; the others as mappings with the fields that are set. The name must be unused.
func (*Document) AddService ¶
AddService appends an infra service, with an upstream entry for each named service (proxies only). The name must be unused.
func (*Document) Decode ¶
Decode turns the document into a Spec. It reports every structural problem (unknown keys, wrong types, missing required fields) with its YAML path. A version newer than CurrentVersion returns errs.ErrNewerFormat. An older version is upgraded in memory; the file itself changes only through Migrate.
func (*Document) Migrate ¶
Migrate upgrades the document in place to CurrentVersion and reports the starting version and whether anything changed.
func (*Document) Remove ¶
Remove deletes the service or app called name and reports which section held it.
func (*Document) SetServiceConfig ¶ added in v0.3.0
SetServiceConfig sets or updates config keys on an existing infra service, keeping comments and ordering intact.
func (*Document) SetServiceHost ¶ added in v0.3.0
SetServiceHost sets or updates the host field on an existing infra service.
func (*Document) SetVersion ¶
SetVersion rewrites the version field.
type EnvoyInbound ¶
EnvoyInbound is TLS termination: Envoy listens on Port with the app's certificate and forwards to the app on 127.0.0.1:AppPort.
type EnvoyOutbound ¶
type EnvoyOutbound struct {
Service string
Port int
LB string // load-balancing policy; empty means round_robin
Timeout string // Go duration; empty means 5s
Retries *int // nil means 2
Outlier *Outlier
}
EnvoyOutbound is an ambassador listener on 127.0.0.1:Port that proxies to Service over verified TLS.
type EnvoySidecar ¶
type EnvoySidecar struct {
Inbound *EnvoyInbound
Outbound []EnvoyOutbound
Transparent *Transparent // nil means off
File string // user-owned native config replacing the generated one
}
EnvoySidecar terminates TLS in front of the app (inbound), proxies the app's plaintext loopback calls to TLS targets (outbound), and optionally captures all of the app's traffic (transparent).
type FluentBitSidecar ¶
FluentBitSidecar ships log files the app writes to the shared directory to an S3 compatible bucket.
type Host ¶
type Host struct {
// Port is the app port the router reaches over TLS; zero means the
// app's TLS port (App.HostPort). Infra services take no port: the
// catalog's HTTPS listener decides.
Port int
}
Host asks for host access: the target answers to https://<name>.<experiment>.localhost:<port> through the lab's host router (HostRouterName).
type Outlier ¶
type Outlier struct {
Consecutive5xx int
Ejection string // Go duration
MaxEjectionPercent int // 1-100; zero means 10, Envoy's default
Interval string // Go duration; empty means 10s, Envoy's default
}
Outlier is Envoy outlier detection: eject a host after Consecutive5xx errors for Ejection, never more than MaxEjectionPercent of the cluster at once. Interval is the analysis sweep that returns ejected hosts.
type PgBouncerSidecar ¶
PgBouncerSidecar pools the app's connections to a Postgres service.
type ProxyInfo ¶
type ProxyInfo struct {
// Modes are the TLS modes an upstream may use (on, off, passthrough).
Modes []string
// HTTP marks an L7 HTTP proxy: an upstream it decrypts (on, off) must
// speak HTTP, so it is an app or an infra service with an HTTPS
// listener. Passthrough is TLS at L4 and takes any TLS target.
HTTP bool
// Targets limits upstreams to these catalog types (pgbouncer:
// postgres); apps are then not allowed. Nil allows apps and any type.
Targets []string
// MinUpstreams is the number of upstreams the type requires; zero means
// none are (an HTTP proxy without upstreams answers 503).
MinUpstreams int
// MaxUpstreams caps the number of upstreams; zero means no cap.
MaxUpstreams int
// StaticFile reports whether the type takes static_file: a YAML
// mapping merged into its generated static configuration.
StaticFile bool
}
ProxyInfo is what validation needs to know about a proxy type's upstreams. It is data on the catalog entry, so adding a proxy changes no validation code.
type Register ¶
type Register struct {
Name string // service name in the registry; empty means the app's name
Port int // the port the app serves on
Check string // path of an HTTPS health check; empty means a TCP check
// Tags are registered with the service, so a registry consumer (such
// as Traefik) can select and configure it.
Tags []string
}
Register asks Breadboard to register an app in a service registry (Consul) through a sidecar agent, so the app's code does not register itself.
type Service ¶
type Service struct {
Name string
Type string
Resources Resources
Config map[string]string
// Upstreams are a proxy's backends, in spec order (the first is the
// default route). Only proxy types take them.
Upstreams []Upstream
// File is a user-owned native config, relative to experiment.yaml,
// that replaces a proxy's generated one.
File string
// StaticFile is a user-owned YAML mapping of static configuration,
// relative to experiment.yaml, merged into a proxy's generated static
// configuration (Traefik's traefik.yml). Only proxy types whose
// ProxyInfo.StaticFile is set take it.
StaticFile string
// Host makes the service reachable from the host through the lab's
// host router. Nil means it is not.
Host *Host
}
Service is an infra service: an instance of a catalog entry.
type Sidecar ¶
type Sidecar struct {
Name string // as written; empty means the type
Type string
Envoy *EnvoySidecar
PgBouncer *PgBouncerSidecar
FluentBit *FluentBitSidecar
}
Sidecar is one entry of an app's sidecars list: a container that shares the app's network namespace and a directory with it. Exactly one of the type-specific fields is set, matching Type.
func (Sidecar) Transparent ¶
Transparent reports whether the sidecar captures the app's traffic.
func (Sidecar) WithDefaults ¶
WithDefaults returns the sidecar with every omitted field defaulted.
type Spec ¶
Spec is the decoded experiment.yaml.
func (*Spec) ServicesOfType ¶
ServicesOfType returns the names of infra services of a catalog type, in spec order.
type Transparent ¶
type Transparent struct {
Egress string // allow or deny undeclared destinations; empty means allow
}
Transparent captures the app's traffic with iptables rules installed by a short-lived init container.
type TypeInfo ¶
type TypeInfo struct {
// Config validates one config key and value. Nil means the type takes no config.
Config func(key, value string) error
// ConfigServices maps the config keys whose value names another
// service of the experiment to the catalog type it must have.
ConfigServices map[string]string
// Plaintext reports whether the entry can enable a plaintext listener for an edge.
Plaintext bool
// Registry reports whether apps using this type can set register.
Registry bool
// HTTPS reports whether the main listener speaks HTTPS, so an Envoy
// sidecar can proxy HTTP to it.
HTTPS bool
// HTTPSListener reports whether any of the entry's listeners speaks
// HTTPS, so the host router can route to a service of the type.
HTTPSListener bool
// Env lists the variables an app using a service of the type receives
// (over a plaintext edge when plain is set). Nil means unknown.
Env func(service string, plain bool) []string
// Proxy is set for proxy types, which take upstreams and file. Nil
// means the type is not a proxy.
Proxy *ProxyInfo
}
TypeInfo is what validation needs to know about a catalog entry.
type TypeLookup ¶
TypeLookup finds a catalog entry by id.
type Upstream ¶
type Upstream struct {
Service string
// Port is the target port; zero means the default for the mode: the
// target's TLS port (on, passthrough) or its plaintext port (off).
Port int
// Name is the route name; empty means Service.
Name string
// TLS is the hop's mode: on (re-encrypt, the default), off (terminate
// at the proxy; plaintext on a private segment) or passthrough.
TLS TLS
}
Upstream is one backend of a proxy: an edge from the proxy to an app (all its replicas) or to an infra service, with its own TLS mode.
type ValidationError ¶
type ValidationError struct {
Violations []Violation
}
ValidationError lists every violation found. It matches errs.ErrValidation.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Is ¶
func (e *ValidationError) Is(target error) bool
Is makes errors.Is(err, errs.ErrValidation) true.