controller

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2025 License: Apache-2.0 Imports: 22 Imported by: 5

Documentation

Index

Constants

View Source
const (
	Base32EncodeStdLowerCase = "abcdefghijklmnopqrstuvwxyz234567"
)

Variables

View Source
var (
	ANNOTATION = &annotationMetadata{}
	LABEL      = &labelMetadata{}
)

Functions

func EnsureAnnotation

func EnsureAnnotation(ctx context.Context, c client.Client, obj client.Object, annKey, annValue string, patch bool, mode ...ModifyMetadataEntryMode) error

EnsureAnnotation ensures that the given annotation has the desired state on the object. If the annotation already exists with the expected value or doesn't exist when deletion is desired, this is a no-op. If the annotation already exists with a different value, a MetadataEntryAlreadyExistsError is returned, unless mode OVERWRITE is set. To remove an annotation, set mode to DELETE. The given annValue does not matter in this case. If patch is set to true, the object will be patched in the cluster immediately, otherwise only the in-memory object is modified. client may be nil when patch is false.

func EnsureLabel

func EnsureLabel(ctx context.Context, c client.Client, obj client.Object, labelKey, labelValue string, patch bool, mode ...ModifyMetadataEntryMode) error

EnsureLabel ensures that the given label has the desired state on the object. If the label already exists with the expected value or doesn't exist when deletion is desired, this is a no-op. If the label already exists with a different value, a MetadataEntryAlreadyExistsError is returned, unless mode OVERWRITE is set. To remove an label, set mode to DELETE. The given labelValue does not matter in this case. If patch is set to true, the object will be patched in the cluster immediately, otherwise only the in-memory object is modified. client may be nil when patch is false.

func GetAnnotation

func GetAnnotation(obj client.Object, key string) (string, bool)

GetAnnotation returns the value of the given annotation key on the object and whether it exists.

func GetField added in v0.6.0

func GetField(obj any, field string, pointer bool) any

GetField returns the value of the field with the given name from the given object. Nested fields can be accessed by separating them with '.' (e.g. "Foo.Bar"). If pointer is true, it returns a pointer to the field value instead. WARNING: This function will panic if pointer is true but obj is not a pointer itself! Panics if the object is nil or the field is not found.

func GetLabel

func GetLabel(obj client.Object, key string) (string, bool)

GetLabel returns the value of the given label key on the object and whether it exists.

func GotAnnotationPredicate

func GotAnnotationPredicate(key, val string) predicate.Predicate

GotAnnotationPredicate reacts if the specified annotation was added to the resource. If val is empty, the value of the annotation doesn't matter, just that it was added. Otherwise, true is only returned if the annotation was added (or changed) with the specified value.

func GotLabelPredicate

func GotLabelPredicate(key, val string) predicate.Predicate

GotLabelPredicate reacts if the specified label was added to the resource. If val is empty, the value of the label doesn't matter, just that it was added. Otherwise, true is only returned if the label was added (or changed) with the specified value.

func HasAnnotation

func HasAnnotation(obj client.Object, key string) bool

HasAnnotation returns true if the given annotation key exists on the object.

func HasAnnotationPredicate

func HasAnnotationPredicate(key, val string) predicate.Predicate

HasAnnotationPredicate reacts if the resource has the specified annotation. If val is empty, the value of the annotation doesn't matter, only its existence. Otherwise, true is only returned if the annotation has the specified value. Note that GotAnnotationPredicate can be used to check if a resource just got a specific annotation.

func HasAnnotationWithValue

func HasAnnotationWithValue(obj client.Object, key, value string) bool

HasAnnotationWithValue returns true if the given annotation key exists on the object and has the given value.

func HasLabel

func HasLabel(obj client.Object, key string) bool

HasLabel returns true if the given label key exists on the object.

func HasLabelPredicate

func HasLabelPredicate(key, val string) predicate.Predicate

HasLabelPredicate reacts if the resource has the specified label. If val is empty, the value of the label doesn't matter, only its existence. Otherwise, true is only returned if the label has the specified value. Note that GotLabelPredicate can be used to check if a resource just got a specific label.

func HasLabelWithValue

func HasLabelWithValue(obj client.Object, key, value string) bool

