tools

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2015 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package tools implements general tools which depend on the api package.

Index

Constants

View Source
const (
	EtcdErrorCodeNotFound      = 100
	EtcdErrorCodeTestFailed    = 101
	EtcdErrorCodeNodeExist     = 105
	EtcdErrorCodeValueRequired = 200
)

Variables

View Source
var (
	EtcdErrorNotFound      = &etcd.EtcdError{ErrorCode: EtcdErrorCodeNotFound}
	EtcdErrorTestFailed    = &etcd.EtcdError{ErrorCode: EtcdErrorCodeTestFailed}
	EtcdErrorNodeExist     = &etcd.EtcdError{ErrorCode: EtcdErrorCodeNodeExist}
	EtcdErrorValueRequired = &etcd.EtcdError{ErrorCode: EtcdErrorCodeValueRequired}
)

Functions

func Everything

func Everything(runtime.Object) bool

Everything is a FilterFunc which accepts all objects.

func IsEtcdNodeExist

func IsEtcdNodeExist(err error) bool

IsEtcdNodeExist returns true iff err is an etcd node aleady exist error.

func IsEtcdNotFound

func IsEtcdNotFound(err error) bool

IsEtcdNotFound returns true iff err is an etcd not found error.

func IsEtcdTestFailed

func IsEtcdTestFailed(err error) bool

IsEtcdTestFailed returns true iff err is an etcd write conflict.

func IsEtcdWatchStoppedByUser

func IsEtcdWatchStoppedByUser(err error) bool

IsEtcdWatchStoppedByUser returns true iff err is a client triggered stop.

func ParseWatchResourceVersion added in v0.2.1

func ParseWatchResourceVersion(resourceVersion, kind string) (uint64, error)

ParseWatchResourceVersion takes a resource version argument and converts it to the etcd version 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).

Types

type EtcdClient

type EtcdClient interface {
	GetCluster() []string
	AddChild(key, data string, ttl uint64) (*etcd.Response, error)
	Get(key string, sort, recursive bool) (*etcd.Response, error)
	Set(key, value string, ttl uint64) (*etcd.Response, error)
	Create(key, value string, ttl uint64) (*etcd.Response, error)
	CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error)
	Delete(key string, recursive bool) (*etcd.Response, error)
	// I'd like to use directional channels here (e.g. <-chan) but this interface mimics
	// the etcd client interface which doesn't, and it doesn't seem worth it to wrap the api.
	Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)
}

EtcdClient is an injectable interface for testing.

func NewEtcdClientStartServerIfNecessary added in v0.2.1

func NewEtcdClientStartServerIfNecessary(server string) (EtcdClient, error)

type EtcdGetSet

type EtcdGetSet interface {
	GetCluster() []string
	Get(key string, sort, recursive bool) (*etcd.Response, error)
	Set(key, value string, ttl uint64) (*etcd.Response, error)
	Create(key, value string, ttl uint64) (*etcd.Response, error)
	Delete(key string, recursive bool) (*etcd.Response, error)
	CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error)
	Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)
}

EtcdGetSet interface exposes only the etcd operations needed by EtcdHelper.

type EtcdHelper

type EtcdHelper struct {
	Client EtcdGetSet
	Codec  runtime.Codec
	// optional, no atomic operations can be performed without this interface
	ResourceVersioner EtcdResourceVersioner
}

EtcdHelper offers common object marshalling/unmarshalling operations on an etcd client.

func (*EtcdHelper) AtomicUpdate

func (h *EtcdHelper) AtomicUpdate(key string, ptrToType runtime.Object, ignoreNotFound bool, tryUpdate EtcdUpdateFunc) error

AtomicUpdate generalizes the pattern that allows for making atomic updates to etcd objects. Note, tryUpdate may be called more than once.

Example:

h := &util.EtcdHelper{client, encoding, versioning}

err := h.AtomicUpdate("myKey", &MyType{}, true, func(input runtime.Object) (runtime.Object, error) {
	// Before this function is called, currentObj has been reset to etcd's current
	// contents for "myKey".

	cur := input.(*MyType) // Guaranteed to work.

	// Make a *modification*.
	cur.Counter++

	// Return the modified object. Return an error to stop iterating.
	return cur, nil
})

func (*EtcdHelper) Create added in v0.3.2

func (h *EtcdHelper) Create(key string, obj, out runtime.Object, ttl uint64) error

Create adds a new object at a key unless it already exists. 'ttl' is time-to-live in seconds, and 0 means forever. If no error is returned, out will be set to the read value from etcd.

func (*EtcdHelper) CreateObj

func (h *EtcdHelper) CreateObj(key string, obj runtime.Object, ttl uint64) error

CreateObj adds a new object at a key unless it already exists. 'ttl' is time-to-live in seconds, and 0 means forever.

func (*EtcdHelper) Delete

func (h *EtcdHelper) Delete(key string, recursive bool) error

Delete removes the specified key.

func (*EtcdHelper) DeleteObj added in v0.3.2

func (h *EtcdHelper) DeleteObj(key string, out runtime.Object) error

DeleteObj removes the specified key and returns the value that existed at that spot.

func (*EtcdHelper) ExtractList

func (h *EtcdHelper) ExtractList(key string, slicePtr interface{}, resourceVersion *uint64) error

ExtractList extracts a go object per etcd node into a slice with the resource version. DEPRECATED: Use ExtractToList instead, it's more convenient.

func (*EtcdHelper) ExtractObj

func (h *EtcdHelper) ExtractObj(key string, objPtr runtime.Object, ignoreNotFound bool) error

ExtractObj 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.

func (*EtcdHelper) ExtractToList

func (h *EtcdHelper) ExtractToList(key string, listObj runtime.Object) error

