krsm

package module
v0.0.0-...-440a765 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

krsm

A state machine library for Kubernetes Resources

Example

Consider a Device with following States and SubStates:

---
title: Device Phase Diagram
---
stateDiagram-v2
    stateId

  [*] --> stateId

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrIllegalState is a sentinel error indicating that the state machine is in an illegal state.
	ErrIllegalState = errors.New("illegal state")

	// ErrDuplicateEdge is a sentinel error indicating that a duplicate edge has been defined by the consumer.
	ErrDuplicateEdge = errors.New("duplicate edge")

	// ErrCouldNotTransition is a sentinel error indicating that one could not transition from the current state
	ErrCouldNotTransition = errors.New("could not transition from current state")

	// ErrCannotHaveDiffParents is a sentinel error indicating that a state cannot have different parents
	ErrCannotHaveDiffParents = errors.New("state cannot have diff parent")

	// ErrNoOutEdges is a sentinel error indicating that a state has no outgoing edges
	ErrNoOutEdges = errors.New("no out edges")
)

Functions

This section is empty.

Types

type Builder

type Builder[S State, E Event] struct {
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder[S State, E Event](name string) *Builder[S, E]

func (*Builder[S, E]) Build

func (b *Builder[S, E]) Build() (stateMachine StateMachine[S, E], err error)

Build builds the Default State Machine. Internally ensures that indices are set on the edges for optimized adjacency list traversal

func (*Builder[S, E]) ConfigureState

func (b *Builder[S, E]) ConfigureState(state S) StateConfigurator[S, E]

func (*Builder[S, E]) ConfigureSubState

func (b *Builder[S, E]) ConfigureSubState(subState S, parentState S) StateConfigurator[S, E]

type Event

type Event interface {
	~string
	constraints.Ordered // if it is not used in a map then remove this
}

type Guard

type Guard[S State, E Event] func(resource Resource[S, E]) bool

func InvertGuard

func InvertGuard[S State, E Event](guard Guard[S, E]) Guard[S, E]

type Resource

type Resource[S State, E Event] interface {
	GetNamespace() string
	GetName() string
	// CurrentState returns the current state of the resource. It should be set at initialization to the initial state.
	// It is the resource implementor's  responsibility to ensure this is concurrent safe.
	CurrentState() S
	// SetTransition records the given transition and also changes the current state of the resource.
	// It is the resource implementor's  responsibility to ensure this is concurrent safe.
	SetTransition(transition Transition[S, E])
}

type State

type State interface {
	~string
	constraints.Ordered
}

type StateConfigurator

type StateConfigurator[S State, E Event] interface {
	ConfigureState(state S) StateConfigurator[S, E]
	ConfigureSubState(subState S, parentState S) StateConfigurator[S, E]
	Target(targetState S, events ...E) StateConfigurator[S, E]
	TargetWithGuard(targetState S, event E, guardLabel string, guardPredicate Guard[S, E]) StateConfigurator[S, E]
	Build() (StateMachine[S, E], error)
}

type StateMachine

type StateMachine[S State, E Event] interface {
	Name() string
	Trigger(event E, resource Resource[S, E], message string) (transition Transition[S, E], err error)
	States() sets.Set[S]
}

type Transition

type Transition[S State, E Event] struct {
	CreatedTime       metav1.Time
	SourceState       S
	SourceParentState S
	Event             E
	TargetState       S
	TargetParentState S
	Message           string
}

Jump to

Keyboard shortcuts

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