HasLabelWithValue returns true if the given label key exists on the object and has the given value.

func HasOwnerReference

func HasOwnerReference(owned, owner client.Object, scheme *runtime.Scheme) (int, error)

HasOwnerReference returns the index of the owner reference if the 'owned' object has a owner reference pointing to the 'owner' object. If not, -1 is returned. Note that name and uid are only compared if set in the owner object. This means that the function will return a positive index for an owner object with empty name and uid if the owned object contains a owner reference which fits just apiversion and kind. The scheme argument may be nil if the owners GVK is populated.

func IsMetadataEntryAlreadyExistsError

func IsMetadataEntryAlreadyExistsError(err error) bool

func IsNil added in v0.5.0

func IsNil(i any) bool

IsNil checks if a given pointer is nil. Opposed to 'i == nil', this works for typed and untyped nil values.

func IsSameObject added in v0.6.0

func IsSameObject[T any](a, b T) bool

IsSameObject takes two interfaces of the same type and returns true if they both are pointers to the same underlying object.

func K8sNameHash added in v0.5.0

func K8sNameHash(ids ...string) string

K8sNameHash takes any number of string arguments and computes a hash out of it, which is then base32-encoded to be a valid DNS1123Subdomain k8s resource name The arguments are joined with '/' before being hashed.

func LabelSelectorPredicate added in v0.8.0

func LabelSelectorPredicate(sel labels.Selector) predicate.Predicate

LabelSelectorPredicate returns a predicate based on a label selector. Opposed to the similarly named function from the controller-runtime library, this one works on label.Selector instead of metav1.LabelSelector.

func LoadKubeconfig

func LoadKubeconfig(configPath string) (*rest.Config, error)

LoadKubeconfig loads a cluster configuration from the given path. If the path points to a single file, this file is expected to contain a kubeconfig which is then loaded. If the path points to a directory which contains a file named "kubeconfig", that file is used. If the path points to a directory which does not contain a "kubeconfig" file, there must be "host", "token", and "ca.crt" files present, which are used to configure cluster access based on an OIDC trust relationship. If the path is empty, the in-cluster config is returned.

func LostAnnotationPredicate

func LostAnnotationPredicate(key, val string) predicate.Predicate

LostAnnotationPredicate reacts if the specified annotation was removed from the resource. If val is empty, this predicate returns true if the annotation was removed completely, independent of which value it had. Otherwise, true is returned if the annotation had the specified value before and now lost it, either by being removed or by being set to a different value.

func LostLabelPredicate

func LostLabelPredicate(key, val string) predicate.Predicate

LostLabelPredicate reacts if the specified label was removed from the resource. If val is empty, this predicate returns true if the label was removed completely, independent of which value it had. Otherwise, true is returned if the label had the specified value before and now lost it, either by being removed or by being set to a different value.

func ObjectKey added in v0.6.0

func ObjectKey(name string, maybeNamespace ...string) client.ObjectKey

ObjectKey returns a client.ObjectKey for the given name and optionally namespace. The first argument is the name of the object. An optional second argument contains the namespace. All further arguments are ignored.

func SetField added in v0.6.0

func SetField(obj any, field string, value any)

SetField sets the field in the given object to the given value. Nested fields can be accessed by separating them with '.' (e.g. "Foo.Bar"). Panics if the object is nil or the field is not found. WARNING: This function will panic if the specified field is not settable (e.g. because obj is not a pointer).

func ToTypedPredicate added in v0.6.0

func ToTypedPredicate[Obj client.Object](p predicate.Predicate) predicate.TypedPredicate[Obj]

ToTypedPredicate wraps a predicate.Predicate (which is an alias for predicate.TypedPredicate[client.Object]) into a predicate.TypedPredicate[Obj].

Types

type DeletionTimestampChangedPredicate

type DeletionTimestampChangedPredicate struct {
	predicate.Funcs
}

DeletionTimestampChangedPredicate reacts to changes of the deletion timestamp.

func (DeletionTimestampChangedPredicate) Update

type MetadataEntryAlreadyExistsError

type MetadataEntryAlreadyExistsError struct {
	MType        metadataEntryType
	Key          string
	DesiredValue string
	ActualValue  string
}

