consul

package
v0.0.0-...-8223eb1 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2020 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

package consul provides a generalized API for reading and writing pod manifests in consul.

Index

Constants

View Source
const (
	TTL = 60 * time.Second

	// Don't change this, it affects where status keys are read and written from
	PreparerPodStatusNamespace statusstore.Namespace = "preparer"
	RCStatusNamespace          statusstore.Namespace = "replication_controller"
)

Healthcheck TTL

Variables

View Source
var (
	// HealthRetryTimeSec determines how long to wait between retries when a health check
	// fails to write.
	HealthRetryTimeSec = param.Int("health_retry_time_sec", 5)

	// SessionTTLSec sets the TTL time for each session created by consulHealthManager. This
	// parameter controls how long it takes for clients to notice that health checks have
	// stopped.
	SessionTTLSec = param.Int("health_session_ttl_sec", 15)

	// HealthWritesPerMinute sets the average number of writes per minute per service that
	// will be sent to Consul to update health.
	HealthWritesPerMinute = param.Int("health_writes_per_minute", 4)

	// HealthMaxBucketSize sets the maximum token bucket size per service used to
	// rate-limit Consul writes.
	HealthMaxBucketSize = param.Int64("health_max_bucket_size", 16)

	// HealthResumeLimit sets the lower bound on the number of tokens at which updates will
	// be resumed. If a service runs out of tokens, its last update will be to set the
	// health status to "unknown" with an error message, and further updates will be
	// throttled until enough tokens have been accumulated.
	HealthResumeLimit = param.Int64("health_resume_limit", 4)
)

Functions

func HealthPath

func HealthPath(service string, node types.NodeName) string

func IsAlreadyLocked

func IsAlreadyLocked(err error) bool

func NewConsulClient

func NewConsulClient(opts Options) consulutil.ConsulClient

func NewConsulStore

func NewConsulStore(client consulutil.ConsulClient) *consulStore

func PodLockPath

func PodLockPath(podPrefix PodPrefix, nodeName types.NodeName, podId types.PodID) (string, error)

Returns the consul path to use when intending to lock a pod, e.g. lock/intent/some_host/some_pod

func PodPath

func PodPath(podPrefix PodPrefix, nodeName types.NodeName, podId types.PodID) (string, error)

func PodUniqueKeyFromConsulPath

func PodUniqueKeyFromConsulPath(consulPath string) (types.PodUniqueKey, error)

Deduces a PodUniqueKey from a consul path. This is useful as pod keys are transitioned from using node name and pod ID to using UUIDs. Input is expected to have 3 '/' separated sections, e.g. 'intent/<node>/<pod_id>' or 'intent/<node>/<pod_uuid>' if the prefix is "intent" or "reality"

/hooks is also a valid pod prefix and the key under it will not be a uuid.

func ReplicationLockPath

func ReplicationLockPath(podId types.PodID) string

Returns the consul path to use when locking out any other pkg/replication-based deploys for a given pod ID

Types

type AlreadyLockedError

type AlreadyLockedError struct {
	Key string
}

func (AlreadyLockedError) Error

func (err AlreadyLockedError) Error() string

type ConsulKVClient

type ConsulKVClient interface {
	Delete(key string, w *api.WriteOptions) (*api.WriteMeta, error)
	Acquire(p *api.KVPair, q *api.WriteOptions) (bool, *api.WriteMeta, error)
	Put(pair *api.KVPair, w *api.WriteOptions) (*api.WriteMeta, error)
}

type HealthManager

type HealthManager interface {
	// NewUpdater creates a new object for publishing a single service's health. Each
	// service should have its own updater.
	NewUpdater(pod types.PodID, service string) HealthUpdater

	// Close removes all published health statuses and releases all manager resources.
	Close()
}

HealthManager manages a collection of health checks that share configuration and resources.

type HealthUpdater

type HealthUpdater interface {
	// PutHealth updates the health status of the app. Checkers are free to call it after
	// every health check or other status change.
	PutHealth(health WatchResult) error

	// Close removes a service's health check and releases all updater resources. Call this
	// when no more health statuses will be published.
	Close()
}

HealthUpdater allows an app's health to be updated.

type ManifestResult

type ManifestResult struct {
	Manifest    manifest.Manifest
	PodLocation types.PodLocation

	// This is expected to be nil for "legacy" pods that do not have a uuid, and non-nil for uuid pods.
	PodUniqueKey types.PodUniqueKey
}

type Options

