state

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ERR_REASON_MISSING_STATE = "MissingState"
	ERR_REASON_INVALID_STATE = "InvalidState"
	ERR_REASON_INTERNAL      = "InternalError"
	ERR_REASON_READ_FAILED   = "ReadFailed"
	ERR_REASON_WRITE_FAILED  = "WriteFailed"
)

Variables

View Source
var (
	STATE_FIELD_LAST_SYNCED_GENERATION = &StateField{
		set: func(state *SyncState, value any) {
			state.LastSyncedGeneration = value.(int64)
		},
		get: func(state *SyncState) any {
			return state.LastSyncedGeneration
		},
		serialize: func(value any) any {
			return value
		},
		hasCorrectType: func(value any) bool {
			_, ok := value.(int64)
			return ok
		},
		name:       "lastSyncedGeneration",
		includedIn: sets.New[StateVerbosity](STATE_VERBOSITY_GENERATION, STATE_VERBOSITY_PHASE, STATE_VERBOSITY_DETAIL),
	}

	STATE_FIELD_PHASE = &StateField{
		set: func(state *SyncState, value any) {
			state.Phase = value.(Phase)
		},
		get: func(state *SyncState) any {
			return state.Phase
		},
		serialize: func(value any) any {
			return string(value.(Phase))
		},
		hasCorrectType: func(value any) bool {
			_, ok := value.(Phase)
			return ok
		},
		name:       "phase",
		includedIn: sets.New[StateVerbosity](STATE_VERBOSITY_PHASE, STATE_VERBOSITY_DETAIL),
	}

	STATE_FIELD_DETAIL = &StateField{
		set: func(state *SyncState, value any) {
			state.Detail = value.(string)
		},
		get: func(state *SyncState) any {
			return state.Detail
		},
		serialize: func(value any) any {
			return value
		},
		hasCorrectType: func(value any) bool {
			_, ok := value.(string)
			return ok
		},
		name:       "detail",
		includedIn: sets.New[StateVerbosity](STATE_VERBOSITY_DETAIL),
	}

	ALL_STATE_FIELDS = []*StateField{STATE_FIELD_LAST_SYNCED_GENERATION, STATE_FIELD_PHASE, STATE_FIELD_DETAIL}
)

Functions

func IsInternalStateError

func IsInternalStateError(err error) bool

func IsInvalidStateError

func IsInvalidStateError(err error) bool

func IsMissingStateError

func IsMissingStateError(err error) bool

func IsReadStateError

func IsReadStateError(err error) bool

func IsSynced

func IsSynced(obj client.Object, sd StateDisplay, state *SyncState) (bool, error)

IsSynced returns whether the last version of the object has been synced. If the object's state has already been read, it can be passed as argument. Otherwise (if the given SyncState is nil), the given StateDisplay's Read method will be used to read the state from the object. It then returns state.LastSyncedGeneration == obj.Generation().

Either the StateDisplay or the SyncState must be non-nil.

func IsWriteStateError

func IsWriteStateError(err error) bool

Types

type AnnotationStateDisplay

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

func NewAnnotationStateDisplay

func NewAnnotationStateDisplay(v StateVerbosity) *AnnotationStateDisplay

func (*AnnotationStateDisplay) Read

func (*AnnotationStateDisplay) Type

func (*AnnotationStateDisplay) Verbosity

func (asd *AnnotationStateDisplay) Verbosity() StateVerbosity

func (*AnnotationStateDisplay) Write

func (asd *AnnotationStateDisplay) Write(obj client.Object, state *SyncState, fields ...*StateField) (sets.Set[string], error)

type InternalStateError

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

InternalStateError is only returned if the developer did something wrong ;-)

func NewInternalStateError

func NewInternalStateError(msg string, values ...any) *InternalStateError

func (*InternalStateError) IsInternalStateError

func (e *InternalStateError) IsInternalStateError() bool

func (*InternalStateError) IsInvalidStateError

func (e *InternalStateError) IsInvalidStateError() bool

func (*InternalStateError) IsMissingStateError

func (e *InternalStateError) IsMissingStateError() bool

func (*InternalStateError) IsReadStateError

func (e *InternalStateError) IsReadStateError() bool

func (*InternalStateError) IsWriteStateError

func (e *InternalStateError) IsWriteStateError() bool

func (*InternalStateError) Reason

func (e *InternalStateError) Reason() string

type InvalidStateError

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

InvalidStateError is returned if a state cannot be read due to an invalid format of one or more state values.

func DefaultInvalidStateError

