conditionutils

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 7 Imported by: 3

Documentation

Overview

Package conditionutils simplifies condition handling with any structurally compatible condition (comparable to a sort of duck-typing) via go reflection.

Index

Constants

View Source
const (
	// DefaultTypeField is the default name for a condition's type field.
	DefaultTypeField = "Type"
	// DefaultStatusField is the default name for a condition's status field.
	DefaultStatusField = "Status"
	// DefaultLastUpdateTimeField field is the default name for a condition's last update time field.
	DefaultLastUpdateTimeField = "LastUpdateTime"
	// DefaultLastTransitionTimeField field is the default name for a condition's last transition time field.
	DefaultLastTransitionTimeField = "LastTransitionTime"
	// DefaultReasonField field is the default name for a condition's reason field.
	DefaultReasonField = "Reason"
	// DefaultMessageField field is the default name for a condition's message field.
	DefaultMessageField = "Message"
	// DefaultObservedGenerationField field is the default name for a condition's observed generation field.
	DefaultObservedGenerationField = "ObservedGeneration"
)

Variables

View Source
var (
	// DefaultTransition is the default Transition.
	DefaultTransition Transition = &FieldsTransition{IncludeStatus: true}

	// DefaultAccessor is an Accessor initialized with the default fields.
	// See NewAccessor for more.
	DefaultAccessor = NewAccessor(AccessorOptions{})

	// Update updates the condition with the given options.
	// See Accessor.Update for more.
	Update = DefaultAccessor.Update

	// MustUpdate updates the condition with the given options.
	// See Accessor.MustUpdate for more.
	MustUpdate = DefaultAccessor.MustUpdate

	// UpdateSlice updates the slice with the given options.
	// See Accessor.UpdateSlice for more.
	UpdateSlice = DefaultAccessor.UpdateSlice

	// MustUpdateSlice updates the slice with the given options.
	// See Accessor.MustUpdateSlice for more.
	MustUpdateSlice = DefaultAccessor.MustUpdateSlice

	// FindSliceIndex finds the index of the target condition in the given slice.
	// See Accessor.FindSliceIndex for more.
	FindSliceIndex = DefaultAccessor.FindSliceIndex

	// MustFindSliceIndex finds the index of the target condition in the given slice.
	// See Accessor.MustFindSliceIndex for more.
	MustFindSliceIndex = DefaultAccessor.MustFindSliceIndex

	// FindSlice finds the target condition in the given slice.
	// See Accessor.FindSlice for more.
	FindSlice = DefaultAccessor.FindSlice

	// MustFindSlice finds the target condition in the given slice.
	// See Accessor.MustFindSlice for more.
	MustFindSlice = DefaultAccessor.MustFindSlice

	// FindSliceStatus finds the condition status in the given slice.
	// See Accessor.FindSliceStatus for more.
	FindSliceStatus = DefaultAccessor.FindSliceStatus

	// MustFindSliceStatus finds the condition status in the given slice.
	// See Accessor.MustFindSliceStatus for more.
	MustFindSliceStatus = DefaultAccessor.MustFindSliceStatus
)

Functions

This section is empty.

Types

type Accessor

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

Accessor allows getting and setting fields from conditions as well as to check on their presence. In addition, it allows complex manipulations on individual conditions and condition slices.

func NewAccessor

func NewAccessor(opts AccessorOptions) *Accessor

NewAccessor creates a new Accessor with the given AccessorOptions.

func (*Accessor) FindSlice

func (a *Accessor) FindSlice(condSlice interface{}, typ string, intoPtr interface{}) (ok bool, err error)

FindSlice finds the condition with the given type from the given slice and updates the target value with it.

If the target type is not found, false is returned and the target value is not updated. FindSlice errors if condSlice is not a slice, intoPtr is not a pointer to a struct and if intoPtr's target value is not settable with an element of condSlice.

func (*Accessor) FindSliceIndex

func (a *Accessor) FindSliceIndex(condSlice interface{}, typ string) (int, error)

FindSliceIndex finds the index of the condition with the given type.

If the target type is not found, -1 is returned. FindSliceIndex errors if condSlice is not a slice of structs.

func (*Accessor) FindSliceStatus

func (a *Accessor) FindSliceStatus(condSlice interface{}, typ string) (corev1.ConditionStatus, error)

