agent

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MPL-2.0 Imports: 34 Imported by: 24

Documentation

Index

Constants

View Source
const (
	MinIPCVersion = 1
	MaxIPCVersion = 1
)
View Source
const DefaultBindPort int = 7946

This is the default port that we use for Serf communication

Variables

This section is empty.

Functions

func LevelFilter

func LevelFilter() *logutils.LevelFilter

LevelFilter returns a LevelFilter that is configured with the log levels that we use.

func MarshalTags

func MarshalTags(tags map[string]string) []string

MarshalTags is a utility function which takes a map of tag key/value pairs and returns the same tags as strings in 'key=value' format.

func NewLogWriter

func NewLogWriter(buf int) *logWriter

NewLogWriter creates a logWriter with the given buffer capacity

func UnmarshalTags

func UnmarshalTags(tags []string) (map[string]string, error)

UnmarshalTags is a utility function which takes a slice of strings in key=value format and returns them as a tag mapping.

func ValidateLevelFilter

func ValidateLevelFilter(minLevel logutils.LogLevel, filter *logutils.LevelFilter) bool

ValidateLevelFilter verifies that the log levels within the filter are valid.

Types

type Agent

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

Agent starts and manages a Serf instance, adding some niceties on top of Serf such as storing logs that you can later retrieve, and invoking EventHandlers when events occur.

func Create

func Create(agentConf *Config, conf *serf.Config, logOutput io.Writer) (*Agent, error)

Create creates a new agent, potentially returning an error

func (*Agent) DeregisterEventHandler

func (a *Agent) DeregisterEventHandler(eh EventHandler)

DeregisterEventHandler removes an EventHandler and prevents more invocations

func (*Agent) ForceLeave

func (a *Agent) ForceLeave(node string) error

ForceLeave is used to eject a failed node from the cluster

func (*Agent) ForceLeavePrune added in v0.8.4

func (a *Agent) ForceLeavePrune(node string) error

ForceLeavePrune completely removes a failed node from the member list entirely

func (*Agent) InstallKey

func (a *Agent) InstallKey(key string) (*serf.KeyResponse, error)

InstallKey initiates a query to install a new key on all members

func (*Agent) Join

func (a *Agent) Join(addrs []string, replay bool) (n int, err error)

Join asks the Serf instance to join. See the Serf.Join function.

func (*Agent) Leave

func (a *Agent) Leave() error

Leave prepares for a graceful shutdown of the agent and its processes

func (*Agent) ListKeys

func (a *Agent) ListKeys() (*serf.KeyResponse, error)

ListKeys sends a query to all members to return a list of their keys

func (*Agent) Query

func (a *Agent) Query(name string, payload []byte, params *serf.QueryParam) (*serf.QueryResponse, error)

Query sends a Query on Serf, see Serf.Query.

func (*Agent) RegisterEventHandler

func (a *Agent) RegisterEventHandler(eh EventHandler)

RegisterEventHandler adds an event handler to receive event notifications

func (*Agent) RemoveKey

func (a *Agent) RemoveKey(key string) (*serf.KeyResponse, error)

RemoveKey sends a query to all members to remove a key from the keyring

func (*Agent) Serf

func (a *Agent) Serf() *serf.Serf

Returns the Serf agent of the running Agent.

func (*Agent) SerfConfig

func (a *Agent) SerfConfig() *serf.Config

Returns the Serf config of the running Agent.

func (*Agent) SetTags

func (a *Agent) SetTags(tags map[string]string) error

SetTags is used to update the tags. The agent will make sure to persist tags if necessary before gossiping to the cluster.

func (*Agent) Shutdown

func (a *Agent) Shutdown() error

Shutdown closes this agent and all of its processes. Should be preceded by a Leave for a graceful shutdown.

func (*Agent) ShutdownCh

func (a *Agent) ShutdownCh() <-chan struct{}

ShutdownCh returns a channel that can be selected to wait for the agent to perform a shutdown.

func (*Agent) Start

func (a *Agent) Start() error

Start is used to initiate the event listeners. It is separate from create so that there isn't a race condition between creating the agent and registering handlers

