chain

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package chain provides a pipeline executor for configuration resolution.

This package implements a stage-based execution pipeline used by the configuration resolver to apply layered configuration from templates, engrams, stories, and step runs.

Executor

The Executor runs a series of stages with configurable error handling:

executor := chain.NewExecutor(logger.WithName("resolver")).
    WithObserver(chain.NewMetricsObserver("engram"))

executor.
    Add(chain.Stage{Name: "template", Mode: chain.ModeFailFast, Fn: applyTemplate}).
    Add(chain.Stage{Name: "engram", Mode: chain.ModeLogAndSkip, Fn: applyEngram}).
    Add(chain.Stage{Name: "story", Mode: chain.ModeLogAndSkip, Fn: applyStory})

if err := executor.Run(ctx); err != nil {
    return err
}

Error Modes

Two error handling modes are supported:

  • ModeFailFast: Stops execution when a stage returns an error
  • ModeLogAndSkip: Logs the error and continues to the next stage

Observers

Observers are notified of stage completion for metrics and logging:

observer := chain.NewMetricsObserver("layer_name")
executor.WithObserver(observer)

The [MetricsObserver] records Prometheus metrics for stage durations and outcomes.

Stage Outcomes

Stages produce one of three outcomes:

  • StageSuccess: Stage completed without error
  • StageError: Stage failed with ModeFailFast
  • StageSkipped: Stage failed with ModeLogAndSkip (execution continued)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Executor

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

Executor runs staged callbacks with shared logging.

func NewExecutor

func NewExecutor(logger logr.Logger) *Executor

NewExecutor creates a resolver chain executor.

func (*Executor) Add

func (e *Executor) Add(stage Stage) *Executor

Add registers a stage in execution order.

func (*Executor) Run

func (e *Executor) Run(ctx context.Context) error

Run executes stages sequentially, honoring the configured mode.

func (*Executor) Warnings

func (e *Executor) Warnings() []StageError

Warnings exposes log-and-skip failures recorded during the most recent Run.

func (*Executor) WithObserver

func (e *Executor) WithObserver(observer Observer) *Executor

type Mode

type Mode int

Mode controls how a stage reacts to errors.

const (
	// ModeFailFast stops execution when the stage returns an error.
	ModeFailFast Mode = iota
	// ModeLogAndSkip records the error and proceeds to the next stage.
	ModeLogAndSkip
)

func (Mode) String

func (m Mode) String() string

String returns the canonical identifier for the mode.

type Observer

type Observer interface {
	StageStarted(ctx context.Context, stage Stage)
	StageCompleted(ctx context.Context, stage Stage, outcome StageOutcome, err error, duration time.Duration)
}

func NewMetricsObserver

func NewMetricsObserver(layer string) Observer

NewMetricsObserver wires resolver stage events into controller metrics.

type Stage

type Stage struct {
	Name string
	Mode Mode
	Fn   StageFunc
}

Stage defines a resolver stage driven by the executor.

type StageError

type StageError struct {
	Stage string
	Err   error
}

StageError captures log-and-skip failures for later inspection.

type StageFunc

type StageFunc func(context.Context) error

StageFunc encapsulates stage logic.

type StageOutcome

type StageOutcome string
const (
	OutcomeSuccess StageOutcome = "success"
	OutcomeSkipped StageOutcome = "skipped"
	OutcomeFailed  StageOutcome = "failed"
)

Jump to

Keyboard shortcuts

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