storage

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: Apache-2.0 Imports: 20 Imported by: 2,190

Documentation

Overview

Interfaces for database-related operations.

Index

Constants

View Source
const (
	ErrCodeKeyNotFound int = iota + 1
	ErrCodeKeyExists
	ErrCodeResourceVersionConflicts
	ErrCodeInvalidObj
	ErrCodeUnreachable
	ErrCodeTimeout
)

Variables

View Source
var (
	ErrInvalidStartRV             = errors.New("continue key is not valid: incorrect encoded start resourceVersion (version meta.k8s.io/v1)")
	ErrEmptyStartKey              = errors.New("continue key is not valid: encoded start key empty (version meta.k8s.io/v1)")
	ErrGenericInvalidKey          = errors.New("continue key is not valid")
	ErrUnrecognizedEncodedVersion = errors.New("continue key is not valid: server does not recognize this encoded version")
)
View Source
var (
	ErrResourceVersionSetOnCreate = errors.New("resourceVersion should not be set on objects to be created")
	ErrStorageNotReady            = errors.New("storage not ready")
)
View Source
var Everything = SelectionPredicate{
	Label: labels.Everything(),
	Field: fields.Everything(),
}

Everything accepts all objects.

Functions

func AnnotateInitialEventsEndBookmark added in v0.29.0

func AnnotateInitialEventsEndBookmark(obj runtime.Object) error

AnnotateInitialEventsEndBookmark adds a special annotation to the given object which indicates that the initial events have been sent.

Note that this function assumes that the obj's annotation field is a reference type (i.e. a map).

func DecodeContinue added in v0.25.0

func DecodeContinue(continueValue, keyPrefix string) (fromKey string, rv int64, err error)

DecodeContinue transforms an encoded predicate from into a versioned struct. TODO: return a typed error that instructs clients that they must relist

func DefaultClusterScopedAttr

func DefaultClusterScopedAttr(obj runtime.Object) (labels.Set, fields.Set, error)

func DefaultNamespaceScopedAttr

func DefaultNamespaceScopedAttr(obj runtime.Object) (labels.Set, fields.Set, error)

func EncodeContinue added in v0.25.0

func EncodeContinue(key, keyPrefix string, resourceVersion int64) (string, error)

EncodeContinue returns a string representing the encoded continuation of the current query.

func FieldIndex added in v0.18.0

func FieldIndex(field string) string

FiledIndex add prefix for field index.

func GetCurrentResourceVersionFromStorage added in v0.29.0

func GetCurrentResourceVersionFromStorage(ctx context.Context, storage Interface, newListFunc func() runtime.Object, resourcePrefix, objectType string) (uint64, error)

GetCurrentResourceVersionFromStorage gets the current resource version from the underlying storage engine. This method issues an empty list request and reads only the ResourceVersion from the object metadata

func HasInitialEventsEndBookmarkAnnotation added in v0.29.0

func HasInitialEventsEndBookmarkAnnotation(obj runtime.Object) (bool, error)

HasInitialEventsEndBookmarkAnnotation checks the presence of the special annotation which marks that the initial events have been sent.

func IsConflict

func IsConflict(err error) bool

IsConflict returns true if and only if err is a write conflict.

func IsExist added in v0.24.0

func IsExist(err error) bool

IsExist returns true if and only if err is "key" already exists error.

func IsInternalError

func IsInternalError(err error) bool

IsInternalError returns true if and only if err is an InternalError.

func IsInvalidError

func IsInvalidError(err error) bool

IsInvalidError returns true if and only if err is an InvalidError.

func IsInvalidObj

func IsInvalidObj(err error) bool

IsInvalidObj returns true if and only if err is invalid error

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if and only if err is "key" not found error.

func IsRequestTimeout added in v0.30.0

func IsRequestTimeout(err error) bool

IsRequestTimeout returns true if and only if err indicates that the request has timed out.

func IsTooLargeResourceVersion added in v0.17.0

func IsTooLargeResourceVersion(err error) bool

IsTooLargeResourceVersion returns true if the error is a TooLargeResourceVersion error.

func IsUnreachable

func IsUnreachable(err error) bool