func (*Agent) Stats

func (a *Agent) Stats() map[string]map[string]string

Stats is used to get various runtime information and stats

func (*Agent) UseKey

func (a *Agent) UseKey(key string) (*serf.KeyResponse, error)

UseKey sends a query instructing all members to switch primary keys

func (*Agent) UserEvent

func (a *Agent) UserEvent(name string, payload []byte, coalesce bool) error

UserEvent sends a UserEvent on Serf, see Serf.UserEvent.

type AgentIPC

type AgentIPC struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewAgentIPC

func NewAgentIPC(agent *Agent, authKey string, listener net.Listener,
	logOutput io.Writer, logWriter *logWriter) *AgentIPC

NewAgentIPC is used to create a new Agent IPC handler

func (*AgentIPC) Shutdown

func (i *AgentIPC) Shutdown()

Shutdown is used to shutdown the IPC layer

type AgentMDNS

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

AgentMDNS is used to advertise ourself using mDNS and to attempt to join peers periodically using mDNS queries.

func NewAgentMDNS

func NewAgentMDNS(agent *Agent, logOutput io.Writer, replay bool,
	node, discover string, iface *net.Interface, bind net.IP, port int) (*AgentMDNS, error)

NewAgentMDNS is used to create a new AgentMDNS

type AppendSliceValue

type AppendSliceValue []string

AppendSliceValue implements the flag.Value interface and allows multiple calls to the same variable to append a list.

func (*AppendSliceValue) Set

func (s *AppendSliceValue) Set(value string) error

func (*AppendSliceValue) String

func (s *AppendSliceValue) String() string

type Command

type Command struct {
	Ui         cli.Ui
	ShutdownCh <-chan struct{}
	// contains filtered or unexported fields
}

Command is a Command implementation that runs a Serf agent. The command will not end unless a shutdown message is sent on the ShutdownCh. If two messages are sent on the ShutdownCh it will forcibly exit.

func (*Command) Help

func (c *Command) Help() string

func (*Command) Run

func (c *Command) Run(args []string) int

func (*Command) Synopsis

func (c *Command) Synopsis() string

type Config

