k3s/pkg/storage/interfaces.go

164 lines
7.7 KiB
Go
Raw Normal View History

2015-07-30 07:27:38 +00:00
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package storage
import (
"time"
"golang.org/x/net/context"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"
2015-07-30 07:27:38 +00:00
)
2015-07-30 07:45:06 +00:00
// Versioner abstracts setting and retrieving metadata fields from database response
2015-07-30 07:27:38 +00:00
// onto the object ot list.
2015-07-30 07:45:06 +00:00
type Versioner interface {
2015-07-30 07:27:38 +00:00
// 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, expiration *time.Time, 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.
UpdateList(obj runtime.Object, resourceVersion uint64) 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)
}
// 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 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
// Expiration is the time at which the node that contained the returned object will expire and be deleted.
// This can be nil if there is no expiration time set for the node.
Expiration *time.Time
// The resource version of the node that contained the returned object.
ResourceVersion uint64
}
// FilterFunc is a predicate which takes an API object and returns true
// if and only if the object should remain in the set.
2015-07-30 07:27:38 +00:00
type FilterFunc func(obj runtime.Object) bool
// Everything is a FilterFunc which accepts all objects.
func Everything(runtime.Object) bool {
return true
}
2015-07-30 07:45:06 +00:00
// Pass an UpdateFunc to Interface.GuaranteedUpdate to make an update
2015-07-30 07:27:38 +00:00
// that is guaranteed to succeed.
// See the comment for GuaranteedUpdate for more details.
2015-07-30 07:45:06 +00:00
type UpdateFunc func(input runtime.Object, res ResponseMeta) (output runtime.Object, ttl *uint64, err error)
2015-07-30 07:27:38 +00:00
2015-07-30 07:45:06 +00:00
// Interface offers a common interface for object marshaling/unmarshling operations and
2015-08-05 13:39:24 +00:00
// hides all the storage-related operations behind it.
2015-07-30 07:45:06 +00:00
type Interface interface {
2015-07-30 07:27:38 +00:00
// Returns list of servers addresses of the underyling database.
// TODO: This method is used only in a single place. Consider refactoring and getting rid
// of this method from the interface.
Backends(ctx context.Context) []string
2015-07-30 07:27:38 +00:00
2015-07-30 07:45:06 +00:00
// Returns Versioner associated with this interface.
Versioner() Versioner
2015-07-30 07:27:38 +00:00
// 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
2015-07-30 07:27:38 +00:00
// Set marshals obj via json and stores in database under key. Will do an atomic update
// if obj's ResourceVersion field is set. '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.
Set(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error
2015-07-30 07:27:38 +00:00
// Delete removes the specified key and returns the value that existed at that spot.
Delete(ctx context.Context, key string, out runtime.Object) error
2015-07-30 07:27:38 +00:00
// Watch begins watching the specified key. Events are decoded into API objects,
// and any items passing 'filter' 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
2015-07-30 07:27:38 +00:00
// (e.g. reconnecting without missing any updates).
Watch(ctx context.Context, key string, resourceVersion string, filter FilterFunc) (watch.Interface, error)
2015-07-30 07:27:38 +00:00
// WatchList begins watching the specified key's items. Items are decoded into API
// objects and any item passing 'filter' 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
2015-07-30 07:27:38 +00:00
// (e.g. reconnecting without missing any updates).
WatchList(ctx context.Context, key string, resourceVersion string, filter FilterFunc) (watch.Interface, error)
2015-07-30 07:27:38 +00:00
// Get unmarshals json 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 ignoreNotFound.
// Treats empty responses and nil response nodes exactly like a not found error.
Get(ctx context.Context, key string, objPtr runtime.Object, ignoreNotFound bool) error
2015-07-30 07:27:38 +00:00
// GetToList unmarshals json found at key and opaque it into *List api object
// (an object that satisfies the runtime.IsList definition).
GetToList(ctx context.Context, key string, filter FilterFunc, listObj runtime.Object) error
2015-07-30 07:27:38 +00:00
// List unmarshalls jsons found at directory defined by key and opaque them
// into *List api object (an object that satisfies runtime.IsList definition).
// The returned contents may be delayed, but it is guaranteed that they will
// be have at least 'resourceVersion'.
List(ctx context.Context, key string, resourceVersion string, filter FilterFunc, listObj runtime.Object) error
2015-07-30 07:27:38 +00:00
// GuaranteedUpdate keeps calling 'tryUpdate()' to update key 'key' (of type 'ptrToType')
// retrying the update until success if there is index conflict.
// Note that object passed to tryUpdate may change acress incovations of tryUpdate() if
// other writers are simultaneously updateing it, to tryUpdate() needs to take into account
2015-07-30 07:27:38 +00:00
// the current contents of the object when deciding how the update object should look.
//
// Example:
2015-07-30 07:27:38 +00:00
//
2015-07-30 07:45:06 +00:00
// s := /* implementation of Interface */
2015-07-30 07:27:38 +00:00
// err := s.GuaranteedUpdate(
// "myKey", &MyType{}, true,
// func(input runtime.Object, res ResponseMeta) (runtime.Object, *uint64, error) {
// // Before each incovation 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
// }
// })
GuaranteedUpdate(ctx context.Context, key string, ptrToType runtime.Object, ignoreNotFound bool, tryUpdate UpdateFunc) error
2015-08-05 13:39:24 +00:00
// Codec provides access to the underlying codec being used by the implementation.
Codec() runtime.Codec
2015-07-30 07:27:38 +00:00
}
// Config interface allows storage tiers to generate the proper storage.interface
// and reduce the dependencies to encapsulate storage.
type Config interface {
// Creates the Interface base on ConfigObject
NewStorage() (Interface, error)
// This function is used to enforce membership, and return the underlying type
GetType() string
}