...

Package conditions

import "edge-infra.dev/pkg/k8s/runtime/conditions"
Overview
Index

Overview ▾

Package conditions implements generic functionality for working with kstatus compliant K8s resource conditions.

This code was originally forked from https://github.com/fluxcd/pkg to avoid dependency tangling and so that we can customize it to suit our specific needs.

Index ▾

func Delete(to Setter, t string)
func FalseCondition(t, reason, messageFormat string, messageArgs ...interface{}) *metav1.Condition
func Get(from Getter, t string) *metav1.Condition
func GetConditions(from Getter) []metav1.Condition
func GetLastTransitionTime(from Getter, t string) *metav1.Time
func GetMessage(from Getter, t string) string
func GetObservedGeneration(from Getter, t string) int64
func GetReason(from Getter, t string) string
func Has(from Getter, t string) bool
func HasAny(from Getter, t []string) bool
func HasReason(from Getter, t, r string) bool
func HaveSameStateOf(expected *metav1.Condition) types.GomegaMatcher
func IsFalse(from Getter, t string) bool
func IsReady(from Getter) bool
func IsReconciling(from Getter) bool
func IsStalled(from Getter) bool
func IsTrue(from Getter, t string) bool
func IsUnknown(from Getter, t string) bool
func MarkFalse(to Setter, t, reason, messageFormat string, messageArgs ...interface{})
func MarkReconciling(to Setter, reason, messageFormat string, messageArgs ...interface{})
func MarkStalled(to Setter, reason, messageFormat string, messageArgs ...interface{})
func MarkTrue(to Setter, t, reason, messageFormat string, messageArgs ...interface{})
func MarkUnknown(to Setter, t, reason, messageFormat string, messageArgs ...interface{})
func MatchCondition(expected metav1.Condition) types.GomegaMatcher
func MatchConditions(expected []metav1.Condition) types.GomegaMatcher
func Progressing(obj Setter, reason, messageFormat string, messageArgs ...interface{})
func Set(to Setter, condition *metav1.Condition)
func SetAggregate(to Setter, targetCondition string, from []Getter, options ...MergeOption)
func SetMirror(to Setter, targetCondition string, from Getter, options ...MirrorOptions)
func SetSummary(to Setter, targetCondition string, options ...MergeOption)
func TrueCondition(t, reason, messageFormat string, messageArgs ...interface{}) *metav1.Condition
func UniqueConditions(conditions []metav1.Condition) []metav1.Condition
func UnknownCondition(t, reason, messageFormat string, messageArgs ...interface{}) *metav1.Condition
type ApplyOption
    func WithForceOverwrite(v bool) ApplyOption
    func WithOwnedConditions(t ...string) ApplyOption
type ConditionMatcher
    func (matcher *ConditionMatcher) FailureMessage(actual interface{}) (message string)
    func (matcher *ConditionMatcher) Match(actual interface{}) (success bool, err error)
    func (matcher *ConditionMatcher) NegatedFailureMessage(actual interface{}) (message string)
type Getter
type MergeOption
    func WithConditions(t ...string) MergeOption
    func WithCounter() MergeOption
    func WithCounterIfOnly(t ...string) MergeOption
    func WithLatestGeneration() MergeOption
    func WithNegativePolarityConditions(t ...string) MergeOption
    func WithSourceRef() MergeOption
    func WithSourceRefIf(t ...string) MergeOption
    func WithStepCounter() MergeOption
    func WithStepCounterIf(value bool) MergeOption
    func WithStepCounterIfOnly(t ...string) MergeOption
type MirrorOptions
    func WithFallbackValue(fallbackValue bool, reason string, message string) MirrorOptions
type Patch
    func NewPatch(before Getter, after Getter) Patch
    func (p Patch) Apply(latest Setter, options ...ApplyOption) error
    func (p Patch) IsZero() bool
type PatchOperation
type PatchOperationType
type Setter

Package files

doc.go getter.go matcher.go merge.go patch.go setter.go status.go strategies.go

func Delete

func Delete(to Setter, t string)

Delete deletes the condition with the given type.

func FalseCondition