type Config struct {
	// All the configurations in this section are identical to their
	// Serf counterparts. See the documentation for Serf.Config for
	// more info.
	NodeName           string `mapstructure:"node_name"`
	Role               string `mapstructure:"role"`
	DisableCoordinates bool   `mapstructure:"disable_coordinates"`

	// Tags are used to attach key/value metadata to a node. They have
	// replaced 'Role' as a more flexible meta data mechanism. For compatibility,
	// the 'role' key is special, and is used for backwards compatibility.
	Tags map[string]string `mapstructure:"tags"`

	// TagsFile is the path to a file where Serf can store its tags. Tag
	// persistence is desirable since tags may be set or deleted while the
	// agent is running. Tags can be reloaded from this file on later starts.
	TagsFile string `mapstructure:"tags_file"`

	// BindAddr is the address that the Serf agent's communication ports
	// will bind to. Serf will use this address to bind to for both TCP
	// and UDP connections. If no port is present in the address, the default
	// port will be used.
	BindAddr string `mapstructure:"bind"`

	// AdvertiseAddr is the address that the Serf agent will advertise to
	// other members of the cluster. Can be used for basic NAT traversal
	// where both the internal ip:port and external ip:port are known.
	AdvertiseAddr string `mapstructure:"advertise"`

	// EncryptKey is the secret key to use for encrypting communication
	// traffic for Serf. The secret key must be exactly 32-bytes, base64
	// encoded. The easiest way to do this on Unix machines is this command:
	// "head -c32 /dev/urandom | base64". If this is not specified, the
	// traffic will not be encrypted.
	EncryptKey string `mapstructure:"encrypt_key"`

	// KeyringFile is the path to a file containing a serialized keyring.
	// The keyring is used to facilitate encryption. If left blank, the
	// keyring will not be persisted to a file.
	KeyringFile string `mapstructure:"keyring_file"`

	// LogLevel is the level of the logs to output.
	// This can be updated during a reload.
	LogLevel string `mapstructure:"log_level"`

	// RPCAddr is the address and port to listen on for the agent's RPC
	// interface.
	RPCAddr string `mapstructure:"rpc_addr"`

	// RPCAuthKey is a key that can be set to optionally require that
	// RPC's provide an authentication key. This is meant to be
	// a very simple authentication control
	RPCAuthKey string `mapstructure:"rpc_auth"`

	// Protocol is the Serf protocol version to use.
	Protocol int `mapstructure:"protocol"`

	// ReplayOnJoin tells Serf to replay past user events
	// when joining based on a `StartJoin`.
	ReplayOnJoin bool `mapstructure:"replay_on_join"`

	// QueryResponseSizeLimit and QuerySizeLimit limit the inbound and
	// outbound payload sizes for queries, respectively. These must fit
	// in a UDP packet with some additional overhead, so tuning these
	// past the default values of 1024 will depend on your network
	// configuration.
	QueryResponseSizeLimit int `mapstructure:"query_response_size_limit"`
	QuerySizeLimit         int `mapstructure:"query_size_limit"`

	// UserEventSizeLimit is maximum byte size limit of user event `name` + `payload` in bytes.
	// It's optimal to be relatively small, since it's going to be gossiped through the cluster.
	UserEventSizeLimit int `mapstructure:"user_event_size_limit"`

	// StartJoin is a list of addresses to attempt to join when the
	// agent starts. If Serf is unable to communicate with any of these
	// addresses, then the agent will error and exit.
	StartJoin []string `mapstructure:"start_join"`

	// EventHandlers is a list of event handlers that will be invoked.
	// These can be updated during a reload.
	EventHandlers []string `mapstructure:"event_handlers"`

	// Profile is used to select a timing profile for Serf. The supported choices
	// are "wan", "lan", and "local". The default is "lan"
	Profile string `mapstructure:"profile"`

	// SnapshotPath is used to allow Serf to snapshot important transactional
	// state to make a more graceful recovery possible. This enables auto
	// re-joining a cluster on failure and avoids old message replay.
	SnapshotPath string `mapstructure:"snapshot_path"`

	// LeaveOnTerm controls if Serf does a graceful leave when receiving
	// the TERM signal. Defaults false. This can be changed on reload.
	LeaveOnTerm bool `mapstructure:"leave_on_terminate"`

	// SkipLeaveOnInt controls if Serf skips a graceful leave when receiving
	// the INT signal. Defaults false. This can be changed on reload.
	SkipLeaveOnInt bool `mapstructure:"skip_leave_on_interrupt"`

	// Discover is used to setup an mDNS Discovery name. When this is set, the
	// agent will setup an mDNS responder and periodically run an mDNS query
	// to look for peers. For peers on a network that supports multicast, this
	// allows Serf agents to join each other with zero configuration.
	Discover string `mapstructure:"discover"`

	// Interface is used to provide a binding interface to use. It can be
	// used instead of providing a bind address, as Serf will discover the
	// address of the provided interface. It is also used to set the multicast
	// device used with `-discover`.
	Interface string `mapstructure:"interface"`

	// ReconnectIntervalRaw is the string reconnect interval time. This interval
	// controls how often we attempt to connect to a failed node.
	ReconnectIntervalRaw string        `mapstructure:"reconnect_interval"`
	ReconnectInterval    time.Duration `mapstructure:"-"`

	// ReconnectTimeoutRaw is the string reconnect timeout. This timeout controls
	// for how long we attempt to connect to a failed node before removing
	// it from the cluster.
	ReconnectTimeoutRaw string        `mapstructure:"reconnect_timeout"`
	ReconnectTimeout    time.Duration `mapstructure:"-"`

	// TombstoneTimeoutRaw is the string tombstone timeout. This timeout controls
	// for how long we remember a left node before removing it from the cluster.
	TombstoneTimeoutRaw string        `mapstructure:"tombstone_timeout"`
	TombstoneTimeout    time.Duration `mapstructure:"-"`

	// By default Serf will attempt to resolve name conflicts. This is done by
	// determining which node the majority believe to be the proper node, and
	// by having the minority node shutdown. If you want to disable this behavior,
	// then this flag can be set to true.
	DisableNameResolution bool `mapstructure:"disable_name_resolution"`

	// EnableSyslog is used to also tee all the logs over to syslog. Only supported
	// on linux and OSX. Other platforms will generate an error.
	EnableSyslog bool `mapstructure:"enable_syslog"`

	// SyslogFacility is used to control which syslog facility messages are
	// sent to. Defaults to LOCAL0.
	SyslogFacility string `mapstructure:"syslog_facility"`

	// RetryJoin is a list of addresses to attempt to join when the
	// agent starts. Serf will continue to retry the join until it
	// succeeds or RetryMaxAttempts is reached.
	RetryJoin []string `mapstructure:"retry_join"`

	// RetryMaxAttempts is used to limit the maximum attempts made
	// by RetryJoin to reach other nodes. If this is 0, then no limit
	// is imposed, and Serf will continue to try forever. Defaults to 0.
	RetryMaxAttempts int `mapstructure:"retry_max_attempts"`

	// RetryIntervalRaw is the string retry interval. This interval
	// controls how often we retry the join for RetryJoin. This defaults
	// to 30 seconds.
	RetryIntervalRaw string        `mapstructure:"retry_interval"`
	RetryInterval    time.Duration `mapstructure:"-"`

	// RejoinAfterLeave controls our interaction with the snapshot file.
	// When set to false (default), a leave causes a Serf to not rejoin
	// the cluster until an explicit join is received. If this is set to
	// true, we ignore the leave, and rejoin the cluster on start. This
	// only has an affect if the snapshot file is enabled.
	RejoinAfterLeave bool `mapstructure:"rejoin_after_leave"`

	// EnableCompression specifies whether message compression is enabled
	// by `github.com/hashicorp/memberlist` when broadcasting events.
	EnableCompression bool `mapstructure:"enable_compression"`

	// StatsiteAddr is the address of a statsite instance. If provided,
	// metrics will be streamed to that instance.
	StatsiteAddr string `mapstructure:"statsite_addr"`

	// StatsdAddr is the address of a statsd instance. If provided,
	// metrics will be sent to that instance.
	StatsdAddr string `mapstructure:"statsd_addr"`

	// BroadcastTimeoutRaw is the string retry interval. This interval
	// controls the timeout for broadcast events. This defaults to
	// 5 seconds.
	BroadcastTimeoutRaw string        `mapstructure:"broadcast_timeout"`
	BroadcastTimeout    time.Duration `mapstructure:"-"`

	// ValidateNodeNames controls whether nodenames only
	// contain alphanumeric, dashes and '.'characters
	// and sets maximum length to 128 characters
	ValidateNodeNames bool `mapstructure:"validate_node_names"`
}