FindSliceStatus finds the status of the condition with the given type. If the condition cannot be found, corev1.ConditionUnknown is returned.

FindSliceStatus errors if the given condSlice is not a slice of structs or if any of the conditions does not support access.

func (*Accessor) HasLastTransitionTime

func (a *Accessor) HasLastTransitionTime(cond interface{}) (bool, error)

HasLastTransitionTime checks if the given condition has a 'LastTransitionTime' field.

It errors if the given value is not a struct.

func (*Accessor) HasLastUpdateTime

func (a *Accessor) HasLastUpdateTime(cond interface{}) (bool, error)

HasLastUpdateTime checks if the given condition has a 'LastUpdateTime' field.

It errors if the given value is not a struct.

func (*Accessor) HasObservedGeneration

func (a *Accessor) HasObservedGeneration(cond interface{}) (bool, error)

HasObservedGeneration checks if the given condition has a observed generation field.

It errors if the given value is not a struct.

func (*Accessor) LastTransitionTime

func (a *Accessor) LastTransitionTime(cond interface{}) (metav1.Time, error)

LastTransitionTime extracts the last transition time of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) LastUpdateTime

func (a *Accessor) LastUpdateTime(cond interface{}) (metav1.Time, error)

LastUpdateTime extracts the last update time of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) Message

func (a *Accessor) Message(cond interface{}) (string, error)

Message gets the message of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the input format.

func (*Accessor) MustFindSlice

func (a *Accessor) MustFindSlice(condSlice interface{}, typ string, intoPtr interface{}) bool

MustFindSlice finds the condition with the given type from the given slice and updates the target value with it.

If the target type is not found, false is returned and the target value is not updated. FindSlice panics if condSlice is not a slice, intoPtr is not a pointer to a struct and if intoPtr's target value is not settable with an element of condSlice.

func (*Accessor) MustFindSliceIndex

func (a *Accessor) MustFindSliceIndex(condSlice interface{}, typ string) int

MustFindSliceIndex finds the index of the condition with the given type.

If the target type is not found, -1 is returned. MustFindSliceIndex panics if condSlice is not a slice of structs.

func (*Accessor) MustFindSliceStatus

func (a *Accessor) MustFindSliceStatus(condSlice interface{}, typ string) corev1.ConditionStatus

MustFindSliceStatus finds the status of the condition with the given type. If the condition cannot be found, corev1.ConditionUnknown is returned.

MustFindSliceStatus errors if the given condSlice is not a slice of structs or if any of the conditions does not support access.

func (*Accessor) MustHasLastTransitionTime

func (a *Accessor) MustHasLastTransitionTime(cond interface{}) bool

MustHasLastTransitionTime checks if the given condition has a 'LastTransitionTime' field.

It panics if the given value is not a struct.

func (*Accessor) MustHasLastUpdateTime

func (a *Accessor) MustHasLastUpdateTime(cond interface{}) bool

MustHasLastUpdateTime checks if the given condition has a 'LastUpdateTime' field.

It panics if the given value is not a struct.

func (*Accessor) MustHasObservedGeneration

func (a *Accessor) MustHasObservedGeneration(cond interface{}) bool

MustHasObservedGeneration checks if the given condition has a observed generation field.

It panics if the given value is not a struct.

func (*Accessor) MustLastTransitionTime

func (a *Accessor) MustLastTransitionTime(cond interface{}) metav1.Time

MustLastTransitionTime extracts the last transition time of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) MustLastUpdateTime

func (a *Accessor) MustLastUpdateTime(cond interface{}) metav1.Time

MustLastUpdateTime extracts the last update time of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) MustMessage

func (a *Accessor) MustMessage(cond interface{}) string

MustMessage gets the message of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the input format.

func (*Accessor) MustObservedGeneration

func (a *Accessor) MustObservedGeneration(cond interface{}) int64

MustObservedGeneration gets the observed generation of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the input format.

func (*Accessor) MustReason

func (a *Accessor) MustReason(cond interface{}) string

MustReason extracts the reason of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) MustSetLastTransitionTime

func (a *Accessor) MustSetLastTransitionTime(condPtr interface{}, lastTransitionTime metav1.Time)

MustSetLastTransitionTime sets the last transition time of the given condition.

