transform

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package transform provides a generic implementation of controller which transforms resources A into resources B.

Index

Constants

This section is empty.

Variables

View Source
var (
	// MetricReconcileCycles counts the number of times reconcile loop ran.
	MetricReconcileCycles = expvar.NewMap("reconcile_cycles")

	// MetricReconcileInputItems counts the number of resources reconciled overall.
	MetricReconcileInputItems = expvar.NewMap("reconcile_input_items")

	// MetricCycleBusy counts the number of seconds the controller was busy in the reconcile loop.
	MetricCycleBusy = expvar.NewMap("reconcile_busy")
)

TransformController specific metrics.

Functions

This section is empty.

Types

type Controller

type Controller[Input generic.ResourceWithRD, Output generic.ResourceWithRD] struct {
	generic.NamedController
	// contains filtered or unexported fields
}

Controller provides a generic implementation of a controller which implements controller transforming Input resources into Output resources.

Controller supports full flow with finalizers:

  • if other controllers set finalizers on this controller outputs, this controller will handle this and wait for the finalizers to be fully removed before attempting to delete the output.
  • if this controller is configured to set finalizers on its inputs, the finalizer will only be removed when matching output is destroyed.

func NewController

func NewController[Input generic.ResourceWithRD, Output generic.ResourceWithRD](
	settings Settings[Input, Output],
	opts ...ControllerOption,
) *Controller[Input, Output]

NewController creates a new TransformController.

func (*Controller[Input, Output]) Inputs

func (ctrl *Controller[Input, Output]) Inputs() []controller.Input

Inputs implements controller.Controller interface.

func (*Controller[Input, Output]) Outputs

func (ctrl *Controller[Input, Output]) Outputs() []controller.Output

Outputs implements controller.Controller interface.

func (*Controller[Input, Output]) Run

func (ctrl *Controller[Input, Output]) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error

Run implements controller.Controller interface.

type ControllerOption

type ControllerOption func(*ControllerOptions)

ControllerOption is an option for TransformController.

func WithExtraEventChannel added in v0.3.7

func WithExtraEventChannel(extraEventCh <-chan struct{}) ControllerOption

WithExtraEventChannel adds an extra event channel to the controller.

When this channel receives data, the controller will run the transform function. This is useful to wake up the controller from a goroutine.

func WithExtraInputs

func WithExtraInputs(inputs ...controller.Input) ControllerOption

WithExtraInputs adds extra inputs to the controller.

func WithExtraOutputs added in v0.3.0

func WithExtraOutputs(outputs ...controller.Output) ControllerOption

WithExtraOutputs adds extra outputs to the controller.

func WithIgnoreTearingDownInputs added in v0.3.0

func WithIgnoreTearingDownInputs() ControllerOption

WithIgnoreTearingDownInputs makes controller treat tearing down inputs as 'normal' inputs.

With this setting enabled outputs will still exist until the input is destroyed. This setting is mutually exclusive with WithInputFinalizers.

func WithInputFinalizers

func WithInputFinalizers() ControllerOption

WithInputFinalizers enables setting finalizers on controller inputs.

The finalizer on input will be removed only when matching output is destroyed.

func WithInputListOptions

func WithInputListOptions(opts ...state.ListOption) ControllerOption

WithInputListOptions adds a filter on input resource list.

E.g., query only resources with specific labels.

func WithOnShutdownCallback added in v0.3.7

func WithOnShutdownCallback(onShutdownCallback OnShutdownCallback) ControllerOption

WithOnShutdownCallback adds a callback to be called when the controller is shutting down, either gracefully or due to an error.

func WithOutputKind added in v0.3.7

func WithOutputKind(kind controller.OutputKind) ControllerOption

WithOutputKind sets main output resource kind.

type ControllerOptions

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

ControllerOptions configures TransformController.

type OnShutdownCallback added in v0.3.7

type OnShutdownCallback func(ctx context.Context, rw controller.ReaderWriter, logger *zap.Logger)

OnShutdownCallback is a function called when the controller is shutting down, either gracefully or due to an error.

type Settings

type Settings[Input generic.ResourceWithRD, Output generic.ResourceWithRD] struct {
	// Name is the name of the controller.
	Name string
	// MapMetadataFunc defines a function which creates new empty Output based on Input.
	//
	// Only Output metadata is important, the spec is ignored.
	MapMetadataFunc func(Input) Output
	// MapMetadataOptionalFunc acts like a MapMetadataFunc, but returns optional Output.
	//
	// If the Output is not present, the controller will skip the input.
	MapMetadataOptionalFunc func(Input) optional.Optional[Output]
	// TransformFunc should modify Output based on Input and any additional resources fetched via Reader.
	//
	// If TransformFunc returns error tagged with SkipReconcileTag, the error is ignored and the controller will
	// call reconcile on next event.
	// If TransformFunc returns any other error, controller will fail.
	TransformFunc func(context.Context, controller.Reader, *zap.Logger, Input, Output) error
	// TransformExtraOutputFunc acts like TransformFunc, but used with extra outputs.
	//
	// If the controller produces additional outputs, this function should be used instead of TransformFunc.
	// The only difference is that Reader+Writer is passed as the argument.
	TransformExtraOutputFunc func(context.Context, controller.ReaderWriter, *zap.Logger, Input, Output) error
	// FinalizerRemovalFunc is called when Input is being torn down while Input Finalizers are enabled.
	//
	// This function defines the pre-checks to be done before finalizer on the input can be removed.
	// If this function returns an error, the finalizer won't be removed and this controller will fail.
	// If FinalizerRemoveFunc returns an error tagged with SkipReconcileTag, the error is ignored and the controller will
	// retry on next reconcile event.
	FinalizerRemovalFunc func(context.Context, controller.Reader, *zap.Logger, Input) error
	// FinalizerRemovalExtraOutputFunc is called when Input is being torn down while Input Finalizers and Extra Outputs are enabled.
	//
	// If the controller produces additional outputs, this function should be used instead of FinalizerRemovalFunc.
	// The only difference is that Reader+Writer is passed as the argument.
	FinalizerRemovalExtraOutputFunc func(context.Context, controller.ReaderWriter, *zap.Logger, Input) error
	// PreTransformHook is called before running transform loop for all inputs.
	PreTransformHook func(context.Context, controller.ReaderWriter) error
	// PostTransformHook is called after running transform loop for all inputs.
	PostTransformHook func(context.Context, controller.ReaderWriter) error
}

Settings configures the controller.

type SkipReconcileTag

type SkipReconcileTag struct{}

SkipReconcileTag is used to tag errors when reconciliation should be skipped without an error.

It's useful when next reconcile event should bring things into order.

Jump to

Keyboard shortcuts

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