IsUnreachable returns true if and only if err indicates the server could not be reached.

func LabelIndex added in v0.18.0

func LabelIndex(label string) string

LabelIndex add prefix for label index.

func NamespaceKeyFunc

func NamespaceKeyFunc(prefix string, obj runtime.Object) (string, error)

func NewTooLargeResourceVersionError added in v0.17.0

func NewTooLargeResourceVersionError(minimumResourceVersion, currentRevision uint64, retrySeconds int) error

NewTooLargeResourceVersionError returns a timeout error with the given retrySeconds for a request for a minimum resource version that is larger than the largest currently available resource version for a requested resource.

func NoNamespaceKeyFunc

func NoNamespaceKeyFunc(prefix string, obj runtime.Object) (string, error)

func PrepareContinueToken added in v0.31.0

func PrepareContinueToken(keyLastItem, keyPrefix string, resourceVersion int64, itemsCount int64, hasMoreItems bool, opts ListOptions) (string, *int64, error)

PrepareContinueToken prepares optional parameters for retrieving additional results for a paginated request.

This function sets up parameters that a client can use to fetch the remaining results from the server if they are available.

func ValidateAllObjectFunc

func ValidateAllObjectFunc(ctx context.Context, obj runtime.Object) error

ValidateAllObjectFunc is a "admit everything" instance of ValidateObjectFunc.

Types

type APIObjectVersioner added in v0.25.0

type APIObjectVersioner struct{}

APIObjectVersioner implements versioning and extracting etcd node information for objects that have an embedded ObjectMeta or ListMeta field.

func (APIObjectVersioner) CompareResourceVersion added in v0.25.0

func (a APIObjectVersioner) CompareResourceVersion(lhs, rhs runtime.Object) int

CompareResourceVersion compares etcd resource versions. Outside this API they are all strings, but etcd resource versions are special, they're actually ints, so we can easily compare them.

func (APIObjectVersioner) ObjectResourceVersion added in v0.25.0

func (a APIObjectVersioner) ObjectResourceVersion(obj runtime.Object) (uint64, error)

ObjectResourceVersion implements Versioner

func (APIObjectVersioner) ParseResourceVersion added in v0.25.0

func (a APIObjectVersioner) ParseResourceVersion(resourceVersion string) (uint64, error)

ParseResourceVersion takes a resource version argument and converts it to the etcd version. For watch we should pass to helper.Watch(). Because resourceVersion is an opaque value, the default watch behavior for non-zero watch is to watch the next value (if you pass "1", you will see updates from "2" onwards).

func (APIObjectVersioner) PrepareObjectForStorage added in v0.25.0

func (a APIObjectVersioner) PrepareObjectForStorage(obj runtime.Object) error

PrepareObjectForStorage clears resourceVersion and selfLink prior to writing to etcd.

func (APIObjectVersioner) UpdateList added in v0.25.0

func (a APIObjectVersioner) UpdateList(obj runtime.Object, resourceVersion uint64, nextKey string, count *int64) error

UpdateList implements Versioner

func (APIObjectVersioner) UpdateObject added in v0.25.0

func (a APIObjectVersioner) UpdateObject(obj runtime.Object, resourceVersion uint64) error

UpdateObject implements Versioner

type AttrFunc

type AttrFunc func(obj runtime.Object) (labels.Set, fields.Set, error)

AttrFunc returns label and field sets and the uninitialized flag for List or Watch to match. In any failure to parse given object, it returns error.

func (AttrFunc) WithFieldMutation

func (f AttrFunc) WithFieldMutation(fieldMutator FieldMutationFunc) AttrFunc

type Feature added in v0.31.0

type Feature = string

Feature is the name of each feature in storage that we check in feature_support_checker.

var RequestWatchProgress Feature = "RequestWatchProgress"

RequestWatchProgress is an etcd feature that may use to check if it supported or not.

type FieldMutationFunc

type FieldMutationFunc func(obj runtime.Object, fieldSet fields.Set) error

FieldMutationFunc allows the mutation of the field selection fields. It is mutating to avoid the extra allocation on this common path

type GetOptions added in v0.19.0