ExtractToList is just like ExtractList, but it works on a ThingyList api object. extracts a go object per etcd node into a slice with the resource version.

func (*EtcdHelper) SetObj

func (h *EtcdHelper) SetObj(key string, obj runtime.Object, ttl uint64) error

SetObj marshals obj via json, and stores under key. Will do an atomic update if obj's ResourceVersion field is set. 'ttl' is time-to-live in seconds, and 0 means forever.

func (*EtcdHelper) Watch

func (h *EtcdHelper) Watch(key string, resourceVersion uint64) watch.Interface

Watch begins watching the specified key. Events are decoded into API objects and sent down the returned watch.Interface. Errors will be sent down the channel.

func (*EtcdHelper) WatchAndTransform

func (h *EtcdHelper) WatchAndTransform(key string, resourceVersion uint64, transform TransformFunc) watch.Interface

WatchAndTransform begins watching the specified key. Events are decoded into API objects and sent down the returned watch.Interface. If the transform function is provided, the value decoded from etcd will be passed to the function prior to being returned.

The transform function can be used to populate data not available to etcd, or to change or wrap the serialized etcd object.

startTime := time.Now()
helper.WatchAndTransform(key, version, func(input runtime.Object) (runtime.Object, error) {
  value := input.(TimeAwareValue)
  value.Since = startTime
  return value, nil
})

Errors will be sent down the channel.

func (*EtcdHelper) WatchList

func (h *EtcdHelper) WatchList(key string, resourceVersion uint64, filter FilterFunc) (watch.Interface, error)

WatchList begins watching the specified key's items. Items are decoded into API objects, and any items passing 'filter' are sent down the returned watch.Interface. resourceVersion may be used to specify what version to begin watching (e.g., for reconnecting without missing any updates).

type EtcdResourceVersioner

type EtcdResourceVersioner interface {
	SetResourceVersion(obj runtime.Object, version uint64) error
	ResourceVersion(obj runtime.Object) (uint64, error)
}

type EtcdResponseWithError

type EtcdResponseWithError struct {
	R *etcd.Response
	E error
	// if N is non-null, it will be assigned into the map after this response is used for an operation
	N *EtcdResponseWithError
}

type EtcdUpdateFunc

type EtcdUpdateFunc func(input runtime.Object) (output runtime.Object, err error)

Pass an EtcdUpdateFunc to EtcdHelper.AtomicUpdate to make an atomic etcd update. See the comment for AtomicUpdate for more detail.

type FakeEtcdClient

type FakeEtcdClient struct {
	Data        map[string]EtcdResponseWithError
	DeletedKeys []string

	sync.Mutex
	Err error

	Ix          int
	TestIndex   bool
	ChangeIndex uint64
	LastSetTTL  uint64
	Machines    []string

	// Will become valid after Watch is called; tester may write to it. Tester may
	// also read from it to verify that it's closed after injecting an error.
	WatchResponse chan *etcd.Response
	WatchIndex    uint64
	// Write to this to prematurely stop a Watch that is running in a goroutine.
	WatchInjectError chan<- error
	WatchStop        chan<- bool
	// If non-nil, will be returned immediately when Watch is called.
	WatchImmediateError error
	// contains filtered or unexported fields
}

func NewFakeEtcdClient

func NewFakeEtcdClient(t TestLogger) *FakeEtcdClient

func (*FakeEtcdClient) AddChild

func (f *FakeEtcdClient) AddChild(key, data string, ttl uint64) (*etcd.Response, error)

func (*FakeEtcdClient) CompareAndSwap

func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error)

func (*FakeEtcdClient) Create

func (f *FakeEtcdClient) Create(key, value string, ttl uint64) (*etcd.Response, error)

func (*FakeEtcdClient) Delete

func (f *FakeEtcdClient) Delete(key string, recursive bool) (*etcd.Response, error)

func (*FakeEtcdClient) ExpectNotFoundGet

func (f *FakeEtcdClient) ExpectNotFoundGet(key string)

func (*FakeEtcdClient) Get

func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, error)

func (*FakeEtcdClient) GetCluster added in v0.2.1

func (f *FakeEtcdClient) GetCluster() []string

func (*FakeEtcdClient) NewError added in v0.3.2

func (f *FakeEtcdClient) NewError(code int) *etcd.EtcdError

func (*FakeEtcdClient) Set

func (f *FakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, error)

func (*FakeEtcdClient) SetError added in v0.3.2

func (f *FakeEtcdClient) SetError(err error)

func (*FakeEtcdClient) WaitForWatchCompletion

func (f *FakeEtcdClient) WaitForWatchCompletion()

func (*FakeEtcdClient) Watch

func (f *FakeEtcdClient) Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)

type FilterFunc

type FilterFunc func(obj runtime.Object) bool

FilterFunc is a predicate which takes an API object and returns true iff the object should remain in the set.

type RuntimeVersionAdapter

type RuntimeVersionAdapter struct {
	Versioner runtime.ResourceVersioner
}

RuntimeVersionAdapter converts a string based versioner to EtcdResourceVersioner

func (RuntimeVersionAdapter) ResourceVersion

func (a RuntimeVersionAdapter) ResourceVersion(obj runtime.Object) (uint64, error)

SetResourceVersion implements EtcdResourceVersioner

func (RuntimeVersionAdapter) SetResourceVersion

func (a RuntimeVersionAdapter) SetResourceVersion(obj runtime.Object, version uint64) error

SetResourceVersion implements EtcdResourceVersioner

type TestLogger

type TestLogger interface {
	Fatalf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Logf(format string, args ...interface{})
}

TestLogger is a type passed to Test functions to support formatted test logs.

type TransformFunc

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

TransformFunc attempts to convert an object to another object for use with a watcher.

Jump to

Keyboard shortcuts

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