condition

package
v2.12.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

+kubebuilder:object:generate=true +kubebuilder:unservedversion

Index

Constants

View Source
const (
	ConditionTrue    = corev1.ConditionTrue
	ConditionFalse   = corev1.ConditionFalse
	ConditionUnknown = corev1.ConditionUnknown
)

Variables

This section is empty.

Functions

func ErrNoop

func ErrNoop() error

ErrNoop is an error that indicates no changes should be made to the status. Used to indicate the reconciler is not ready to change the condition status yet.

func ErrSkipReconciliation

func ErrSkipReconciliation() error

ErrSkipReconciliation is an error that indicates the reconciliation should be skipped. Used to indicate that the object is not ready for reconciliation or not owned by the controller. Only checked in PreHooks.

func ErrStatusUnknown

func ErrStatusUnknown(e error) error

ErrStatusUnknown wraps an error with a status unknown error. Used to indicate the status of a given condition is unknown during reconciliation.

func ForceRequeue

func ForceRequeue(ctx context.Context)

ForceRequeue forces an immediate requeue, regardless if status has changed.

func ForceStatusUpdate

func ForceStatusUpdate(ctx context.Context)

ForceStatusUpdate forces a full "Status" update, regardless if conditions have changed.

func HandleFinalizer

func HandleFinalizer[T ConditionedType](
	ctx context.Context,
	c client.Client,
	obj T,
	finalizer string,
	finalizeFunc FinalizerFunc[T]) (bool, error)

HandleFinalizer ensures that the finalizer is present on the object. If the finalizer is not present, it adds it and updates the object. If the finalizer is present, it calls the finalizeFunc and removes the finalizer. It returns true if the object was updated. Reconcilers can use this to requeue.

func IsStatusUnknown added in v2.0.6

func IsStatusUnknown(err error) bool

IsStatusUnknown reports whether err was produced by ErrStatusUnknown. Exposes the unexported sentinel for callers that need to distinguish "we're waiting on something" from a hard error.

func RequireOptimisticLock added in v2.10.3

func RequireOptimisticLock(ctx context.Context)

RequireOptimisticLock makes this pass's status patch conditional on the object not having changed since it was read, for a condition that records a decision a stale read would make differently. A conflict requeues the object instead.

Types

type AnyConditionedReconciler

type AnyConditionedReconciler interface {
	Reconcile(context.Context, reconcile.Request) (reconcile.Result, error)
}

AnyConditionedReconciler is an interface for reconcilers that reconcile objects with conditions. The ConditionedReconciler is a typed version of this interface. +kubebuilder:object:generate=false

type Condition