Config is the configuration that can be set for an Agent. Some of these configurations are exposed as command-line flags to `serf agent`, whereas many of the more advanced configurations can only be set by creating a configuration file.

func DecodeConfig

func DecodeConfig(r io.Reader) (*Config, error)

DecodeConfig reads the configuration from the given reader in JSON format and decodes it into a proper Config structure.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig contains the defaults for configurations.

func MergeConfig

func MergeConfig(a, b *Config) *Config

MergeConfig merges two configurations together to make a single new configuration.

func ReadConfigPaths

func ReadConfigPaths(paths []string) (*Config, error)

ReadConfigPaths reads the paths in the given order to load configurations. The paths can be to files or directories. If the path is a directory, we read one directory deep and read any files ending in ".json" as configuration files.

func (*Config) AddrParts

func (c *Config) AddrParts(address string) (string, int, error)

BindAddrParts returns the parts of the BindAddr that should be used to configure Serf.

func (*Config) EncryptBytes

func (c *Config) EncryptBytes() ([]byte, error)

EncryptBytes returns the encryption key configured.

func (*Config) EventScripts

func (c *Config) EventScripts() []EventScript

EventScripts returns the list of EventScripts associated with this configuration and specified by the "event_handlers" configuration.

func (*Config) NetworkInterface