func FalseCondition(t, reason, messageFormat string, messageArgs ...interface{}) *metav1.Condition

FalseCondition returns a condition with Status=False and the given type, reason and message.

func Get

func Get(from Getter, t string) *metav1.Condition

Get returns the condition with the given type, if the condition does not exist, it returns nil.

func GetConditions

func GetConditions(from Getter) []metav1.Condition

func GetLastTransitionTime

func GetLastTransitionTime(from Getter, t string) *metav1.Time

GetLastTransitionTime returns the LastTransitionType or nil if the condition does not exist (is nil).

func GetMessage

func GetMessage(from Getter, t string) string

GetMessage returns a nil safe string of Message for the condition with the given type.

func GetObservedGeneration

func GetObservedGeneration(from Getter, t string) int64

GetObservedGeneration returns a nil safe int64 of ObservedGeneration for the condition with the given type.

func GetReason

func GetReason(from Getter, t string) string

GetReason returns a nil safe string of Reason for the condition with the given type.

func Has

func Has(from Getter, t string) bool

Has returns true if a condition with the given type exists.

func HasAny

func HasAny(from Getter, t []string) bool

HasAny returns true if a condition with any of the given types exist.

func HasReason

func HasReason(from Getter, t, r string) bool

HasReason returns true if the given condition type t exists and has Reason r

func HaveSameStateOf

func HaveSameStateOf(expected *metav1.Condition) types.GomegaMatcher

HaveSameStateOf returns a custom matcher to check equality of a metav1.Condition, the condition message is checked for a subset string match.

func IsFalse

func IsFalse(from Getter, t string) bool

IsFalse is true if the condition with the given type is False, otherwise it is false if the condition is not False or if the condition does not exist (is nil).

func IsReady

func IsReady(from Getter) bool

IsReady is true if IsStalled and IsReconciling are False, and status.ReadyCondition is True, otherwise it is false if the condition is not True or if it does not exist (is nil).

func IsReconciling

func IsReconciling(from Getter) bool

IsReconciling is true if status.ReconcilingCondition is True and status.StalledCondition is False or does not exist, otherwise it is false.

func IsStalled

func IsStalled(from Getter) bool

IsStalled is true if status.StalledCondition is True and status.ReconcilingCondition is False or does not exist, otherwise it is false.

func IsTrue

func IsTrue(from Getter, t string) bool

IsTrue is true if the condition with the given type is True, otherwise it is false if the condition is not True or if the condition does not exist (is nil).

func IsUnknown

func IsUnknown(from Getter, t string) bool

IsUnknown is true if the condition with the given type is Unknown or if the condition does not exist (is nil).

func MarkFalse

func MarkFalse(to Setter, t, reason, messageFormat string, messageArgs ...interface{})

MarkFalse sets Status=False for the condition with the given type, reason and message.

func MarkReconciling

func MarkReconciling(to Setter, reason, messageFormat string, messageArgs ...interface{})

MarkReconciling sets status.ReconcilingCondition=True with the given reason and message, and deletes the status.StalledCondition. This is normally called at the beginning of a reconcile run for an object.

For more information about the condition types, see the kstatus spec: https://github.com/kubernetes-sigs/cli-utils/blob/e351b2bc43cec2107ba1d874c3dec54fd0956c59/pkg/kstatus/README.md#conditions

func MarkStalled

func MarkStalled(to Setter, reason, messageFormat string, messageArgs ...interface{})

MarkStalled sets status.StalledCondition=True with the given reason and message, and deletes the status.ReconcilingCondition. This is normally deferred and conditionally called at the end of a reconcile run for an object. A common approach is to mark the object stalled if the object is not requeued as a reconcile result.

For more information about the condition types, see the kstatus spec: https://github.com/kubernetes-sigs/cli-utils/blob/e351b2bc43cec2107ba1d874c3dec54fd0956c59/pkg/kstatus/README.md#conditions

func MarkTrue

func MarkTrue(to Setter, t, reason, messageFormat string, messageArgs ...interface{})

MarkTrue sets Status=True for the condition with the given type, reason and message.

func MarkUnknown