It panics if the given value is not a pointer to a struct or does not have a field that can be converted to the output format.

func (*Accessor) MustSetLastTransitionTimeIfExists

func (a *Accessor) MustSetLastTransitionTimeIfExists(condPtr interface{}, lastTransitionTime metav1.Time)

MustSetLastTransitionTimeIfExists sets the last transition time of the given condition.

It panics if the given value is not a pointer to a struct or the field value cannot be converted to the given format.

func (*Accessor) MustSetLastUpdateTime

func (a *Accessor) MustSetLastUpdateTime(condPtr interface{}, lastUpdateTime metav1.Time)

MustSetLastUpdateTime sets the last update time of the given condition.

It errors if the given value is not a pointer to a struct or does not have a field that can be converted to the given format.

func (*Accessor) MustSetLastUpdateTimeIfExists

func (a *Accessor) MustSetLastUpdateTimeIfExists(condPtr interface{}, lastUpdateTime metav1.Time)

MustSetLastUpdateTimeIfExists sets the last update time of the given condition if the field exists.

It panics if the given value is not a pointer to a struct or the field value cannot be converted to the given format.

func (*Accessor) MustSetMessage

func (a *Accessor) MustSetMessage(condPtr interface{}, message string)

MustSetMessage sets the message of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the given format.

func (*Accessor) MustSetObservedGeneration

func (a *Accessor) MustSetObservedGeneration(condPtr interface{}, gen int64)

MustSetObservedGeneration sets the observed generation of the given condition.

It panics if the given value is not a pointer to a struct or does not have a field that can be converted to the given format.

func (*Accessor) MustSetReason

func (a *Accessor) MustSetReason(condPtr interface{}, reason string)

MustSetReason sets the reason of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the given format.

func (*Accessor) MustSetStatus

func (a *Accessor) MustSetStatus(condPtr interface{}, status corev1.ConditionStatus)

MustSetStatus sets the status of the given condition.

It panics if the given value is not a pointer to a struct or does not have a field that can be converted to the given format.

func (*Accessor) MustSetType

func (a *Accessor) MustSetType(condPtr interface{}, typ string)

MustSetType sets the type of the given condition to the given value.

It panics if the given value is not a pointer to a struct or does not have a field that can be converted to the given format.

func (*Accessor) MustStatus

func (a *Accessor) MustStatus(cond interface{}) corev1.ConditionStatus

MustStatus extracts the status of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) MustType

func (a *Accessor) MustType(cond interface{}) string

MustType extracts the type of the given condition.

It panics if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) MustUpdate

func (a *Accessor) MustUpdate(condPtr interface{}, opts ...UpdateOption)

MustUpdate updates the condition with the given options, setting transition- and update time accordingly.

MustUpdate panics if the given condPtr is not a pointer to a struct supporting the required condition fields.

func (*Accessor) MustUpdateSlice

func (a *Accessor) MustUpdateSlice(condSlicePtr interface{}, typ string, opts ...UpdateOption)

MustUpdateSlice finds and updates the condition with the given target type.

MustUpdateSlice panics if condSlicePtr is not a pointer to a slice of structs that can be accessed with this Accessor. If no condition with the given type can be found, a new one is appended with the given type and updates applied. The last update time and last transition time of the new / existing condition is correctly updated: For new conditions, it's always set to the current time while for existing conditions, it's checked whether the status changed and then updated.

func (*Accessor) ObservedGeneration

func (a *Accessor) ObservedGeneration(cond interface{}) (int64, error)

ObservedGeneration gets the observed generation of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the input format.

func (*Accessor) Reason

func (a *Accessor) Reason(cond interface{}) (string, error)

Reason extracts the reason of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) SetLastTransitionTime

func (a *Accessor) SetLastTransitionTime(condPtr interface{}, lastTransitionTime metav1.Time) error

SetLastTransitionTime sets the last transition time of the given condition if the field exists.

It errors if the given value is not a pointer to a struct or does not have a field that can be converted to the output format.

func (*Accessor) SetLastTransitionTimeIfExists

func (a *Accessor) SetLastTransitionTimeIfExists(condPtr interface{}, lastTransitionTime metav1.Time) error

SetLastTransitionTimeIfExists sets the last transition time of the given condition.