type GetOptions struct {
	// IgnoreNotFound determines what is returned if the requested object is not found. If
	// true, a zero object is returned. If false, an error is returned.
	IgnoreNotFound bool
	// ResourceVersion provides a resource version constraint to apply to the get operation
	// as a "not older than" constraint: the result contains data at least as new as the provided
	// ResourceVersion. The newest available data is preferred, but any data not older than this
	// ResourceVersion may be served.
	ResourceVersion string
}

GetOptions provides the options that may be provided for storage get operations.

type HighWaterMark

type HighWaterMark int64

HighWaterMark is a thread-safe object for tracking the maximum value seen for some quantity.

func (*HighWaterMark) Update

func (hwm *HighWaterMark) Update(current int64) bool

Update returns true if and only if 'current' is the highest value ever seen.

type IndexerFunc added in v0.16.4

type IndexerFunc func(obj runtime.Object) string

IndexerFunc is a function that for a given object computes `<value of an index>` for a particular `<index>`.

type IndexerFuncs added in v0.16.4

type IndexerFuncs map[string]IndexerFunc

IndexerFuncs is a mapping from `<index name>` to function that for a given object computes `<value for that index>`.

type Interface

type Interface interface {
	// Returns Versioner associated with this interface.
	Versioner() Versioner

	// Create adds a new object at a key unless it already exists. 'ttl' is time-to-live
	// in seconds (0 means forever). If no error is returned and out is not nil, out will be
	// set to the read value from database.
	Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error

	// Delete removes the specified key and returns the value that existed at that spot.
	// If key didn't exist, it will return NotFound storage error.
	// If 'cachedExistingObject' is non-nil, it can be used as a suggestion about the
	// current version of the object to avoid read operation from storage to get it.
	// However, the implementations have to retry in case suggestion is stale.
	Delete(
		ctx context.Context, key string, out runtime.Object, preconditions *Preconditions,
		validateDeletion ValidateObjectFunc, cachedExistingObject runtime.Object) error

	// Watch begins watching the specified key. Events are decoded into API objects,
	// and any items selected by 'p' are sent down to returned watch.Interface.
	// resourceVersion may be used to specify what version to begin watching,
	// which should be the current resourceVersion, and no longer rv+1
	// (e.g. reconnecting without missing any updates).
	// If resource version is "0", this interface will get current object at given key
	// and send it in an "ADDED" event, before watch starts.
	Watch(ctx context.Context, key string, opts ListOptions) (watch.Interface, error)

	// Get unmarshals object found at key into objPtr. On a not found error, will either
	// return a zero object of the requested type, or an error, depending on 'opts.ignoreNotFound'.
	// Treats empty responses and nil response nodes exactly like a not found error.
	// The returned contents may be delayed, but it is guaranteed that they will
	// match 'opts.ResourceVersion' according 'opts.ResourceVersionMatch'.
	Get(ctx context.Context, key string, opts GetOptions, objPtr runtime.Object) error

	// GetList unmarshalls objects found at key into a *List api object (an object
	// that satisfies runtime.IsList definition).
	// If 'opts.Recursive' is false, 'key' is used as an exact match. If `opts.Recursive'
	// is true, 'key' is used as a prefix.
	// The returned contents may be delayed, but it is guaranteed that they will
	// match 'opts.ResourceVersion' according 'opts.ResourceVersionMatch'.
	GetList(ctx context.Context, key string, opts ListOptions, listObj runtime.Object) error

	// GuaranteedUpdate keeps calling 'tryUpdate()' to update key 'key' (of type 'destination')
	// retrying the update until success if there is index conflict.
	// Note that object passed to tryUpdate may change across invocations of tryUpdate() if
	// other writers are simultaneously updating it, so tryUpdate() needs to take into account
	// the current contents of the object when deciding how the update object should look.
	// If the key doesn't exist, it will return NotFound storage error if ignoreNotFound=false
	// else `destination` will be set to the zero value of it's type.
	// If the eventual successful invocation of `tryUpdate` returns an output with the same serialized
	// contents as the input, it won't perform any update, but instead set `destination` to an object with those
	// contents.
	// If 'cachedExistingObject' is non-nil, it can be used as a suggestion about the
	// current version of the object to avoid read operation from storage to get it.
	// However, the implementations have to retry in case suggestion is stale.
	//
	// Example:
	//
	// s := /* implementation of Interface */
	// err := s.GuaranteedUpdate(
	//     "myKey", &MyType{}, true, preconditions,
	//     func(input runtime.Object, res ResponseMeta) (runtime.Object, *uint64, error) {
	//       // Before each invocation of the user defined function, "input" is reset to
	//       // current contents for "myKey" in database.
	//       curr := input.(*MyType)  // Guaranteed to succeed.
	//
	//       // Make the modification
	//       curr.Counter++
	//
	//       // Return the modified object - return an error to stop iterating. Return
	//       // a uint64 to alter the TTL on the object, or nil to keep it the same value.
	//       return cur, nil, nil
	//    }, cachedExistingObject
	// )
	GuaranteedUpdate(
		ctx context.Context, key string, destination runtime.Object, ignoreNotFound bool,
		preconditions *Preconditions, tryUpdate UpdateFunc, cachedExistingObject runtime.Object) error

	// Count returns number of different entries under the key (generally being path prefix).
	Count(key string) (int64, error)

	// ReadinessCheck checks if the storage is ready for accepting requests.
	ReadinessCheck() error

	// RequestWatchProgress requests the a watch stream progress status be sent in the
	// watch response stream as soon as possible.
	// Used for monitor watch progress even if watching resources with no changes.
	//
	// If watch is lagging, progress status might:
	// * be pointing to stale resource version. Use etcd KV request to get linearizable resource version.
	// * not be delivered at all. It's recommended to poll request progress periodically.
	//
	// Note: Only watches with matching context grpc metadata will be notified.
	// https://github.com/kubernetes/kubernetes/blob/9325a57125e8502941d1b0c7379c4bb80a678d5c/vendor/go.etcd.io/etcd/client/v3/watch.go#L1037-L1042
	//
	// TODO: Remove when storage.Interface will be separate from etc3.store.
	// Deprecated: Added temporarily to simplify exposing RequestProgress for watch cache.
	RequestWatchProgress(ctx context.Context) error
}

