engine

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package engine orchestrates backup restore drills.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalExpression

func EvalExpression(expect, actual string) (bool, error)

EvalExpression evaluates an expectation expression against an actual value. Supported expressions:

  • "> N", ">= N", "< N", "<= N", "== N" — numeric comparison
  • "contains \"text\"" — substring match
  • "age < Xh" / "age < Xm" — time freshness check
  • "true" / "false" — boolean
  • literal string comparison (fallback)

func GetDefaultPorts

func GetDefaultPorts(provider string) []int

GetPorts returns the default ports to expose for the provider.

func SortResults

func SortResults(results []DrillResult)

SortResults orders results by drill name for deterministic persisted state.

Types

type AlertSpec

type AlertSpec struct {
	Type     string            `yaml:"type"`
	Endpoint string            `yaml:"endpoint,omitempty"`
	URL      string            `yaml:"url,omitempty"`
	Headers  map[string]string `yaml:"headers,omitempty"`
}

AlertSpec defines where to send alerts.

type BackupConfig

type BackupConfig struct {
	Tool   string     `yaml:"tool"`
	Stanza string     `yaml:"stanza"`
	Source string     `yaml:"source"`
	Repo   RepoConfig `yaml:"repo"`
	Target string     `yaml:"-"`
}

BackupConfig defines where and how to find the backup.

type Check

type Check struct {
	Type   string   `yaml:"type"`
	Name   string   `yaml:"name"`
	SQL    string   `yaml:"sql,omitempty"`
	Keys   []string `yaml:"keys,omitempty"`
	Expect string   `yaml:"expect"`
}

Check defines a validation check.

type CheckResult

type CheckResult struct {
	Name     string
	Type     string
	Expected string
	Actual   string
	Passed   bool
	Duration time.Duration
	Error    error
}

CheckResult holds the outcome of a single validation check.

type Config

type Config struct {
	Drills    []DrillConfig `yaml:"drills"`
	Metrics   MetricsConfig `yaml:"metrics"`
	Reporting ReportConfig  `yaml:"reporting"`
}

Config is the top-level configuration.

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig reads and parses a drill config from the given file path.

func ParseConfig

func ParseConfig(data []byte) (*Config, error)

ParseConfig parses raw YAML bytes into a Config, performing environment variable interpolation and validation.

type Container

type Container interface {
	// ID returns the container identifier.
	ID() string

	// Host returns the hostname or IP for connecting to the container.
	Host() string

	// Port returns the mapped port for the given container port.
	Port(containerPort int) int
}

Container represents a running ephemeral container.

type ContainerConf

type ContainerConf struct {
	Image     string            `yaml:"image"`
	Env       map[string]string `yaml:"env,omitempty"`
	Resources ResourceConf      `yaml:"resources"`
}

ContainerConf defines the ephemeral container spec.

type ContainerSpec

type ContainerSpec struct {
	Image       string
	Env         map[string]string
	Ports       []int
	MemoryLimit int64
	CPULimit    int64
	Cmd         []string
}

ContainerSpec defines what container to create.

type DrillConfig

type DrillConfig struct {
	Name     string       `yaml:"name"`
	Provider string       `yaml:"provider"`
	Schedule string       `yaml:"schedule"`
	Backup   BackupConfig `yaml:"backup"`
	Restore  RestoreSpec  `yaml:"restore"`
	Validate []Check      `yaml:"checks"`
	Alerts   []AlertSpec  `yaml:"alerts"`
}

DrillConfig represents the configuration for a single drill.

func (*DrillConfig) DrillTimeout

func (d *DrillConfig) DrillTimeout() time.Duration

DrillTimeout returns the parsed timeout or a default of 10 minutes.

type DrillResult

type DrillResult struct {
	Name             string
	Provider         string
	StartedAt        time.Time
	Duration         time.Duration
	BackupTimestamp  time.Time
	BackupAge        time.Duration
	ValidationPassed bool
	Checks           []CheckResult
	Error            error
	CleanupSkipped   bool
	TargetID         string
	TargetHost       string
	TargetPorts      map[int]int
}

DrillResult holds the outcome of a single drill execution.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine orchestrates the drill lifecycle.

