resource

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: Apache-2.0 Imports: 18 Imported by: 14

Documentation

Overview

Package resource contains types and methods to reconcile controller owned resources It is generalized to work with any GroupVersionKind.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateOrUpdate

func CreateOrUpdate(ctx context.Context, cl client.Client, scheme *runtime.Scheme,
	owner client.Object, template TemplateInterface) (*corev1.ObjectReference, error)

CreateOrUpdate cretes or updates resources. The function receives several parameters:

  • ctx: the context. The logger is expected to be within the context, otherwise the function won't produce any logs.
  • cl: the kubernetes API client
  • scheme: the kubernetes API scheme
  • owner: the object that owns the resource. Used to set the OwnerReference in the resource
  • template: the struct that describes how the resource needs to be reconciled. It must implement the TemplateInterface interface. When template.GetEnsureProperties is not set or an empty list, this function will lookup for configuration in the global configuration (see package config).

Types

type Property

type Property string

Property represents a json path to a field in the resource that can be either reconciled to ensure it mathes the desired value or can be ignored to avoid reconciling certain fields in the rource we are not interested in.

type Template

type Template[T client.Object] struct {
	// TemplateBuilder is the function that is used as the basic
	// template for the object. It is called by Build() to create the
	// object.
	TemplateBuilder TemplateBuilderFunction[T]
	// TemplateMutations are functions that are called during Build() after
	// TemplateBuilder has been invoked, to perform mutations on the object that require
	// access to a kubernetes API server.
	TemplateMutations []TemplateMutationFunction
	// IsEnabled specifies whether the resource described by this Template should
	// exist or not.
	IsEnabled bool
	// EnsureProperties are the properties from the desired object that should be enforced
	// to the live object. The syntax is jsonpath.
	EnsureProperties []Property
	// IgnoreProperties are the properties from the live object that should not trigger
	// updates. This is used to ignore nested properties within the "EnsuredProperties". The
	// syntax is jsonpath.
	IgnoreProperties []Property
}

Template implements TemplateInterface.

func NewTemplate

func NewTemplate[T client.Object](tb TemplateBuilderFunction[T]) *Template[T]

NewTemplate returns a new Template struct using the passed parameters

func NewTemplateFromObjectFunction

func NewTemplateFromObjectFunction[T client.Object](fn func() T) *Template[T]

NewTemplateFromObjectFunction returns a new Template using the given kubernetes object as the base.

func (*Template[T]) Apply

func (t *Template[T]) Apply(mutation TemplateBuilderFunction[T]) *Template[T]

Apply chains template functions to make them composable

func (*Template[T]) Build

func (t *Template[T]) Build(ctx context.Context, cl client.Client, o client.Object) (client.Object, error)

Build returns a T resource. It first executes the TemplateBuilder function and then each of the TemplateMutationFunction functions specified by the TemplateMutations field.

func (*Template[T]) Enabled

func (t *Template[T]) Enabled() bool

Enabled indicates if the resource should be present or not

func (*Template[T]) GetEnsureProperties

func (t *Template[T]) GetEnsureProperties() []Property

GetEnsureProperties returns the list of properties that should be reconciled

func (*Template[T]) GetIgnoreProperties

func (t *Template[T]) GetIgnoreProperties() []Property

GetIgnoreProperties returns the list of properties that should be ignored

func (*Template[T]) WithEnabled

func (t *Template[T]) WithEnabled(enabled bool) *Template[T]

func (*Template[T]) WithEnsureProperties

func (t *Template[T]) WithEnsureProperties(ensure []Property) *Template[T]

func (*Template[T]) WithIgnoreProperties

func (t *Template[T]) WithIgnoreProperties(ignore []Property) *Template[T]

func (*Template[T]) WithMutation

func (t *Template[T]) WithMutation(fn TemplateMutationFunction) *Template[T]

func (*Template[T]) WithMutations

func (t *Template[T]) WithMutations(fns []TemplateMutationFunction) *Template[T]

type TemplateBuilderFunction

type TemplateBuilderFunction[T client.Object] func(client.Object) (T, error)

TemplateBuilderFunction is a function that returns a k8s API object (client.Object) when called. TemplateBuilderFunction has no access to cluster live info. A TemplateBuilderFunction is used to return the basic shape of a resource (a template) that can then be further modified before it's compared with it's live state and reconciled.

type TemplateInterface

type TemplateInterface interface {
	Build(ctx context.Context, cl client.Client, o client.Object) (client.Object, error)
	Enabled() bool
	GetEnsureProperties() []Property
	GetIgnoreProperties() []Property
}

TemplateInterface represents a template that can has methods that instruct how a certain resource needs to be progressed to match its desired state.

type TemplateMutationFunction

type TemplateMutationFunction func(context.Context, client.Client, client.Object) error

TemplateMutationFunction represents mutation functions that require an API client, generally because they need to retrieve live cluster information to mutate the object. A TemplateMutationFunction is typically used to modify a template using live values obtained from a kubernetes API server.

Jump to

Keyboard shortcuts

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