func DefaultInvalidStateError(field *StateField, fieldValue interface{}, err error) *InvalidStateError

func NewInvalidStateError

func NewInvalidStateError(msg string, values ...any) *InvalidStateError

func (*InvalidStateError) IsInternalStateError

func (e *InvalidStateError) IsInternalStateError() bool

func (*InvalidStateError) IsInvalidStateError

func (e *InvalidStateError) IsInvalidStateError() bool

func (*InvalidStateError) IsMissingStateError

func (e *InvalidStateError) IsMissingStateError() bool

func (*InvalidStateError) IsReadStateError

func (e *InvalidStateError) IsReadStateError() bool

func (*InvalidStateError) IsWriteStateError

func (e *InvalidStateError) IsWriteStateError() bool

func (*InvalidStateError) Reason

func (e *InvalidStateError) Reason() string

type MissingStateError

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

MissingStateError is returned if a part of the state, which is required by its verbosity, is missing.

func DefaultMissingStateError

func DefaultMissingStateError(v StateVerbosity, missingFields ...*StateField) *MissingStateError

func NewMissingStateError

func NewMissingStateError(msg string, values ...any) *MissingStateError

func (*MissingStateError) IsInternalStateError

func (e *MissingStateError) IsInternalStateError() bool

func (*MissingStateError) IsInvalidStateError

func (e *MissingStateError) IsInvalidStateError() bool

func (*MissingStateError) IsMissingStateError

func (e *MissingStateError) IsMissingStateError() bool

func (*MissingStateError) IsReadStateError

func (e *MissingStateError) IsReadStateError() bool

func (*MissingStateError) IsWriteStateError

func (e *MissingStateError) IsWriteStateError() bool

func (*MissingStateError) Reason

func (e *MissingStateError) Reason() string

type Phase

type Phase string
const (
	// PHASE_UNDEFINED is a dummy phase used as 'invalid' return value.
	PHASE_UNDEFINED Phase = "Undefined"
	// PHASE_PROGRESSING means that k8syncer has picked up the change and is working on it.
	PHASE_PROGRESSING Phase = "Progressing"
	// PHASE_FINISHED means that the sync is finished and there is nothing to do at the moment.
	PHASE_FINISHED Phase = "Finished"
	// PHASE_ERROR means that an error occurred and the resource will be requeued.
	PHASE_ERROR Phase = "Error"
	// PHASE_DELETING means that k8syncer has picked up a deletion and is working on it.
	PHASE_DELETING Phase = "Deleting"
	// PHASE_ERROR_DELETING is like PHASE_ERROR, but is used for errors which occur during deletion.
	PHASE_ERROR_DELETING Phase = "ErrorDeleting"
)

func PhaseFromString

func PhaseFromString(s string) Phase

PhaseFromString parses a given string into the corresponding phase. Returns PHASE_UNDEFINED if no phase matches.

type ReadStateError

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

ReadStateError is returned if reading the state from the resource failed for some other reason than it being missing or invalid.

func DefaultReadStateError

func DefaultReadStateError(err error) *ReadStateError

func NewReadStateError

func NewReadStateError(msg string, values ...any) *ReadStateError

func (*ReadStateError) IsInternalStateError

func (e *ReadStateError) IsInternalStateError() bool

func (*ReadStateError) IsInvalidStateError

func (e *ReadStateError) IsInvalidStateError() bool

func (*ReadStateError) IsMissingStateError

func (e *ReadStateError) IsMissingStateError() bool

func (*ReadStateError) IsReadStateError

func (e *ReadStateError) IsReadStateError() bool

func (*ReadStateError) IsWriteStateError

func (e *ReadStateError) IsWriteStateError() bool

func (*ReadStateError) Reason

func (e *ReadStateError) Reason() string

type StateDisplay

type StateDisplay interface {
	// Type returns the type of the state display.
	Type() string
	// Read reads the state from the given object.
	// Returns a MissingStateError if not enough fields are specified for the verbosity configured at the state display.
	// Returns an InvalidStateError if one of the state fields has an invalid value.
	Read(obj client.Object) (*SyncState, StateError)
	// Write writes the given SyncState into the given resource.
	// The first return value contains the top-level fields of the resource which actually changed.
	//   e.g. "metadata", "spec", "status"
	//   This is used to determine whether the resource needs to be updated after writing the state.
	//   nil or an empty set means that the resource has not changed.
	// It will write the state according to its verbosity.
	//   Not defined for STATE_VERBOSITY_ANY and STATE_VERBOSITY_UNDEFINED.
	// Only the specified fields are written, and only if they are included in the configured verbosity.
	//   Pass the ALL_STATE_FIELDS variable to write the complete state (also filtered by verbosity).
	Write(obj client.Object, state *SyncState, fields ...*StateField) (sets.Set[string], error)
	// Verbosity returns the verbosity for which this state display is configured.
	Verbosity() StateVerbosity
}