func New

func New(runtime Runtime, reporter Reporter) *Engine

New creates a new drill engine.

func (*Engine) RegisterProvider

func (e *Engine) RegisterProvider(p Provider)

RegisterProvider adds a provider to the engine's registry.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context, drills []DrillConfig) ([]DrillResult, error)

Run executes all configured drills sequentially.

func (*Engine) RunParallel

func (e *Engine) RunParallel(ctx context.Context, drills []DrillConfig) ([]DrillResult, error)

RunParallel executes all configured drills concurrently.

func (*Engine) SetNoCleanup

func (e *Engine) SetNoCleanup(v bool)

SetNoCleanup configures whether containers are kept after drills.

type MetricsConfig

type MetricsConfig struct {
	Prometheus struct {
		Enabled     bool              `yaml:"enabled"`
		Pushgateway string            `yaml:"pushgateway"`
		Labels      map[string]string `yaml:"labels"`
	} `yaml:"prometheus"`
}

MetricsConfig defines Prometheus metrics settings.

type PreflightProvider

type PreflightProvider interface {
	Preflight(ctx context.Context, rt Runtime, cfg BackupConfig, target Container, checks []Check) error
}

PreflightProvider can validate target capabilities before restore execution.

type Provider

type Provider interface {
	// Name returns the provider identifier (e.g., "postgres", "mysql").
	Name() string

	// Restore pulls a backup and restores it into the target container.
	Restore(ctx context.Context, rt Runtime, cfg BackupConfig, target Container) (*RestoreResult, error)

	// Validate runs provider-specific checks against the restored data.
	Validate(ctx context.Context, rt Runtime, target Container, checks []Check) (*ValidationResult, error)

	// Cleanup performs provider-specific teardown.
	Cleanup(ctx context.Context, target Container) error
}

Provider handles backup restoration for a specific database/store type.

type RepoConfig

type RepoConfig struct {
	Type     string `yaml:"type"`
	Bucket   string `yaml:"bucket"`
	Endpoint string `yaml:"endpoint"`
	Prefix   string `yaml:"prefix"`
	Region   string `yaml:"region"`
}

RepoConfig defines backup storage location.

type ReportConfig

type ReportConfig struct {
	Format    []string `yaml:"format"`
	Output    string   `yaml:"output"`
	Retention string   `yaml:"retention"`
}

ReportConfig defines reporting settings.

func (ReportConfig) RetentionDuration added in v1.0.1

func (r ReportConfig) RetentionDuration() (time.Duration, error)

RetentionDuration returns the reporting retention window.

type Reporter

type Reporter interface {
	// Report publishes results to the configured output(s).
	Report(ctx context.Context, results []DrillResult) error
}

Reporter publishes drill results.

type ResourceConf

type ResourceConf struct {
	Memory string `yaml:"memory"`
	CPU    string `yaml:"cpu"`
}

ResourceConf defines container resource limits.

type RestoreResult

type RestoreResult struct {
	BackupTimestamp string
	BackupSize      int64
	Duration        int64 // milliseconds
}

RestoreResult holds metadata from a restore operation.

type RestoreSpec

type RestoreSpec struct {
	Target    string        `yaml:"target"`
	Container ContainerConf `yaml:"container"`
	Timeout   string        `yaml:"timeout"`
}

RestoreSpec defines how to restore.

type Runtime

type Runtime interface {
	// Create provisions a container with the given spec.
	Create(ctx context.Context, spec ContainerSpec) (Container, error)

	// Exec runs a command inside the container.
	Exec(ctx context.Context, c Container, cmd []string) ([]byte, error)

	// CopyTo copies data into the container filesystem.
	CopyTo(ctx context.Context, c Container, dest string, src io.Reader) error

	// Destroy tears down the container and cleans up.
	Destroy(ctx context.Context, c Container) error

	// Logs returns the container's log stream.
	Logs(ctx context.Context, c Container) (io.ReadCloser, error)
}

Runtime abstracts the container execution environment.

type ValidationResult

type ValidationResult struct {
	Checks []CheckResult
}

ValidationResult holds results from provider-specific validation.

Jump to

Keyboard shortcuts

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