func (c *Config) NetworkInterface() (*net.Interface, error)

Networkinterface is used to get the associated network interface from the configured value

type EventFilter

type EventFilter struct {
	Event string
	Name  string
}

EventFilter is used to filter which events are processed

func ParseEventFilter

func ParseEventFilter(v string) []EventFilter

ParseEventFilter a string with the event type filters and parses it into a series of EventFilters if it can.

func (*EventFilter) Invoke

func (s *EventFilter) Invoke(e serf.Event) bool

Invoke tests whether or not this event script should be invoked for the given Serf event.

func (*EventFilter) Valid

func (s *EventFilter) Valid() bool

Valid checks if this is a valid agent event script.

type EventHandler

type EventHandler interface {
	HandleEvent(serf.Event)
}

EventHandler is a handler that does things when events happen.

type EventScript

type EventScript struct {
	EventFilter
	Script string
}

EventScript is a single event script that will be executed in the case of an event, and is configured from the command-line or from a configuration file.

func ParseEventScript

func ParseEventScript(v string) []EventScript

ParseEventScript takes a string in the format of "type=script" and parses it into an EventScript struct, if it can.

func (*EventScript) String

func (s *EventScript) String() string

type GatedWriter

type GatedWriter struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

GatedWriter is an io.Writer implementation that buffers all of its data into an internal buffer until it is told to let data through.

func (*GatedWriter) Flush

func (w *GatedWriter) Flush()

Flush tells the GatedWriter to flush any buffered data and to stop buffering.

func (*GatedWriter) Write

func (w *GatedWriter) Write(p []byte) (n int, err error)

type IPCClient

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

func (*IPCClient) RegisterQuery

func (c *IPCClient) RegisterQuery(q *serf.Query) uint64

RegisterQuery is used to register a pending query that may get a response. The ID of the query is returned

func (*IPCClient) Send

func (c *IPCClient) Send(header *responseHeader, obj interface{}) error

send is used to send an object using the MsgPack encoding. send is serialized to prevent write overlaps, while properly buffering.

func (*IPCClient) String

func (c *IPCClient) String() string

type LogHandler

type LogHandler interface {
	HandleLog(string)
}

LogHandler interface is used for clients that want to subscribe to logs, for example to stream them over an IPC mechanism

type Member

type Member struct {
	Name        string
	Addr        net.IP
	Port        uint16
	Tags        map[string]string
	Status      string
	ProtocolMin uint8
	ProtocolMax uint8
	ProtocolCur uint8
	DelegateMin uint8
	DelegateMax uint8
	DelegateCur uint8
}

type MockEventHandler

type MockEventHandler struct {
	Events []serf.Event
	sync.Mutex
}

MockEventHandler is an EventHandler implementation that can be used for tests.

func (*MockEventHandler) HandleEvent

func (h *MockEventHandler) HandleEvent(e serf.Event)

type MockQueryHandler

type MockQueryHandler struct {
	Response []byte
	Queries  []*serf.Query
	sync.Mutex
}

MockQueryHandler is an EventHandler implementation used for tests, it always responds to a query with a given response

func (*MockQueryHandler) HandleEvent

func (h *MockQueryHandler) HandleEvent(e serf.Event)

type ScriptEventHandler

type ScriptEventHandler struct {
	SelfFunc func() serf.Member
	Scripts  []EventScript
	Logger   *log.Logger
	// contains filtered or unexported fields
}

ScriptEventHandler invokes scripts for the events that it receives.

func (*ScriptEventHandler) HandleEvent

func (h *ScriptEventHandler) HandleEvent(e serf.Event)

func (*ScriptEventHandler) UpdateScripts

func (h *ScriptEventHandler) UpdateScripts(scripts []EventScript)

UpdateScripts is used to safely update the scripts we invoke in a thread safe manner

type SyslogWrapper

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

SyslogWrapper is used to cleaup log messages before writing them to a Syslogger. Implements the io.Writer interface.

func (*SyslogWrapper) Write

func (s *SyslogWrapper) Write(p []byte) (int, error)

Write is used to implement io.Writer

Jump to

Keyboard shortcuts

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