func MarkUnknown(to Setter, t, reason, messageFormat string, messageArgs ...interface{})

MarkUnknown sets Status=Unknown for the condition with the given type, reason and message.

func MatchCondition

func MatchCondition(expected metav1.Condition) types.GomegaMatcher

MatchCondition returns a custom matcher to check equality of metav1.Condition.

func MatchConditions

func MatchConditions(expected []metav1.Condition) types.GomegaMatcher

MatchConditions returns a custom matcher to check equality of a metav1.Condition slice, the condition messages are checked for a subset string match.

func Progressing

func Progressing(obj Setter, reason, messageFormat string, messageArgs ...interface{})

Progressing helps report the progressive status of an object based on the given status value. It always sets Reconciling=True with the given status values.

Ready condition is set only when:

- there's no existing Ready condition in the status - Ready=Unknown

func Set

func Set(to Setter, condition *metav1.Condition)

Set sets the given condition.

NOTE: If a condition already exists, the LastTransitionTime is updated only if a change is detected in any of the following fields: Status, Reason, and Message. The ObservedGeneration is always updated.

func SetAggregate

func SetAggregate(to Setter, targetCondition string, from []Getter, options ...MergeOption)

SetAggregate creates a new condition with the aggregation of all the conditions from a list of dependency objects, or a subset using WithConditions; if none of the source objects have a condition within the scope of the merge operation, no target condition is generated.

func SetMirror

func SetMirror(to Setter, targetCondition string, from Getter, options ...MirrorOptions)

SetMirror creates a new condition by mirroring the the Ready condition from a dependent object; if the Ready condition does not exists in the source object, no target conditions is generated.

func SetSummary

func SetSummary(to Setter, targetCondition string, options ...MergeOption)

SetSummary creates a new summary condition with the summary of all the conditions existing on an object. If the object does not have other conditions, no summary condition is generated.

func TrueCondition

func TrueCondition(t, reason, messageFormat string, messageArgs ...interface{}) *metav1.Condition

TrueCondition returns a condition with Status=True and the given type, reason and message.

func UniqueConditions

func UniqueConditions(conditions []metav1.Condition) []metav1.Condition

func UnknownCondition

func UnknownCondition(t, reason, messageFormat string, messageArgs ...interface{}) *metav1.Condition

UnknownCondition returns a condition with Status=Unknown and the given type, reason and message.

type ApplyOption

ApplyOption defines an option for applying a condition patch.

type ApplyOption func(*applyOptions)

func WithForceOverwrite

func WithForceOverwrite(v bool) ApplyOption

WithForceOverwrite instructs the patch helper to always use the value provided by the controller in case of conflicts for the owned conditions.

func WithOwnedConditions

func WithOwnedConditions(t ...string) ApplyOption

WithOwnedConditions allows to define condition types owned by the controller. In case of conflicts for the owned conditions, the patch helper will always use the value provided by the controller.

type ConditionMatcher

type ConditionMatcher struct {
    Expected *metav1.Condition
}

func (*ConditionMatcher) FailureMessage

func (matcher *ConditionMatcher) FailureMessage(actual interface{}) (message string)

func (*ConditionMatcher) Match

func (matcher *ConditionMatcher) Match(actual interface{}) (success bool, err error)

func (*ConditionMatcher) NegatedFailureMessage

func (matcher *ConditionMatcher) NegatedFailureMessage(actual interface{}) (message string)

type Getter

Getter interface defines methods that a Kubernetes resource object should implement in order to use the conditions package for getting conditions.

type Getter interface {
    client.Object
    GetConditions() []metav1.Condition
}

type MergeOption

MergeOption defines an option for computing a summary of conditions.

type MergeOption func(*mergeOptions)

func WithConditions

func WithConditions(t ...string) MergeOption

WithConditions instructs merge about the condition types to consider when doing a merge operation; if this option is not specified, all the conditions (except Ready, Stalled, and Reconciling) will be considered. This is required so we can provide some guarantees about the semantic of the target condition without worrying about side effects if someone or something adds custom conditions to the objects.