type Options struct {
	// The hostname and port of Consul (eg "example.com:8500"), or a unix socket
	// (eg "unix:///var/run/consul.sock"). The empty string defaults to
	// "127.0.0.1:8500".
	Address string
	// Set to true to use HTTPS.
	HTTPS bool
	// The ACL token to pass to Consul.
	Token string
	// If non-nil, this http.Client will be used for Consul communication.
	Client *http.Client
	// If provided, the wait time to be used on queries from this client.
	// See the "wait" parameter:
	// https://consul.io/intro/getting-started/kv.html
	WaitTime time.Duration
}

type PodPrefix

type PodPrefix string

PodPrefix represents the top level of a subtree whose leaves are expected to be pods, e.g. "hooks", "intent", "reality"

const (
	INTENT_TREE  PodPrefix = "intent"
	REALITY_TREE PodPrefix = "reality"
	HOOK_TREE    PodPrefix = "hooks"
	LOCK_TREE              = "lock"
)

func (PodPrefix) String

func (p PodPrefix) String() string

type PodStatusStore

type PodStatusStore interface {
	GetStatusFromIndex(index podstore.PodIndex) (podstatus.PodStatus, *api.QueryMeta, error)
}

type Session

type Session interface {
	Lock(key string) (Unlocker, error)
	LockTxn(
		lockCtx context.Context,
		key string,
	) (TxnUnlocker, error)
	UnlockTxn(
		ctx context.Context,
		key string,
		value []byte,
	) error
	LockIfKeyNotExistsTxn(
		ctx context.Context,
		key string,
		value []byte,
	) (TxnUnlocker, error)
	Renew() error
	Destroy() error
	Session() string
}

Represents a session that can be used to lock keys in the KV store. The only current implementation wraps consul sessions, which can be used to obtain locks on multiple keys, and must be periodically renewed.

func NewManagedSession

func NewManagedSession(client consulutil.ConsulClient, sessionID string, name string, quitCh chan struct{}, renewalErrCh chan error, renewalCh <-chan time.Time) Session

TODO: this should really use RenewPeriodic() instead of continuallyRenew() because it properly handles the server changing the session TTL to put backpressure on clients during high load.

func NewSession

func NewSession(client consulutil.ConsulClient, name string, renewalCh <-chan time.Time) (Session, chan error, error)

func NewUnmanagedSession

func NewUnmanagedSession(client consulutil.ConsulClient, sessionID, name string) Session

Creates a Session struct using an existing consul session, and does not set up auto-renewal. Use this constructor when the underlying session already exists and should not be managed here.

func SessionContext

func SessionContext(ctx context.Context, client consulutil.ConsulClient, name string) (context.Context, Session, error)

SessionContext creates a consul session and keeps it renewed until either a renewal error occurs or the passed context is canceled. If a renewal error occurs, it will cancel the output context

type TxnUnlocker

type TxnUnlocker interface {
	// UnlockTxn adds consul operations to the transaction within the passed
	// context to unlock the lock. The caller must call transaction.Commit() with
	// the passed context for the lock to actually be released
	UnlockTxn(ctx context.Context) error

	// CheckLockedTxn adds consul operations to the transaction within the passed
	// context to check that the lock is still held. This is useful for callers
	// that wish for the operations they are attempting to fail if the lock is no
	// longer held for any reason
	CheckLockedTxn(ctx context.Context) error
}

TxnUnlocker represents a lock that is held in consul, with convenience functions for operating with the lock

type Unlocker

type Unlocker interface {
	Unlock() error
	Key() string
	DestroySession() error
}

type WatchResult

type WatchResult struct {
	Id      types.PodID
	Node    types.NodeName
	Service string
	Status  string
	Time    time.Time
	Expires time.Time `json:"Expires,omitempty"`
}

func (WatchResult) IsStale

func (r WatchResult) IsStale() bool

IsStale returns true when the result is stale according to the local clock.

func (WatchResult) ValueEquiv

func (r WatchResult) ValueEquiv(s WatchResult) bool

ValueEquiv returns true if the value of the WatchResult--everything except the timestamps--is equivalent to another WatchResult.

Directories

Path Synopsis
package consulutil contains common routines for setting up a live Consul server for use in unit tests.
package consulutil contains common routines for setting up a live Consul server for use in unit tests.
Package flags provides frequently used kingpin flags for command-line tools that connect to Consul.
Package flags provides frequently used kingpin flags for command-line tools that connect to Consul.
TODO: update this comment as things change.
TODO: update this comment as things change.
Package transaction provides an interface for crafting transactional updates to consul.
Package transaction provides an interface for crafting transactional updates to consul.

Jump to

Keyboard shortcuts

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