reconcile

package
v0.43.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 12 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

Index

Constants

View Source
const DefaultInterval = 60 * time.Second

DefaultInterval is the default reconciliation interval.

Variables

This section is empty.

Functions

func NewStateSnapshot

func NewStateSnapshot() *stateSnapshot

NewStateSnapshot returns a new, empty snapshot.

Types

type Config

type Config struct {
	// Interval is the time between reconciliation cycles.
	// Default: 60s
	Interval time.Duration `yaml:"interval"`
}

Config holds the configuration for the reconciliation loop. Config is passed as a constructor argument — no file I/O in this package.

func (*Config) ApplyDefaults

func (c *Config) ApplyDefaults()

ApplyDefaults sets default values for zero-valued fields.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks that configuration values are acceptable.

type DispatchHandler added in v0.3.0

type DispatchHandler func(ctx context.Context, desired *api.NodeStateSnapshot)

DispatchHandler is a function invoked on every successful pull. Dispatch handlers consume the delivery-queue blocks of the snapshot (e.g. executions), which are not desired state to converge on. They return nothing: a dispatch problem is logged where it happens and must neither block the snapshot update nor count as a handler failure.

type ReachabilityObserver added in v0.7.0

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

ReachabilityObserver logs the control plane's reachability verdict about this node whenever it changes. The verdict is the one signal a node cannot derive for itself: an agent whose state pulls succeed while its heartbeats stop being admitted looks healthy in its own journal and reads as stale or unreachable to everyone else. One line at the moment the verdict changes puts that split where a node operator already looks.

The block is a diagnostic projection, not desired state. The observer stores nothing beyond the last verdict it saw, and the snapshot store still never keeps the block.

A ReachabilityObserver is not safe for concurrent use. Handle is invoked only from the reconcile goroutine, one cycle at a time, so its fields need no mutex.

func NewReachabilityObserver added in v0.7.0

func NewReachabilityObserver(logger *slog.Logger) *ReachabilityObserver

NewReachabilityObserver returns an observer that logs verdict changes through logger.

func (*ReachabilityObserver) Handle added in v0.7.0

func (o *ReachabilityObserver) Handle(_ context.Context, desired *api.NodeStateSnapshot)

Handle observes the snapshot's reachability block. Its signature matches DispatchHandler, so it runs on every successful pull, including the cycles the empty-diff short-circuit ends early.

type ReconcileHandler

type ReconcileHandler func(ctx context.Context, desired *api.NodeStateSnapshot, diff StateDiff) error

ReconcileHandler is a function invoked when drift is detected.

type Reconciler

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

Reconciler periodically compares desired state against a local snapshot and invokes registered handlers to correct drift.

func NewReconciler

func NewReconciler(client StateFetcher, cfg Config, logger *slog.Logger) *Reconciler

NewReconciler creates a new Reconciler with the given configuration. Config defaults are applied automatically.

func (*Reconciler) MaxAge added in v0.43.0

func (r *Reconciler) MaxAge() time.Duration

MaxAge returns the maximum age the reconciler applies: the limit read as at most ten years and raised to at least three reconcile intervals; 0 for none.

It is for the reconcile goroutine, or for the caller before Run.

func (*Reconciler) MaxAgeSeconds added in v0.43.0

func (r *Reconciler) MaxAgeSeconds() int64

MaxAgeSeconds returns the maximum age as the control plane set it, in seconds, before the floor and the cap; 0 for none. The state store records this value.

It is for the reconcile goroutine, or for the caller before Run.

func (*Reconciler) RegisterDispatchHandler added in v0.3.0

func (r *Reconciler) RegisterDispatchHandler(handler DispatchHandler)

RegisterDispatchHandler adds a dispatch handler invoked on every successful pull, regardless of drift. Handlers are called in registration order. RegisterDispatchHandler must be called before Run; it is not safe for concurrent use.

func (*Reconciler) RegisterHandler

func (r *Reconciler) RegisterHandler(handler ReconcileHandler)

RegisterHandler adds a reconciliation handler invoked on drift detection. Handlers are called in registration order. RegisterHandler must be called before Run; it is not safe for concurrent use.

func (*Reconciler) RegisterWithdrawHandler added in v0.43.0

func (r *Reconciler) RegisterWithdrawHandler(handler WithdrawHandler)

RegisterWithdrawHandler adds a handler the reconciler calls when the desired state passes its maximum age. Handlers are called in registration order, before the reconcile handlers get the withdrawn snapshot. RegisterWithdrawHandler must be called before Run; it is not safe for concurrent use.

func (*Reconciler) Restore added in v0.43.0

func (r *Reconciler) Restore(ctx context.Context, stored *api.NodeStateSnapshot, age time.Duration) bool

