statemachine

package statemachine

import "chantico/internal/statemachine"

Index

Types

type ActionFunction

type ActionFunction[T Reconcilable] struct {
	Type ActionFunctionType
	Pure func(context.Context, T) *ActionResult
	IO   func(context.Context, client.Client, T) *ActionResult
}

ActionFunction represents a single step in the action pipeline for a state. Either Pure or IO must be set (not both), matching the Type field.

type ActionFunctionType

type ActionFunctionType int

ActionFunctionType distinguishes between Pure and IO action functions.

const (
	ActionFunctionIO ActionFunctionType = iota
	ActionFunctionPure
)

In this context, "Pure" means "does not modify kubernetes cluster resources"

type ActionResult

type ActionResult struct {
	*ctrl.Result
	ph.PatchType
}

ActionResult carries the reconcile result and what kind of patch to apply.

func InitializeFinalizer

func InitializeFinalizer[T Reconcilable](ctx context.Context, resource T) *ActionResult

InitializeFinalizer is a generic Pure action that adds the resource's finalizer if it is not already present.

func RemoveFinalizer

func RemoveFinalizer[T Reconcilable](ctx context.Context, resource T) *ActionResult

RemoveFinalizer is a generic Pure action that removes the resource's finalizer when the resource is being deleted.

type Machine

type Machine[T Reconcilable] struct {
	Actions   map[string][]ActionFunction[T]
	FailState string
}

Machine is a generic state-action machine parameterized over a Reconcilable resource type. It maps states to ordered slices of action functions and knows which state represents failure (to break out of the action loop).

func (*Machine[T]) ExecuteActions

func (m *Machine[T]) ExecuteActions(
	ctx context.Context,
	kubernetesClient client.Client,
	resource T,
	patch *ph.PatchHelper,
) *ActionResult

ExecuteActions runs the action functions registered for the resource's current state. After each action that returns a non-nil result, it patches the resource. It breaks early if the resource enters the FailState or if an action returns a non-nil ctrl.Result (indicating requeue).

type Reconcilable

type Reconcilable interface {
	client.Object
	GetState() string
	SetState(string)
	GetUpdateGeneration() int64
	SetUpdateGeneration(int64)
	GetFinalizerName() string
	GetErrorMessage() string
	SetErrorMessage(string)
}

Reconcilable is the interface that CRD types must satisfy to be used with the generic state-action machine.