type StateError

type StateError interface {
	error
	Reason() string

	IsMissingStateError() bool
	IsInvalidStateError() bool
	IsInternalStateError() bool
	IsReadStateError() bool
	IsWriteStateError() bool
}

StateError is the parent error interface for state handling errors.

func ObjectToUnstructured

func ObjectToUnstructured(obj client.Object) (*unstructured.Unstructured, StateError)

ObjectToUnstructured converts a client.Object into an *unstructured.Unstructured.

type StateField

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

func (*StateField) Name

func (sf *StateField) Name() string

type StateVerbosity

type StateVerbosity string
const (
	// STATE_VERBOSITY_UNDEFINED is a dummy value for undefined verbosity.
	STATE_VERBOSITY_UNDEFINED StateVerbosity = "undefined"
	// STATE_VERBOSITY_ANY can be used to avoid MissingStateErrors.
	STATE_VERBOSITY_ANY StateVerbosity = "any"
	// STATE_VERBOSITY_GENERATION means that only the last synced generation is displayed.
	STATE_VERBOSITY_GENERATION StateVerbosity = StateVerbosity(config.STATE_VERBOSITY_GENERATION)
	// STATE_VERBOSITY_PHASE means that last synced generation and phase are displayed.
	STATE_VERBOSITY_PHASE StateVerbosity = StateVerbosity(config.STATE_VERBOSITY_PHASE)
	// STATE_VERBOSITY_DETAIL means that last synced generation, phase, and details are displayed.
	STATE_VERBOSITY_DETAIL StateVerbosity = StateVerbosity(config.STATE_VERBOSITY_DETAIL)
)

func (StateVerbosity) Includes

func (sv StateVerbosity) Includes(field *StateField) bool

IncludesPhase returns true if the verbosity includes the phase.

type StatusStateDisplay

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

func NewStatusStateDisplay

func NewStatusStateDisplay(lastSyncedGenerationPath, phasePath, detailPath string, v StateVerbosity) *StatusStateDisplay

func (*StatusStateDisplay) Read

func (ssd *StatusStateDisplay) Read(rawObj client.Object) (*SyncState, StateError)

func (*StatusStateDisplay) Type

func (*StatusStateDisplay) Type() string

func (*StatusStateDisplay) Verbosity

func (ssd *StatusStateDisplay) Verbosity() StateVerbosity

func (*StatusStateDisplay) Write

func (ssd *StatusStateDisplay) Write(rawObj client.Object, state *SyncState, fields ...*StateField) (sets.Set[string], error)

type SyncState

type SyncState struct {
	// Verbosity defines what is contained in the state.
	Verbosity StateVerbosity
	// Phase is the current phase.
	Phase Phase
	// LastSyncedGeneration is the last generation of the resource which has successfully been synced.
	LastSyncedGeneration int64
	// Detail can contain further details (e.g. error messages)
	Detail string
}

SyncState represents the current state.

func New

func New(v StateVerbosity) *SyncState

New creates a new SyncState with an undefined phase and the specified verbosity.

func (*SyncState) GetField

func (s *SyncState) GetField(field *StateField) any

func (*SyncState) IsFinal

func (s *SyncState) IsFinal() bool

IsFinal returns true if the state is final.

func (*SyncState) SetField

func (s *SyncState) SetField(field *StateField, value any) StateError

type WriteStateError

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

WriteStateError is returned if writing the state to the resource failed.

func DefaultWriteStateError

func DefaultWriteStateError(err error) *WriteStateError

func NewWriteStateError

func NewWriteStateError(msg string, values ...any) *WriteStateError

func (*WriteStateError) IsInternalStateError

func (e *WriteStateError) IsInternalStateError() bool

func (*WriteStateError) IsInvalidStateError

func (e *WriteStateError) IsInvalidStateError() bool

func (*WriteStateError) IsMissingStateError

func (e *WriteStateError) IsMissingStateError() bool

func (*WriteStateError) IsReadStateError

func (e *WriteStateError) IsReadStateError() bool

func (*WriteStateError) IsWriteStateError

func (e *WriteStateError) IsWriteStateError() bool

func (*WriteStateError) Reason

func (e *WriteStateError) Reason() string

Jump to

Keyboard shortcuts

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