ctrlkit

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: Apache-2.0 Imports: 14 Imported by: 2

README

ctrlkit

Go Report Card GoDoc Build Status codecov License

ctrlkit is a library written in Go that expedites the development of Kubernetes controllers. It provides a set of interfaces and utilities that can be used to build controllers that are easy to test and maintain. The ctrlkit library is designed to be used in conjunction with the controller-runtime.

Use Cases

Contribution

Contributions are welcome! For more information, see CONTRIBUTING.

License

The project is licensed under the Apache 2.0 license. For more information, see LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Continue = NoRequeue

Continue is an alias of NoRequeue.

View Source
var ErrExit = errors.New("exit")

ErrExit exits the workflow early.

View Source
var Nop = &nopAction{}

Nop is a special action that does nothing.

View Source
var SequentialJoin = OrderedJoin

SequentialJoin is an alias of JoinOrdered, because it runs the actions in both join and sequential style. It is useful to use `SequentialJoin` to declare something that must run after something else. E.g., if B must run after A, but you want B to run no matter what A returns, you can use SequentialJoin(A, B) instead of Sequential(A, B).

Functions

func Exit

func Exit() (ctrl.Result, error)

Exit returns an empty result and an ErrExit.

func ExitIf

func ExitIf(cond bool) (ctrl.Result, error)

ExitIf exits if the condition is true.

func IgnoreExit

func IgnoreExit(r ctrl.Result, err error) (ctrl.Result, error)

IgnoreExit keeps the result but returns a nil when err == ErrExit.

func NeedsRequeue

func NeedsRequeue(result ctrl.Result, err error) bool

NeedsRequeue reports if the result and error indicates a requeue.

func NoRequeue

func NoRequeue() (ctrl.Result, error)

NoRequeue returns an empty result and a nil.

func RequeueAfter

func RequeueAfter(after time.Duration) (ctrl.Result, error)

RequeueAfter returns a result with requeue after set to the given duration and a nil.

func RequeueIfError

func RequeueIfError(err error) (ctrl.Result, error)

RequeueIfError returns an empty result with the err.

func RequeueIfErrorAndWrap

func RequeueIfErrorAndWrap(explain string, err error) (ctrl.Result, error)

RequeueIfErrorAndWrap returns an empty result with a wrapped err.

func RequeueImmediately

func RequeueImmediately() (ctrl.Result, error)

RequeueImmediately returns a result with requeue set to true and a nil.

func ValidateOwnership

func ValidateOwnership(obj, owner client.Object) bool

ValidateOwnership validates the ownership by checking the owner references.

Types

type Action

type Action = internal.Action

Action is the basic unit to form a workflow. It represents some reaction to the states it observes. It is recommended to follow the Single-Responsibility-Rule while designing a Action.

func If

func If(predicate bool, act Action) Action

If returns the given action if predicate is true, or an Nop otherwise.

func IfElse

func IfElse(predicate bool, left, right Action) Action

IfElse returns the action left if predicate is true, or the right otherwise.

func Join

func Join(actions ...Action) Action

Join organizes the actions in a split-join flow, which doesn't guarantee the execution order.

func NewAction

func NewAction(description string, f ActionFunc) Action

NewAction wraps the given description and function into an action.

func OptimizeWorkflow

func OptimizeWorkflow(workflow Action) Action

OptimizeWorkflow optimizes the workflow by eliminating unnecessary layers:

  • Nop in Sequential or Join will be removed
  • Empty Join and Sequential will be omitted
  • Parallel in Sequential will be unwrapped
  • Sequential and Join with single child will be simplified by removing them
  • Sequential(Sequential) will be flattened
  • Join(Join) will be flattened
  • Parallel(Parallel) will be simplified with only one Parallel
  • Timeout(Timeout) will be simplified with the tighter timeout

func OrderedJoin

func OrderedJoin(actions ...Action) Action

OrderedJoin organizes the actions in a split-join flow and guarantees the execution order.

func Parallel

func Parallel(act Action) Action

Parallel wraps the action and runs it in parallel.

func ParallelJoin

func ParallelJoin(actions ...Action) Action

ParallelJoin organizes the actions in a split-join flow and executes them in parallel.

func Retry

func Retry(limit int, act Action) Action

Retry wraps an action into a retryable action. It reruns the action iff. there is a non-exit error.

func RetryInterval

func RetryInterval(limit int, interval time.Duration, act Action) Action

RetryInterval wraps an action into a retryable action. It accepts a retry interval to make gaps between retires.

func Sequential

func Sequential(actions ...Action) Action

Sequential organizes the actions into a sequential flow.

func Shared

func Shared(inner Action) Action

Shared wraps the action into a shared action. Any executions against this action would result in exactly once execution of the inner action and the same result.

func Timeout

func Timeout(timeout time.Duration, act Action) Action

Timeout wraps the reconcile action with a timeout.

type ActionFunc

type ActionFunc func(context.Context) (ctrl.Result, error)

ActionFunc is an alias for func(context.Context) (ctrl.Result, error).

type ActionHook

type ActionHook interface {
	// PreRun is a hook that runs before the action. It's embedded by the ctrlkit-gen tool and should
	// be provided with an option. One can get the name and the states of the action from this hook.
	PreRun(ctx context.Context, logger logr.Logger, action string, states map[string]runtime.Object)

	// PostRun is a hook than runs after the action. It's embedded by the ctrlkit-gen tool. One can get
	// the result of the action from this hook.
	PostRun(ctx context.Context, logger logr.Logger, action string, result ctrl.Result, err error)
}

ActionHook provides hooks for actions implementations in controller manager.

type ChainedActionHooks

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

ChainedActionHooks chains a sequence of hooks into one.

func ChainActionHooks

func ChainActionHooks(hooks ...ActionHook) *ChainedActionHooks

ChainActionHooks creates a ChainedActionHooks with the given hooks.

func (*ChainedActionHooks) Add

func (c *ChainedActionHooks) Add(hook ActionHook)

Add adds hook to this.

func (*ChainedActionHooks) PostRun

func (c *ChainedActionHooks) PostRun(ctx context.Context, logger logr.Logger, action string, result ctrl.Result, err error)

PostRun implements the ActionHook interface.

func (*ChainedActionHooks) PreRun

func (c *ChainedActionHooks) PreRun(ctx context.Context, logger logr.Logger, action string, states map[string]runtime.Object)

PreRun implements the ActionHook interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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