It errors if the given value is not a pointer to a struct or the field value cannot be converted to the given format.

func (*Accessor) SetLastUpdateTime

func (a *Accessor) SetLastUpdateTime(condPtr interface{}, lastUpdateTime metav1.Time) error

SetLastUpdateTime sets the last update time of the given condition.

It errors if the given value is not a pointer to a struct or does not have a field that can be converted to the given format.

func (*Accessor) SetLastUpdateTimeIfExists

func (a *Accessor) SetLastUpdateTimeIfExists(condPtr interface{}, lastUpdateTime metav1.Time) error

SetLastUpdateTimeIfExists sets the last update time of the given condition if the field exists.

It errors if the given value is not a pointer to a struct or the field value cannot be converted to the given format.

func (*Accessor) SetMessage

func (a *Accessor) SetMessage(condPtr interface{}, message string) error

SetMessage sets the message of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the given format.

func (*Accessor) SetObservedGeneration

func (a *Accessor) SetObservedGeneration(condPtr interface{}, gen int64) error

SetObservedGeneration sets the observed generation of the given condition.

It errors if the given value is not a pointer to a struct or does not have a field that can be converted to the given format.

func (*Accessor) SetReason

func (a *Accessor) SetReason(condPtr interface{}, reason string) error

SetReason sets the reason of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the given format.

func (*Accessor) SetStatus

func (a *Accessor) SetStatus(condPtr interface{}, status corev1.ConditionStatus) error

SetStatus sets the status of the given condition.

It errors if the given value is not a pointer to a struct or does not have a field that can be converted to the given format.

func (*Accessor) SetType

func (a *Accessor) SetType(condPtr interface{}, typ string) error

SetType sets the type of the given condition to the given value.

It errors if the given value is not a pointer to a struct or does not have a field that can be converted to the given format.

func (*Accessor) Status

func (a *Accessor) Status(cond interface{}) (corev1.ConditionStatus, error)

Status extracts the status of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) Type

func (a *Accessor) Type(cond interface{}) (string, error)

Type extracts the type of the given condition.

It errors if the given value is not a struct or does not have a field that can be converted to the output format.

func (*Accessor) Update

func (a *Accessor) Update(condPtr interface{}, opts ...UpdateOption) error

Update updates the condition with the given options, setting transition- and update time accordingly.

Update errors if the given condPtr is not a pointer to a struct supporting the required condition fields.

func (*Accessor) UpdateSlice

func (a *Accessor) UpdateSlice(condSlicePtr interface{}, typ string, opts ...UpdateOption) error

UpdateSlice finds and updates the condition with the given target type.

UpdateSlice errors if condSlicePtr is not a pointer to a slice of structs that can be accessed with this Accessor. If no condition with the given type can be found, a new one is appended with the given type and updates applied. The last update time and last transition time of the new / existing condition is correctly updated: For new conditions, it's always set to the current time while for existing conditions, it's checked whether the status changed and then updated.

type AccessorOptions

type AccessorOptions struct {
	TypeField               string
	StatusField             string
	LastUpdateTimeField     string
	LastTransitionTimeField string
	ReasonField             string
	MessageField            string
	ObservedGenerationField string

	DisableTimestampUpdates bool
	Transition              Transition
	Clock                   clock.Clock
}

AccessorOptions are options to create an Accessor.

If left blank, defaults are being used via AccessorOptions.SetDefaults.

func (*AccessorOptions) SetDefaults

func (o *AccessorOptions) SetDefaults()

SetDefaults sets default values for AccessorOptions.

type FieldsTransition

type FieldsTransition struct {
	// IncludeStatus includes Accessor.Status for the transition calculation. This is the most frequent choice.
	IncludeStatus bool
	// IncludeReason includes Accessor.Reason for the transition calculation. While more seldom, there are use cases
	// for including the reason in the transition calculation.
	IncludeReason bool
	// IncludeMessage includes Accessor.Message for the transition calculation. Used rarely, usually causes
	// a lot of transitions.
	IncludeMessage bool
}

FieldsTransition computes whether a condition transitioned using the `Include`-Fields.

func (*FieldsTransition) Checkpoint

func (f *FieldsTransition) Checkpoint(acc *Accessor, cond interface{}) (TransitionCheckpoint, error)

Checkpoint implements Transition.

type Transition

