spec

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 13 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 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

View Source
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.

View Source
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.

View Source
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.

View Source
const (
	EgressAllow = "allow"
	EgressDeny  = "deny"
)

Egress policies for an Envoy sidecar in transparent mode.

View Source
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.

View Source
const CurrentVersion = 1

CurrentVersion is the newest spec version this build reads and writes.

View Source
const DefaultHealthPort = 8080

DefaultHealthPort is the port of an app's HTTP health check when health sets none.

View Source
const DefaultPlainPort = 8080

DefaultPlainPort is the port an app listens on for plaintext from a terminating proxy when the upstream sets none.

View Source
const FileName = "experiment.yaml"

FileName is the spec's file name inside a project directory.

View Source
const HostRouterName = "bb-host"

HostRouterName is the engine-owned service that routes host access. The name is reserved in the service namespace.

View Source
const MaxReplicas = 10

MaxReplicas bounds apps.<name>.replicas.

Variables

View Source
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.

SidecarTypes lists the types a spec may declare, in display order.

Functions

func Invalid

func Invalid(path, format string, args ...any) error

Invalid builds a validation error with a single violation.

func NetinitName

func NetinitName(instance string) string

NetinitName is the service name of the init container that installs an instance's transparent-mode traffic rules.

func PodName

func PodName(instance string) string

PodName is the service name of an instance's namespace holder.

func SidecarName

func SidecarName(instance string) string

SidecarName is the name of the sidecar service that registers an app instance: <instance>-agent.

func SidecarServiceName

func SidecarServiceName(instance, id string) string

SidecarServiceName is the service name of a sidecar in an instance's pod.

func TargetEnv

func TargetEnv(service string) string

TargetEnv is the variable an app receives for an Envoy outbound target: API_URL for api, ORDER_SERVICE_URL for order-service.

func TerminateTargets

func TerminateTargets(s *Spec) map[string]bool

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

func (a App) HostPort() (int, bool)

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

func (a App) Instances() []string

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

func (a App) IsPod() bool

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

func (a App) ReplicaCount() int

ReplicaCount is the number of instances the app runs.

func (App) TLSPort

func (a App) TLSPort() (int, bool)

TLSPort is the port other containers reach the app on over TLS: its Envoy sidecar's inbound port, else the port it registers (which it serves TLS on with TLS_CERT_FILE and TLS_KEY_FILE). False when it declares neither.

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 New

func New(name string) *Document

New returns a document for a fresh experiment.

func Parse

func Parse(data []byte) (*Document, error)

Parse reads a document. A YAML syntax error is a validation error.

func (*Document) AddApp

func (d *Document) AddApp(app App) error

AddApp appends an app. The name must be unused.

func (*Document) AddProxy

func (d *Document) AddProxy(svc Service) error

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

func (d *Document) AddService(name, typ string, upstreams ...string) error

AddService appends an infra service, with an upstream entry for each named service (proxies only). The name must be unused.

func (*Document) Bytes

func (d *Document) Bytes() ([]byte, error)

Bytes encodes the document with two-space indentation.

func (*Document) Decode

func (d *Document) Decode() (*Spec, error)

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

func (d *Document) HasName(name string) bool

HasName reports whether a service or app already uses name.

func (*Document) Migrate

func (d *Document) Migrate() (from int, changed bool, err error)

Migrate upgrades the document in place to CurrentVersion and reports the starting version and whether anything changed.

func (*Document) Remove

func (d *Document) Remove(name string) (section string, err error)

Remove deletes the service or app called name and reports which section held it.

func (*Document) SetServiceConfig added in v0.3.0

func (d *Document) SetServiceConfig(name string, cfg map[string]string) error

SetServiceConfig sets or updates config keys on an existing infra service, keeping comments and ordering intact.

func (*Document) SetServiceHost added in v0.3.0

func (d *Document) SetServiceHost(name string, h Host) error

SetServiceHost sets or updates the host field on an existing infra service.

func (*Document) SetVersion

func (d *Document) SetVersion(v int)

SetVersion rewrites the version field.

func (*Document) Version

func (d *Document) Version() int

Version returns the declared spec version, or 0 when it is missing or not an integer.

type EnvoyInbound

type EnvoyInbound struct {
	Port    int
	AppPort int
}

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

type FluentBitSidecar struct {
	To     string
	Bucket string
}

FluentBitSidecar ships log files the app writes to the shared directory to an S3 compatible bucket.

type Health

type Health struct {
	Path string
	Port int
}

Health overrides an app's default health check.

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

type PgBouncerSidecar struct {
	Service  string
	PoolMode string
	Port     int
	PoolSize int
}

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 Resources

type Resources struct {
	Memory string
	CPUs   string
}

Resources overrides a service's default limits. Empty fields keep the default.

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

func (s Sidecar) ID() string

ID is the sidecar's name within its app: Name, or the type when unset.

func (Sidecar) Transparent

func (s Sidecar) Transparent() bool

Transparent reports whether the sidecar captures the app's traffic.

func (Sidecar) WithDefaults

func (s Sidecar) WithDefaults() Sidecar

WithDefaults returns the sidecar with every omitted field defaulted.

type Spec

type Spec struct {
	Version  int
	Name     string
	Services []Service
	Apps     []App
}

Spec is the decoded experiment.yaml.

func (*Spec) App

func (s *Spec) App(name string) (App, bool)

App returns the app with the given name.

func (*Spec) Service

func (s *Spec) Service(name string) (Service, bool)

Service returns the infra service with the given name.

func (*Spec) ServicesOfType

func (s *Spec) ServicesOfType(typ string) []string

ServicesOfType returns the names of infra services of a catalog type, in spec order.

type TLS

type TLS struct {
	Mode   string
	Reason string
}

TLS is an edge's TLS override. The zero value means the default (TLS on).

func (TLS) Plaintext

func (t TLS) Plaintext() bool

Plaintext reports whether the edge explicitly turned TLS off.

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

type TypeLookup func(id string) (TypeInfo, bool)

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.

func (Upstream) Mode

func (u Upstream) Mode() string

Mode is the upstream's TLS mode with the default applied.

func (Upstream) Route

func (u Upstream) Route() string

Route is the upstream's route name: Name, or the target's name.

type Use

type Use struct {
	Service string
	TLS     TLS
}

Use is one entry of an app's uses list: an edge to an infra service.

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.

type Violation

type Violation struct {
	Path    string
	Message string
}

Violation is one broken rule, located by its YAML path.

func (Violation) Error

func (v Violation) Error() string

Error makes a Violation usable as a gate check result.

func (Violation) String

func (v Violation) String() string

Jump to

Keyboard shortcuts

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