Interface offers a common interface for object marshaling/unmarshaling operations and hides all the storage-related operations behind it.

type InternalError

type InternalError struct {
	Reason string
}

InternalError is generated when an error occurs in the storage package, i.e., not from the underlying storage backend (e.g., etcd).

func NewInternalError

func NewInternalError(reason string) InternalError

func NewInternalErrorf

func NewInternalErrorf(format string, a ...interface{}) InternalError

func (InternalError) Error

func (e InternalError) Error() string

type InvalidError

type InvalidError struct {
	Errs field.ErrorList
}

InvalidError is generated when an error caused by invalid API object occurs in the storage package.

func NewInvalidError

func NewInvalidError(errors field.ErrorList) InvalidError

func (InvalidError) Error

func (e InvalidError) Error() string

type ListOptions added in v0.19.0

type ListOptions struct {
	// ResourceVersion provides a resource version constraint to apply to the list operation
	// as a "not older than" constraint: the result contains data at least as new as the provided
	// ResourceVersion. The newest available data is preferred, but any data not older than this
	// ResourceVersion may be served.
	ResourceVersion string
	// ResourceVersionMatch provides the rule for how the resource version constraint applies. If set
	// to the default value "" the legacy resource version semantic apply.
	ResourceVersionMatch metav1.ResourceVersionMatch
	// Predicate provides the selection rules for the list operation.
	Predicate SelectionPredicate
	// Recursive determines whether the list or watch is defined for a single object located at the
	// given key, or for the whole set of objects with the given key as a prefix.
	Recursive bool
	// ProgressNotify determines whether storage-originated bookmark (progress notify) events should
	// be delivered to the users. The option is ignored for non-watch requests.
	//
	// Firstly, note that this field is different from the Predicate.AllowWatchBookmarks field.
	// Secondly, this field is intended for internal clients only such as the watch cache.
	//
	// This means that external clients do not have the ability to set this field directly.
	// For example by setting the allowWatchBookmarks query parameter.
	//
	// The motivation for this approach is the fact that the frequency
	// of bookmark events from a storage like etcd might be very high.
	// As the number of watch requests increases, the server load would also increase.
	//
	// Furthermore, the server is not obligated to provide bookmark events at all,
	// as described in https://github.com/kubernetes/enhancements/tree/master/keps/sig-api-machinery/956-watch-bookmark#proposal
	ProgressNotify bool
	// SendInitialEvents, when set together with Watch option,
	// begin the watch stream with synthetic init events to build the
	// whole state of all resources followed by a synthetic "Bookmark"
	// event containing a ResourceVersion after which the server
	// continues streaming events.
	SendInitialEvents *bool
}