type Transition interface {
	// Checkpoint creates a TransactionCheckpoint using the current values of cond extracted with Accessor.
	Checkpoint(acc *Accessor, cond interface{}) (TransitionCheckpoint, error)
}

Transition can determine whether a condition transitioned (i.e. LastTransitionTime needs to be updated) or not.

type TransitionCheckpoint

type TransitionCheckpoint interface {
	// Transitioned reports whether the condition transitioned.
	Transitioned(acc *Accessor, cond interface{}) (bool, error)
}

TransitionCheckpoint can determine whether a condition transitioned using pre-gathered values of a condition.

type UpdateFromCondition

type UpdateFromCondition struct {
	// Accessor is the Accessor to access the Condition.
	// If unset, the Accessor from ApplyUpdate is used.
	Accessor *Accessor
	// Condition is a condition struct. Must not be nil.
	Condition interface{}
}

UpdateFromCondition updates a condition from a source Condition.

If Accessor is set, the ApplyUpdate function will use the Accessor for reading the properties for the source Condition over the one supplied. Otherwise, the Accessor supplied by ApplyUpdate will be used. All properties of the source condition will be transferred to the target condition with the exception of Type, LastTransitionTime and LastUpdateTime.

func (UpdateFromCondition) ApplyUpdate

func (u UpdateFromCondition) ApplyUpdate(a *Accessor, condPtr interface{}) error

type UpdateMessage

type UpdateMessage string

UpdateMessage implements UpdateOption to set the message.

func (UpdateMessage) ApplyUpdate

func (u UpdateMessage) ApplyUpdate(a *Accessor, condPtr interface{}) error

ApplyUpdate implements UpdateOption.

type UpdateObservedGeneration

type UpdateObservedGeneration int64

UpdateObservedGeneration implements UpdateOption to set the observed generation.

func UpdateObserved

func UpdateObserved(obj metav1.Object) UpdateObservedGeneration

UpdateObserved is a shorthand for updating the observed generation from a metav1.Object's generation.

func (UpdateObservedGeneration) ApplyUpdate

func (u UpdateObservedGeneration) ApplyUpdate(a *Accessor, condPtr interface{}) error

ApplyUpdate implements UpdateOption.

type UpdateOption

type UpdateOption interface {
	// ApplyUpdate applies the update, given a condition pointer.
	ApplyUpdate(a *Accessor, condPtr interface{}) error
}

UpdateOption is an option given to Accessor.UpdateSlice that updates an individual condition.

func UpdateTimestampsWith

func UpdateTimestampsWith(updates ...UpdateOption) UpdateOption

UpdateTimestampsWith updates timestamps with the DefaultTransition and clock.RealClock. See UpdateTimestamps for more information.

type UpdateReason

type UpdateReason string

UpdateReason implements UpdateOption to set the reason.

func (UpdateReason) ApplyUpdate

func (u UpdateReason) ApplyUpdate(a *Accessor, condPtr interface{}) error

ApplyUpdate implements UpdateOption.

type UpdateStatus

type UpdateStatus corev1.ConditionStatus

UpdateStatus implements UpdateOption to set a corev1.ConditionStatus.

func (UpdateStatus) ApplyUpdate

func (u UpdateStatus) ApplyUpdate(a *Accessor, condPtr interface{}) error

ApplyUpdate implements UpdateOption.

type UpdateTimestamps

type UpdateTimestamps struct {
	// Transition is the Transition to check whether a condition transitioned. Required.
	Transition Transition
	// Updates are all updates to run.
	Updates []UpdateOption
	// Clock is the clock to yield the current time. If unset, clock.RealClock is used.
	Clock clock.Clock
}

UpdateTimestamps manages the LastUpdateTime and LastTransitionTime field by creating a checkpoint with Transition, running all Updates and then checking if the TransitionCheckpoint reports transitioned. If so, the LastTransitionTimeField and the LastUpdateTimeField will be set to the current time using Clock (if Clock is unset, it uses clock.RealClock). Otherwise, only LastUpdateTimeField is updated..

func (UpdateTimestamps) ApplyUpdate

func (u UpdateTimestamps) ApplyUpdate(a *Accessor, condPtr interface{}) error

ApplyUpdate implements UpdateOption.

Jump to

Keyboard shortcuts

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