Restore applies a stored desired state before the first pull, as a pull's apply without the pull: it counts as a pull age old for the maximum age, diffs stored against the current snapshot and, on drift, invokes the reconcile handlers. It runs no dispatch handler, because those mean "a pull succeeded" and would log the absent blocks of a pull that never happened.

stored becomes the snapshot whether or not a handler failed. The snapshot before a restore is empty, so keeping it would leave a later pull unable to remove a restored peer the control plane has dropped. A failure sets reapply instead (see runCycle), so the next successful pull writes every peer it lists again. A negative age counts as 0, and a nil stored does nothing.

Restore returns whether a handler failed and logs stored state applied. It is called once, before Run, from the goroutine that then calls Run.

func (*Reconciler) Run

func (r *Reconciler) Run(ctx context.Context, nodeID string) error

Run starts the reconciliation loop. It blocks until ctx is cancelled. The first cycle runs immediately; subsequent cycles run at cfg.Interval or when TriggerReconcile is called. Under a maximum age (see SetMaxAgeSeconds), a timer armed after every cycle fires when the last successful pull, or the restore, grows older than MaxAge, and Run then withdraws the desired state.

func (*Reconciler) SetCycleObserver added in v0.43.0

func (r *Reconciler) SetCycleObserver(fn DispatchHandler)

SetCycleObserver sets fn to run once at the end of every cycle whose fetch succeeded, with the pulled snapshot: after the reconcile handlers, with drift or without, and also when a handler failed. A panicking observer is logged and otherwise ignored. The state store writes from here rather than from a dispatch handler, so a pulled state that kills the process while it is applied is never stored. SetCycleObserver must be called before Run; it is not safe for concurrent use.

func (*Reconciler) SetMaxAgeSeconds added in v0.43.0

func (r *Reconciler) SetMaxAgeSeconds(v int64)

SetMaxAgeSeconds sets the maximum age of the desired state in whole seconds, counted from the last successful pull. A value below 1 clears the limit. It logs state freshness limit set when the applied value (see MaxAge) changed, state freshness limit removed when a limit is cleared, and state freshness limit below three reconcile intervals when the floor raises the value, once per value.

It is for the reconcile goroutine, or for the caller before Run.

func (*Reconciler) TriggerReconcile

func (r *Reconciler) TriggerReconcile()

TriggerReconcile requests an immediate reconciliation cycle. Multiple rapid calls are coalesced — only one extra cycle runs.

type StateDiff

type StateDiff struct {
	PeersToAdd    []api.SnapshotPeer
	PeersToRemove []string           // node IDs
	PeersToUpdate []api.SnapshotPeer // peers with changed fields

	PolicyChanged  bool
	BridgeChanged  bool
	StateChanged   bool
	ReportsChanged bool
	// SharedSubnetsChanged is true when the shared_subnets lists differ in
	// length, in a Subnet, or in a Bridges list compared in order, because
	// the order is the failover order. A nil and an empty list are equal, as
	// are a nil and an empty Bridges list.
	SharedSubnetsChanged bool
	// AreaChanged is true when one area block is nil and the other is not, or
	// when the two differ in ID, in Border, or in their routes compared in
	// order: in length, in an AreaID, or in a Prefixes or Via list compared
	// in order, because the order of Via is the order the node walks. A nil
	// and an empty Routes, Prefixes or Via list are equal.
	AreaChanged bool
}

StateDiff describes the drift between a desired NodeStateSnapshot (from the control plane) and the current locally-observed snapshot. The four flags of the pointer blocks (policy, bridge, state and reports) are presence-aware: a nil block and a populated block are distinct, so null on the wire ("not populated") drives convergence rather than a no-op. SharedSubnetsChanged is not: a nil and an empty shared_subnets list both mean no shared subnet. AreaChanged is presence-aware for the area block, whose null is a state, and not for the lists inside it.

func ComputeDiff

func ComputeDiff(desired, current *api.NodeStateSnapshot) StateDiff

ComputeDiff compares the desired snapshot from the control plane against the current local snapshot and returns a StateDiff describing what has changed.

func (StateDiff) IsEmpty

func (d StateDiff) IsEmpty() bool

IsEmpty reports whether there is no drift at all.

func (StateDiff) Summary added in v0.2.0

func (d StateDiff) Summary() string

Summary returns a compact human-readable description of the diff for the cycle log, e.g. "peers+1-0~2 policy bridge state reports shared_subnets area". Only changed parts are mentioned; an empty diff returns "none".

type StateFetcher

type StateFetcher interface {
	FetchState(ctx context.Context, nodeID string) (*api.NodeStateSnapshot, error)
}

StateFetcher retrieves the desired-state snapshot from the control plane.

type WithdrawHandler added in v0.43.0

type WithdrawHandler func(ctx context.Context) error

WithdrawHandler withdraws state the reconcile handlers do not own, such as the policy, which lives on the dispatch seam, when the desired state passes its maximum age (see RegisterWithdrawHandler).

Jump to

Keyboard shortcuts

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