type Condition struct {
	// Type of this condition. At most one of each condition type may apply to
	// a resource at any point in time.
	Type ConditionType `json:"type"`

	// If set, this represents the .metadata.generation that the object condition was set based upon.
	// +optional
	ObservedGeneration int64 `json:"observedGeneration,omitempty"`

	// Status of this condition; is it currently True, False, or Unknown.
	Status corev1.ConditionStatus `json:"status"`

	// LastTransitionTime is the last time this condition transitioned from one
	// status to another.
	// +optional
	LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`

	// Last time we probed the condition.
	// +optional
	LastProbeTime metav1.Time `json:"lastProbeTime,omitempty"`

	// A Reason for this condition's last transition from one status to another.
	// +optional
	Reason ConditionReason `json:"reason,omitempty"`

	// A Message containing details about this condition's last transition from
	// one status to another, if any.
	// +optional
	Message string `json:"message,omitempty"`
}

A Condition that may apply to a resource.

func Available

func Available() Condition

Available returns a condition that indicates the resource is currently observed to be available for use.

func Creating

func Creating() Condition

Creating returns a condition that indicates the resource is currently being created.

func Deleting

func Deleting() Condition

Deleting returns a condition that indicates the resource is currently being deleted.

func ErrorCondition

func ErrorCondition(condType ConditionType, reason ConditionReason, err error) Condition

ErrorCondition generate error condition for conditionType and error

func ReadyCondition

func ReadyCondition(condType ConditionType) Condition

ReadyCondition generate ready condition for conditionType

func ReconcileError

func ReconcileError(err error) Condition

ReconcileError returns a condition indicating that Crossplane encountered an error while reconciling the resource. This could mean Crossplane was unable to update the resource to reflect its desired state, or that Crossplane was unable to determine the current actual state of the resource.

func ReconcilePending

func ReconcilePending() Condition

ReconcilePending returns a condition indicating the beginning of a reconciliation.

func ReconcileSuccess

func ReconcileSuccess() Condition

ReconcileSuccess returns a condition indicating that Crossplane successfully completed the most recent reconciliation of the resource.

func Unavailable

func Unavailable() Condition

Unavailable returns a condition that indicates the resource is not currently available for use. Unavailable should be set only when Crossplane expects the resource to be available but knows it is not, for example because its API reports it is unhealthy.

func UnknownCondition

func UnknownCondition(condType ConditionType, reason ConditionReason, message string) Condition

UnknownCondition generate error condition for conditionType and error

func (*Condition) DeepCopy

func (in *Condition) DeepCopy() *Condition

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Condition.

func (*Condition) DeepCopyInto

func (in *Condition) DeepCopyInto(out *Condition)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

func (Condition) Equal

func (c Condition) Equal(other Condition) bool

Equal returns true if the condition is identical to the supplied condition, ignoring the LastTransitionTime.

func (Condition) WithMessage

func (c Condition) WithMessage(msg string) Condition

WithMessage returns a condition by adding the provided message to existing condition.

type ConditionReason

type ConditionReason string

A ConditionReason represents the reason a resource is in a condition. nolint

const (
	ReasonAvailable   ConditionReason = "Available"
	ReasonUnavailable ConditionReason = "Unavailable"
	ReasonCreating    ConditionReason = "Creating"
	ReasonDeleting    ConditionReason = "Deleting"
)

Reasons a resource is or is not ready.

const (
	ReasonReconcilePending ConditionReason = "ReconcilePending"
	ReasonReconcileSuccess ConditionReason = "ReconcileSuccess"
	ReasonReconcileError   ConditionReason = "ReconcileError"
)

Reasons a resource is or is not synced.

type ConditionType

type ConditionType string

A ConditionType represents a condition a resource could be in. nolint

const (
	// TypeReady resources are believed to be ready to handle work.
	TypeReady ConditionType = "Ready"

	// TypeSynced resources are believed to be in sync with the
	// Kubernetes resources that manage their lifecycle.
	TypeSynced ConditionType = "Synced"
)

Managed Condition types.

type ConditionedReconciler

type ConditionedReconciler[T ConditionedType] struct {
	// contains filtered or unexported fields
}

ConditionedReconciler is a reconciler that reconciles objects with many conditions. It encodes the core kubernetes pattern of reconciling objects with conditions & finalizer. Each condition is a function that updates the object's status. Functions are executed in first-in/first-out order, and all functions are called for each reconciliation. The status is updated after all functions have been called. A finalizer name is required to use the finalizer, and the finalizer function is called before the object is deleted. A useful pattern is to have a "Ready" condition as the last condition, validating the status of all previous ones. +kubebuilder:object:generate=false

func NewConditionedReconciler

func NewConditionedReconciler[T ConditionedType](
	c client.Client,
	scheme *runtime.Scheme,
	obj T,
	interval time.Duration,
) *ConditionedReconciler[T]

NewConditionedReconciler creates a new Typed ConditionedReconciler. Objects will be reconciled at the given interval.

func (*ConditionedReconciler[T]) AddPostHook

func (r *ConditionedReconciler[T]) AddPostHook(fn func(context.Context, T) error)

AddPostHook adds a function to be called after the conditions are evaluated. Functions are executed in first-in/first-out order. Post-hooks are AFTER conditions, and AFTER the object status has been updated. To skip the remaining post-hooks, return `ErrSkipReconciliation`.

func (*ConditionedReconciler[T]) AddPreHook

func (r *ConditionedReconciler[T]) AddPreHook(fn func(context.Context, T) error)

AddPreHook adds a function to be called before the conditions are evaluated. Functions are executed in first-in/first-out order. A failure in a pre-hook will prevent the conditions from being evaluated. To completely skip reconciliation, return `ErrSkipReconciliation`.

func (*ConditionedReconciler[T]) BatchStatusUpdates added in v2.12.2

func (r *ConditionedReconciler[T]) BatchStatusUpdates()

BatchStatusUpdates lets later conditions read earlier changes in the same pass. Use it only when condition functions do not require those earlier changes to have been persisted first. Hard errors and explicit skips still stop a pass at the existing boundary.

func (*ConditionedReconciler[T]) OptimisticStatusWrites added in v2.12.2

func (r *ConditionedReconciler[T]) OptimisticStatusWrites()

OptimisticStatusWrites prevents a stale cached object from publishing status after another write advanced its resource version.

func (*ConditionedReconciler[T]) ReadyRequeueInterval added in v2.12.2

func (r *ConditionedReconciler[T]) ReadyRequeueInterval(interval time.Duration)

ReadyRequeueInterval overrides periodic reconciliation for healthy objects. Events and condition transitions still queue the object immediately. A nonpositive interval retains the existing periodic schedule.

func (*ConditionedReconciler[T]) Reconcile

Reconcile reconciles the object with the given request. implements the `AnyConditionedReconciler` and `reconcile.Reconciler` interfaces.

func (*ConditionedReconciler[T]) RetryStatusConflicts added in v2.12.3

func (r *ConditionedReconciler[T]) RetryStatusConflicts(reader client.Reader, rebase func(fresh, desired T) bool)

RetryStatusConflicts turns on OptimisticStatusWrites and, when the lock rejects a status patch, rereads the object through reader (uncached) instead of discarding the pass. If the object is the same one (same UID), rebase reconciles the pass's desired status with the fresh read, for fields another writer may own, and the pass's changes are patched again under the fresh resource version. A replaced or deleted object is never written.

rebase returns false when the fresh read invalidates what the pass concluded (a verdict computed from a field the other writer just changed). The pass is then dropped and requeued, as with plain OptimisticStatusWrites, and its post-hooks do not run.

Use it when another writer updates the same object often, so a conflict is expected rather than a sign that the pass's conclusions are stale. rebase may be nil when the pass owns every field it writes.

func (*ConditionedReconciler[T]) SeparateFinalizer added in v2.12.3

func (r *ConditionedReconciler[T]) SeparateFinalizer() reconcile.Reconciler

SeparateFinalizer keeps cleanup from occupying the workers that reconcile live objects. Reconcile still installs the finalizer, but leaves deleting objects to the returned reconciler. Register it with a separate controller that watches both deletion transitions and objects already deleting at startup. Like the other configuration methods, call this before starting either controller.

func (*ConditionedReconciler[T]) SetCondition

func (r *ConditionedReconciler[T]) SetCondition(ct ConditionType, fn func(context.Context, T) error)

SetCondition sets a reconcile function for the given condition type.

func (*ConditionedReconciler[T]) SetFinalizer

func (r *ConditionedReconciler[T]) SetFinalizer(finalizer string, fn func(context.Context, T) error)

SetFinalizer sets the finalizer name and function for the reconciler. Only one finalizer per controller.

func (*ConditionedReconciler[T]) TrackGeneration added in v2.12.0

func (r *ConditionedReconciler[T]) TrackGeneration()

TrackGeneration stamps every condition this reconciler sets with the object's generation. A pass stops at the first condition that changes, so the ones after it can describe an older spec; the stamp lets a reader tell.

A condition whose function returns ErrNoop is not set, so it keeps its old stamp for as long as it does, and a reader waiting for the current one waits that long. Conditions of a reconciler that tracks generations should not return ErrNoop.

type ConditionedStatus

type ConditionedStatus struct {
	// Conditions of the resource.
	// +optional
	Conditions []Condition `json:"conditions,omitempty"`
}

A ConditionedStatus reflects the observed status of a resource. Only one condition of each type may exist.

func NewConditionedStatus

func NewConditionedStatus(c ...Condition) *ConditionedStatus

NewConditionedStatus returns a stat with the supplied conditions set.

func (*ConditionedStatus) AllTrue

func (s *ConditionedStatus) AllTrue(condTypes ...ConditionType) bool

AllTrue returns true if all of the supplied condition types are true.

func (*ConditionedStatus) AnyUnknown

func (s *ConditionedStatus) AnyUnknown(condTypes ...ConditionType) bool

AnyUnknown returns true if any of the supplied condition types are unknown.

func (*ConditionedStatus) DeepCopy

func (in *ConditionedStatus) DeepCopy() *ConditionedStatus

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ConditionedStatus.

func (*ConditionedStatus) DeepCopyInto

func (in *ConditionedStatus) DeepCopyInto(out *ConditionedStatus)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

func (*ConditionedStatus) Equal

func (s *ConditionedStatus) Equal(other *ConditionedStatus) bool

Equal returns true if the status is identical to the supplied status, ignoring the LastTransitionTimes and order of statuses.

func (*ConditionedStatus) ErrAllTrue

func (s *ConditionedStatus) ErrAllTrue(condTypes ...ConditionType) error

ErrAllTrue returns an error containing all of the supplied condition types that are NOT true. All conditions must have the same ObservedGeneration.

func (*ConditionedStatus) GetCondition

func (s *ConditionedStatus) GetCondition(ct ConditionType) Condition

GetCondition returns the condition for the given ConditionType if exists, otherwise returns an unknown condition.

func (*ConditionedStatus) IsAvailable

func (s *ConditionedStatus) IsAvailable() bool

IsAvailable returns true if the resource is available.

func (*ConditionedStatus) SetConditions

func (s *ConditionedStatus) SetConditions(c ...Condition)

SetConditions sets the supplied conditions, replacing any existing conditions of the same type. This is a no-op if all supplied conditions are identical, ignoring the last transition time, to those already set.

type ConditionedType

type ConditionedType interface {
	client.Object
	ConditionedStatus() *ConditionedStatus
	InitializeConditionedStatus()
}

ConditionedType is an interface for objects that have a ConditionedStatus. See `condition.ConditionedStatus` for more information. +kubebuilder:object:generate=false

type FinalizerFunc

type FinalizerFunc[T ConditionedType] func(context.Context, T) error

FinalizerFunc is a function that finalizes an object. +kubebuilder:object:generate=false

type ReconcilerContext

type ReconcilerContext struct {
	ForceUpdate       bool
	ForceRequeue      bool
	OptimisticLock    bool
	ReconcileInterval time.Duration
}

func GetReconcilerContext

func GetReconcilerContext(ctx context.Context) *ReconcilerContext

GetReconcilerContext retrieves the ReconcilerContext from the context.

func (*ReconcilerContext) DeepCopy

func (in *ReconcilerContext) DeepCopy() *ReconcilerContext

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReconcilerContext.

func (*ReconcilerContext) DeepCopyInto

func (in *ReconcilerContext) DeepCopyInto(out *ReconcilerContext)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

Jump to

Keyboard shortcuts

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