ListOptions provides the options that may be provided for storage list operations.

type MatchValue

type MatchValue struct {
	IndexName string
	Value     string
}

MatchValue defines a pair (`<index name>`, `<value for that index>`).

type Preconditions

type Preconditions struct {
	// Specifies the target UID.
	// +optional
	UID *types.UID `json:"uid,omitempty"`
	// Specifies the target ResourceVersion
	// +optional
	ResourceVersion *string `json:"resourceVersion,omitempty"`
}

Preconditions must be fulfilled before an operation (update, delete, etc.) is carried out.

func NewUIDPreconditions

func NewUIDPreconditions(uid string) *Preconditions

NewUIDPreconditions returns a Preconditions with UID set.

func (*Preconditions) Check

func (p *Preconditions) Check(key string, obj runtime.Object) error

type ResponseMeta

type ResponseMeta struct {
	// TTL is the time to live of the node that contained the returned object. It may be
	// zero or negative in some cases (objects may be expired after the requested
	// expiration time due to server lag).
	TTL int64
	// The resource version of the node that contained the returned object.
	ResourceVersion uint64
}

ResponseMeta contains information about the database metadata that is associated with an object. It abstracts the actual underlying objects to prevent coupling with concrete database and to improve testability.

type SelectionPredicate

type SelectionPredicate struct {
	Label               labels.Selector
	Field               fields.Selector
	GetAttrs            AttrFunc
	IndexLabels         []string
	IndexFields         []string
	Limit               int64
	Continue            string
	AllowWatchBookmarks bool
}

SelectionPredicate is used to represent the way to select objects from api storage.

func (*SelectionPredicate) Empty

func (s *SelectionPredicate) Empty() bool

Empty returns true if the predicate performs no filtering.

func (*SelectionPredicate) MatcherIndex

func (s *SelectionPredicate) MatcherIndex(ctx context.Context) []MatchValue

For any index defined by IndexFields, if a matcher can match only (a subset) of objects that return <value> for a given index, a pair (<index name>, <value>) wil be returned.

func (*SelectionPredicate) Matches

func (s *SelectionPredicate) Matches(obj runtime.Object) (bool, error)

Matches returns true if the given object's labels and fields (as returned by s.GetAttrs) match s.Label and s.Field. An error is returned if s.GetAttrs fails.

func (*SelectionPredicate) MatchesObjectAttributes

func (s *SelectionPredicate) MatchesObjectAttributes(l labels.Set, f fields.Set) bool

MatchesObjectAttributes returns true if the given labels and fields match s.Label and s.Field.

func (*SelectionPredicate) MatchesSingle

func (s *SelectionPredicate) MatchesSingle() (string, bool)

MatchesSingle will return (name, true) if and only if s.Field matches on the object's name.

func (*SelectionPredicate) MatchesSingleNamespace added in v0.27.0

func (s *SelectionPredicate) MatchesSingleNamespace() (string, bool)

MatchesSingleNamespace will return (namespace, true) if and only if s.Field matches on the object's namespace.

type SimpleUpdateFunc

type SimpleUpdateFunc func(runtime.Object) (runtime.Object, error)

type StorageError

type StorageError struct {
	Code               int
	Key                string
	ResourceVersion    int64
	AdditionalErrorMsg string
}

func NewInvalidObjError

func NewInvalidObjError(key, msg string) *StorageError

func NewKeyExistsError

func NewKeyExistsError(key string, rv int64) *StorageError

func NewKeyNotFoundError

func NewKeyNotFoundError(key string, rv int64) *StorageError