func NewMetadataEntryAlreadyExistsError

func NewMetadataEntryAlreadyExistsError(mType metadataEntryType, key, desired, actual string) *MetadataEntryAlreadyExistsError

func (*MetadataEntryAlreadyExistsError) Error

type ModifyMetadataEntryMode

type ModifyMetadataEntryMode string
const (
	OVERWRITE ModifyMetadataEntryMode = "overwrite"
	DELETE    ModifyMetadataEntryMode = "delete"
)

type ReconcileResult added in v0.6.0

type ReconcileResult[Obj client.Object, ConType comparable] struct {
	// The old object, before it was modified.
	// Basically, if OldObject.Status differs from Object.Status, the status will be patched during status updating.
	// May be nil, in this case only the status metadata (observedGeneration etc.) is updated.
	// Changes to anything except the status are ignored.
	OldObject Obj
	// The current objectclient.Object	// If nil, the status update becomes a no-op.
	Object Obj
	// The result of the reconciliation.
	// Is simply passed through.
	Result ctrl.Result
	// The error that occurred during reconciliation, if any.
	ReconcileError errors.ReasonableError
	// The reason to display in the object's status.
	// If empty, it is taken from the error, if any.
	Reason string
	// The message to display in the object's status.
	// Potential error messages from the reconciliation error are appended.
	Message string
	// Conditions contains a list of conditions that should be updated on the object.
	// Also note that names of conditions are globally unique, so take care to avoid conflicts with other objects.
	// The lastTransition timestamp of the condition will be overwritten with the current time while updating.
	Conditions []conditions.Condition[ConType]
}

The result of a reconciliation. Obj is the k8s resource that is reconciled. ConType is the type of the "Status" field of the condition, usually a string alias. Simply use string if your object's status does not have conditions.

type StatusChangedPredicate

type StatusChangedPredicate struct {
	predicate.Funcs
}

StatusChangedPredicate returns true if the object's status changed. Getting the status is done via reflection and only works if the corresponding field is named 'Status'. If getting the status fails, this predicate always returns true.

func (StatusChangedPredicate) Update

type StatusField added in v0.6.0

type StatusField string
const (
	// This is kind of a meta field, it holds the name of the field that stores the status itself.
	STATUS_FIELD                     StatusField = "Status"
	STATUS_FIELD_OBSERVED_GENERATION StatusField = "ObservedGeneration"
	STATUS_FIELD_LAST_RECONCILE_TIME StatusField = "LastReconcileTime"
	STATUS_FIELD_CONDITIONS          StatusField = "Conditions"
	STATUS_FIELD_REASON              StatusField = "Reason"
	STATUS_FIELD_MESSAGE             StatusField = "Message"
	STATUS_FIELD_PHASE               StatusField = "Phase"
)

func AllStatusFields added in v0.6.0

func AllStatusFields() []StatusField

AllStatusFields returns all status fields that are used by the status updater. The meta field STATUS_FIELD is not included.

type StatusUpdaterBuilder added in v0.6.0

type StatusUpdaterBuilder[Obj client.Object, PhType ~string, ConType comparable] struct {
	// contains filtered or unexported fields
}

StatusUpdaterBuilder is a builder for creating a status updater. Do not use this directly, use NewStatusUpdaterBuilder() instead.

func NewStatusUpdaterBuilder added in v0.6.0

func NewStatusUpdaterBuilder[Obj client.Object, PhType ~string, ConType comparable]() *StatusUpdaterBuilder[Obj, PhType, ConType]

NewStatusUpdaterBuilder initializes a new StatusUpdaterBuilder.

func (*StatusUpdaterBuilder[Obj, PhType, ConType]) Build added in v0.6.0

func (b *StatusUpdaterBuilder[Obj, PhType, ConType]) Build() *statusUpdater[Obj, PhType, ConType]

Build returns the status updater.

func (*StatusUpdaterBuilder[Obj, PhType, ConType]) WithConditionUpdater added in v0.6.0

func (b *StatusUpdaterBuilder[Obj, PhType, ConType]) WithConditionUpdater(construct func() conditions.Condition[ConType], removeUntouchedConditions bool) *StatusUpdaterBuilder[Obj, PhType, ConType]