NOTE: The order of conditions types defines the priority for determining the Reason and Message for the target condition.

func WithCounter

func WithCounter() MergeOption

WithCounter instructs merge to add a "x of y Type" string to the message, where x is the number of conditions in the top group, y is the number of objects in scope, and Type is the top group condition type.

func WithCounterIfOnly

func WithCounterIfOnly(t ...string) MergeOption

WithCounterIfOnly ensures a counter is show only if a subset of condition exists. This may apply when you want to use a step counter while reconciling the resource, but then want to move away from this notation as soon as the resource has been reconciled, and e.g. a health check condition is generated.

IMPORTANT: This options requires WithStepCounter or WithStepCounterIf to be set.

func WithLatestGeneration

func WithLatestGeneration() MergeOption

WithLatestGeneration instructs merge to consider the conditions with the latest observed generation only.

func WithNegativePolarityConditions

func WithNegativePolarityConditions(t ...string) MergeOption

WithNegativePolarityConditions instructs merge about the condition types that adhere to a "normal-false" or "abnormal-true" pattern, i.e. that conditions are present with a value of True whenever something unusual happens.

NOTE: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties

func WithSourceRef

func WithSourceRef() MergeOption

WithSourceRef instructs merge to add info about the originating object to the target Reason and in summaries.

func WithSourceRefIf

func WithSourceRefIf(t ...string) MergeOption

WithSourceRefIf ensures a source ref is show only if one of the types in the set exists.

func WithStepCounter

func WithStepCounter() MergeOption

WithStepCounter instructs merge to add a "x of y completed" string to the message, where x is the number of conditions with Status=true and y is the number of conditions in scope.

func WithStepCounterIf

func WithStepCounterIf(value bool) MergeOption

WithStepCounterIf adds a step counter if the value is true. This can be used e.g. to add a step counter only if the object is not being deleted.

func WithStepCounterIfOnly

func WithStepCounterIfOnly(t ...string) MergeOption

WithStepCounterIfOnly ensures a step counter is show only if a subset of condition exists. This may apply when you want to use a step counter while reconciling the resource, but then want to move away from this notation as soon as the resource has been reconciled, and e.g. a health check condition is generated.

IMPORTANT: This options requires WithStepCounter or WithStepCounterIf to be set. IMPORTANT: This option works only while generating the Summary or Aggregated condition.

type MirrorOptions

MirrorOptions defines an option for mirroring conditions.

type MirrorOptions func(*mirrorOptions)

func WithFallbackValue

func WithFallbackValue(fallbackValue bool, reason string, message string) MirrorOptions

WithFallbackValue specify a fallback value to use in case the mirrored condition does not exists; in case the fallbackValue is false, given values for reason and message will be used.

type Patch

Patch defines a list of operations to change a list of conditions into another.

type Patch []PatchOperation

func NewPatch

func NewPatch(before Getter, after Getter) Patch

NewPatch returns the list of Patch required to align source conditions to after conditions.

func (Patch) Apply

func (p Patch) Apply(latest Setter, options ...ApplyOption) error

Apply executes a three-way merge of a list of Patch. When merge conflicts are detected (latest deviated from before in an incompatible way), an error is returned.

func (Patch) IsZero

func (p Patch) IsZero() bool

IsZero returns true if the patch has no changes.

type PatchOperation

PatchOperation define an operation that changes a single condition.

type PatchOperation struct {
    Before *metav1.Condition
    After  *metav1.Condition
    Op     PatchOperationType
}

type PatchOperationType

PatchOperationType defines patch operation types.

type PatchOperationType string
const (
    // AddConditionPatch defines an add condition patch operation.
    AddConditionPatch PatchOperationType = "Add"

    // ChangeConditionPatch defines an change condition patch operation.
    ChangeConditionPatch PatchOperationType = "Change"

    // RemoveConditionPatch defines a remove condition patch operation.
    RemoveConditionPatch PatchOperationType = "Remove"
)

type Setter

Setter is an interface that defines methods a Kubernetes object should implement in order to use the conditions package for setting conditions.

type Setter interface {
    Getter
    SetConditions([]metav1.Condition)
}