func NewResourceVersionConflictsError

func NewResourceVersionConflictsError(key string, rv int64) *StorageError

func NewTimeoutError added in v0.30.0

func NewTimeoutError(key, msg string) *StorageError

func NewUnreachableError

func NewUnreachableError(key string, rv int64) *StorageError

func (*StorageError) Error

func (e *StorageError) Error() string

type UpdateFunc

type UpdateFunc func(input runtime.Object, res ResponseMeta) (output runtime.Object, ttl *uint64, err error)

Pass an UpdateFunc to Interface.GuaranteedUpdate to make an update that is guaranteed to succeed. See the comment for GuaranteedUpdate for more details.

func SimpleUpdate

func SimpleUpdate(fn SimpleUpdateFunc) UpdateFunc

SimpleUpdateFunc converts SimpleUpdateFunc into UpdateFunc

type ValidateObjectFunc

type ValidateObjectFunc func(ctx context.Context, obj runtime.Object) error

ValidateObjectFunc is a function to act on a given object. An error may be returned if the hook cannot be completed. The function may NOT transform the provided object.

type Versioner

type Versioner interface {
	// UpdateObject sets storage metadata into an API object. Returns an error if the object
	// cannot be updated correctly. May return nil if the requested object does not need metadata
	// from database.
	UpdateObject(obj runtime.Object, resourceVersion uint64) error
	// UpdateList sets the resource version into an API list object. Returns an error if the object
	// cannot be updated correctly. May return nil if the requested object does not need metadata from
	// database. continueValue is optional and indicates that more results are available if the client
	// passes that value to the server in a subsequent call. remainingItemCount indicates the number
	// of remaining objects if the list is partial. The remainingItemCount field is omitted during
	// serialization if it is set to nil.
	UpdateList(obj runtime.Object, resourceVersion uint64, continueValue string, remainingItemCount *int64) error
	// PrepareObjectForStorage should set SelfLink and ResourceVersion to the empty value. Should
	// return an error if the specified object cannot be updated.
	PrepareObjectForStorage(obj runtime.Object) error
	// ObjectResourceVersion returns the resource version (for persistence) of the specified object.
	// Should return an error if the specified object does not have a persistable version.
	ObjectResourceVersion(obj runtime.Object) (uint64, error)

	// ParseResourceVersion takes a resource version argument and
	// converts it to the storage backend. For watch we should pass to helper.Watch().
	// Because resourceVersion is an opaque value, the default watch
	// behavior for non-zero watch is to watch the next value (if you pass
	// "1", you will see updates from "2" onwards).
	ParseResourceVersion(resourceVersion string) (uint64, error)
}

Versioner abstracts setting and retrieving metadata fields from database response onto the object ot list. It is required to maintain storage invariants - updating an object twice with the same data except for the ResourceVersion and SelfLink must be a no-op. A resourceVersion of type uint64 is a 'raw' resourceVersion, intended to be sent directly to or from the backend. A resourceVersion of type string is a 'safe' resourceVersion, intended for consumption by users.

Directories

Path Synopsis
Package storage provides conversion of storage errors to API errors.
Package storage provides conversion of storage errors to API errors.
Package value contains methods for assisting with transformation of values in storage.
Package value contains methods for assisting with transformation of values in storage.
encrypt/aes
Package aes transforms values for storage at rest using AES-GCM.
Package aes transforms values for storage at rest using AES-GCM.
encrypt/envelope
Package envelope transforms values for storage at rest using a Envelope provider
Package envelope transforms values for storage at rest using a Envelope provider
encrypt/envelope/kmsv2
Package kmsv2 transforms values for storage at rest using a Envelope v2 provider
Package kmsv2 transforms values for storage at rest using a Envelope v2 provider
encrypt/envelope/kmsv2/v2
Package v2 contains definition of kms-plugin's serialized types.
Package v2 contains definition of kms-plugin's serialized types.
encrypt/secretbox
Package secretbox transforms values for storage at rest using XSalsa20 and Poly1305.
Package secretbox transforms values for storage at rest using XSalsa20 and Poly1305.

Jump to

Keyboard shortcuts

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