WithConditionUpdater must be called if the conditions should be updated, because this requires some additional information. Note that the conditions will only be updated if this method has been called (with a non-nil condition constructor) AND the conditions field has not been disabled.

func (*StatusUpdaterBuilder[Obj, PhType, ConType]) WithCustomUpdateFunc added in v0.6.0

func (b *StatusUpdaterBuilder[Obj, PhType, ConType]) WithCustomUpdateFunc(f func(obj Obj, rr ReconcileResult[Obj, ConType]) error) *StatusUpdaterBuilder[Obj, PhType, ConType]

WithCustomUpdateFunc allows to pass in a function that performs a custom update on the object. This function is called after all other status updates have been applied. It gets the original object passed in and can modify it directly. Note that only changes to the status field are sent to the cluster. Set this to nil to disable the custom update (it is nil by default).

func (*StatusUpdaterBuilder[Obj, PhType, ConType]) WithFieldOverride added in v0.6.0

func (b *StatusUpdaterBuilder[Obj, PhType, ConType]) WithFieldOverride(field StatusField, name string) *StatusUpdaterBuilder[Obj, PhType, ConType]

WithFieldOverride overwrites the name of the field. Use STATUS_FIELD to override the name of the field that holds the status itself. All other fields are expected to be within the status struct. Set to an empty string to disable the field. Doing this for STATUS_FIELD disables the complete status update. The default names are: - STATUS_FIELD: "Status" - STATUS_FIELD_OBSERVED_GENERATION: "ObservedGeneration" - STATUS_FIELD_LAST_RECONCILE_TIME: "LastReconcileTime" - STATUS_FIELD_CONDITIONS: "Conditions" - STATUS_FIELD_REASON: "Reason" - STATUS_FIELD_MESSAGE: "Message" - STATUS_FIELD_PHASE: "Phase"

func (*StatusUpdaterBuilder[Obj, PhType, ConType]) WithFieldOverrides added in v0.6.0

func (b *StatusUpdaterBuilder[Obj, PhType, ConType]) WithFieldOverrides(overrides map[StatusField]string) *StatusUpdaterBuilder[Obj, PhType, ConType]

WithFieldOverrides is a wrapper around WithFieldOverride that allows to apply multiple overrides at once.

func (*StatusUpdaterBuilder[Obj, PhType, ConType]) WithNestedStruct added in v0.6.0

func (b *StatusUpdaterBuilder[Obj, PhType, ConType]) WithNestedStruct(name string, appliesTo ...StatusField) *StatusUpdaterBuilder[Obj, PhType, ConType]

WithNestedStruct is a helper for easily updating the field names if some or all of the fields are wrapped in a nested struct within the status. Basically, the field names for all specified fields are prefixed with '<name>.', unless the field is empty (which disables the field). If appliesTo is empty, all fields are assumed to be nested (except for the status itself).

func (*StatusUpdaterBuilder[Obj, PhType, ConType]) WithPhaseUpdateFunc added in v0.6.0

func (b *StatusUpdaterBuilder[Obj, PhType, ConType]) WithPhaseUpdateFunc(f func(obj Obj, rr ReconcileResult[Obj, ConType]) (PhType, error)) *StatusUpdaterBuilder[Obj, PhType, ConType]

WithPhaseUpdateFunc sets the function that determines the phase of the object. It is strongly recommended to either disable the phase field or override this function, because the default will simply set the Phase to the zero value of PhType. The function is called with a deep copy of the object, after all other status updates have been applied (except for the custom update). If the returned error is nil, the object's phase is then set to the returned value. Setting this to nil causes the default phase update function to be used. To disable the phase update altogether, use WithoutField(STATUS_FIELD_PHASE).

func (*StatusUpdaterBuilder[Obj, PhType, ConType]) WithoutFields added in v0.6.0

func (b *StatusUpdaterBuilder[Obj, PhType, ConType]) WithoutFields(fields ...StatusField) *StatusUpdaterBuilder[Obj, PhType, ConType]

WithoutFields removes the specified fields from the status update. It basically calls WithFieldOverride(field, "") for each field. This can be used in combination with AllStatusFields() to disable all fields.

Jump to

Keyboard shortcuts

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