service

package
v0.0.0-...-e7128a5 Latest Latest
Warning

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

Go to latest
Published: May 20, 2019 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertMapStringStringToURLValues

func ConvertMapStringStringToURLValues(params map[string]string) url.Values

ConvertMapStringStringToURLValues converts params to `url.Values`

func EqualMapStringString

func EqualMapStringString(l map[string]string, r map[string]string) bool

EqualMapStringString Returns true when the two maps are equal

func EqualNodeIPSet

func EqualNodeIPSet(l NodeIPSet, r NodeIPSet) bool

EqualNodeIPSet returns true when NodeIPSets contain the same elements

func GetNodeMiniCreateParameters

func GetNodeMiniCreateParameters(node NodeMini) map[string]string

GetNodeMiniCreateParameters converts `NodeMini` into parameters

func GetNodeMiniRemoveParameters

func GetNodeMiniRemoveParameters(node NodeMini) map[string]string

GetNodeMiniRemoveParameters converts `NodeMini` into remove parameters

func GetSwarmServiceMiniCreateParameters

func GetSwarmServiceMiniCreateParameters(ssm SwarmServiceMini) map[string]string

GetSwarmServiceMiniCreateParameters converts `SwarmServiceMini` into parameters

func GetSwarmServiceMiniRemoveParameters

func GetSwarmServiceMiniRemoveParameters(ssm SwarmServiceMini) map[string]string

GetSwarmServiceMiniRemoveParameters converts `SwarmServiceMini` into remove parameters

func GetTaskList

func GetTaskList(ctx context.Context, client *client.Client, serviceID string) ([]swarm.Task, error)

GetTaskList returns tasks when it is the service is converged

func NewDockerClientFromEnv

func NewDockerClientFromEnv() (*client.Client, error)

NewDockerClientFromEnv returns a `*client.Client` struct using environment variable `DF_DOCKER_HOST` for the host

func TasksAllRunning

func TasksAllRunning(ctx context.Context, cli *client.Client, serviceID string) (bool, error)

TasksAllRunning checks if a service is currently up and running

Types

type CancelManager

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

CancelManager implements the `CancelManaging` interface that is thread safe

func NewCancelManager

func NewCancelManager() *CancelManager

NewCancelManager creates a new `CancelManager`

func (*CancelManager) Add

func (m *CancelManager) Add(rootCtx context.Context, id string, reqID int64) context.Context

Add creates an context for `id` and `reqID` and returns that context. If `id` exists in memory, the task with that `id` will be canceled.

func (*CancelManager) Delete

func (m *CancelManager) Delete(id string, reqID int64) bool

Delete calls cancel context with the corresponding `id` and `reqID` and removes 'id' from map If the corresponding `id` and `reqID` are not present, Delete does nothing. In all cases, Delete returns true if an item was deleted

type CancelManaging

type CancelManaging interface {
	Add(rootCtx context.Context, id string, reqID int64) context.Context
	Delete(id string, reqID int64) bool
}

CancelManaging manages canceling of contexts

type Event

type Event struct {
	Type         EventType
	ID           string
	TimeNano     int64
	ConsultCache bool
}

Event contains information about docker events

type EventType

type EventType string

EventType is the type of event from eventlisteners

const (
	// EventTypeCreate is for create or update event
	EventTypeCreate EventType = "create"
	// EventTypeRemove is for remove events
	EventTypeRemove EventType = "remove"
)

type NodeCache

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

NodeCache implements `NodeCacher` Not threadsafe!

func NewNodeCache

func NewNodeCache() *NodeCache

NewNodeCache creates a new `NewNodeCache`

func (*NodeCache) Delete

func (c *NodeCache) Delete(ID string)

Delete removes node from cache

func (*NodeCache) Get

func (c *NodeCache) Get(ID string) (NodeMini, bool)

Get gets node from cache

func (*NodeCache) InsertAndCheck

func (c *NodeCache) InsertAndCheck(n NodeMini) bool

InsertAndCheck inserts `NodeMini` into cache If the node is new or updated `InsertAndCheck` returns true.

func (*NodeCache) IsNewOrUpdated

func (c *NodeCache) IsNewOrUpdated(n NodeMini) bool

IsNewOrUpdated returns true if node is new or updated

func (*NodeCache) Keys

func (c *NodeCache) Keys() map[string]struct{}

Keys return the keys of the cache

type NodeCacher

type NodeCacher interface {
	InsertAndCheck(n NodeMini) bool
	IsNewOrUpdated(n NodeMini) bool
	Delete(ID string)
	Get(ID string) (NodeMini, bool)
	Keys() map[string]struct{}
}

NodeCacher caches sevices

type NodeClient

type NodeClient struct {
	DockerClient *client.Client
}

NodeClient implementes `NodeInspector` for docker

func NewNodeClient

func NewNodeClient(c *client.Client) *NodeClient

NewNodeClient creates a `NodeClient`

func (NodeClient) NodeInspect

func (c NodeClient) NodeInspect(nodeID string) (swarm.Node, error)

NodeInspect returns `swarm.Node` from its ID

func (NodeClient) NodeList

func (c NodeClient) NodeList(ctx context.Context) ([]swarm.Node, error)

NodeList returns a list of all nodes

type NodeIP

type NodeIP struct {
	Name string `json:"name"`
	Addr string `json:"addr"`
	ID   string `json:"id"`
}

NodeIP defines a node/addr pair

type NodeIPSet

type NodeIPSet map[NodeIP]struct{}

NodeIPSet is a set of NodeIPs

func (NodeIPSet) Add

func (ns NodeIPSet) Add(name, addr, id string)

Add node to set

func (NodeIPSet) Cardinality

func (ns NodeIPSet) Cardinality() int

Cardinality returns the size of set

func (NodeIPSet) MarshalJSON

func (ns NodeIPSet) MarshalJSON() ([]byte, error)

MarshalJSON creates JSON array from NodeIPSet

func (*NodeIPSet) UnmarshalJSON

func (ns *NodeIPSet) UnmarshalJSON(b []byte) error

UnmarshalJSON recreates NodeIPSet from a JSON array

type NodeInspector

type NodeInspector interface {
	NodeInspect(nodeID string) (swarm.Node, error)
	NodeList(ctx context.Context) ([]swarm.Node, error)
}

NodeInspector is able to inspect a swarm node

type NodeListener

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

NodeListener listens for docker node events

func NewNodeListener

func NewNodeListener(c *client.Client, logger *log.Logger) *NodeListener

NewNodeListener creates a `NodeListener“

func (NodeListener) ListenForNodeEvents

func (s NodeListener) ListenForNodeEvents(
	eventChan chan<- Event)

ListenForNodeEvents listens for events and places them on channels

type NodeListening

type NodeListening interface {
	ListenForNodeEvents(eventChan chan<- Event)
}

NodeListening listens to node events

type NodeMini

type NodeMini struct {
	ID           string
	Hostname     string
	VersionIndex uint64
	State        swarm.NodeState
	Addr         string
	NodeLabels   map[string]string
	EngineLabels map[string]string
	Role         swarm.NodeRole
	Availability swarm.NodeAvailability
}

NodeMini is a optimized version of `swarm.Node` for caching purposes

func MinifyNode

func MinifyNode(n swarm.Node) NodeMini

MinifyNode minifies `swarm.Node` only labels prefixed with `com.df.` will be used

func (NodeMini) Equal

func (ns NodeMini) Equal(other NodeMini) bool

Equal returns true when NodeMini is equal to `other`

type NodePoller

type NodePoller struct {
	Client          NodeInspector
	Cache           NodeCacher
	PollingInterval int
	MinifyFunc      func(swarm.Node) NodeMini
	Log             *log.Logger
}

NodePoller implements `NodePolling`

func NewNodePoller

func NewNodePoller(
	client NodeInspector,
	cache NodeCacher,
	pollingInterval int,
	minifyFunc func(swarm.Node) NodeMini,
	log *log.Logger,
) *NodePoller

NewNodePoller creates a new `NodePoller`

func (NodePoller) Run

func (n NodePoller) Run(eventChan chan<- Event)

Run starts poller and places events onto `eventChan`

type NodePolling

type NodePolling interface {
	Run(eventChan chan<- Event)
}

NodePolling provides an interface for polling node changes

type Notification

type Notification struct {
	EventType  EventType
	ID         string
	Parameters string
	TimeNano   int64
	Context    context.Context
	ErrorChan  chan error
}

Notification is a node notification

type NotificationSender

type NotificationSender interface {
	Create(ctx context.Context, params string) error
	Remove(ctx context.Context, params string) error
	GetCreateAddr() string
	GetRemoveAddr() string
}

NotificationSender sends notifications to listeners

type Notifier

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

Notifier implements `NotificationSender`

func NewNotifier

func NewNotifier(
	createAddr, removeAddr, createHTTPMethod,
	removeHTTPMethod, notifyType string,
	retries int, interval int, logger *log.Logger) *Notifier

NewNotifier returns a `Notifier`

func (Notifier) Create

func (n Notifier) Create(ctx context.Context, params string) error

Create sends create notifications to listeners

func (Notifier) GetCreateAddr

func (n Notifier) GetCreateAddr() string

GetCreateAddr returns create addresses

func (Notifier) GetRemoveAddr

func (n Notifier) GetRemoveAddr() string

GetRemoveAddr returns create addresses

func (Notifier) Remove

func (n Notifier) Remove(ctx context.Context, params string) error

Remove sends remove notifications to listeners

type NotifyDistributing

type NotifyDistributing interface {
	Run(serviceChan <-chan Notification, nodeChan <-chan Notification)
	HasServiceListeners() bool
	HasNodeListeners() bool
}

NotifyDistributing takes a stream of `Notification` and NodeNotifiction and distributes it listeners

type NotifyDistributor

type NotifyDistributor struct {
	NotifyEndpoints      map[string]NotifyEndpoint
	ServiceCancelManager CancelManaging
	NodeCancelManager    CancelManaging
	// contains filtered or unexported fields
}

NotifyDistributor distributes service and node notifications to `NotifyEndpoints` `NotifyEndpoints` are keyed by hostname to send notifications to

func NewNotifyDistributorFromEnv

func NewNotifyDistributorFromEnv(retries, interval int,
	extraCreateServiceAddr, extraRemoveServiceAddr,
	extraCreateNodeAddr, extraRemoveNodeAddr string,
	logger *log.Logger) *NotifyDistributor

NewNotifyDistributorFromEnv creates `NotifyDistributor` from environment variables

func (NotifyDistributor) HasNodeListeners

func (d NotifyDistributor) HasNodeListeners() bool

HasNodeListeners when there exists node listeners

func (NotifyDistributor) HasServiceListeners

func (d NotifyDistributor) HasServiceListeners() bool

HasServiceListeners when there exists service listeners

func (NotifyDistributor) Run

func (d NotifyDistributor) Run(serviceChan <-chan Notification, nodeChan <-chan Notification)

Run starts the distributor

type NotifyEndpoint

type NotifyEndpoint struct {
	ServiceNotifier NotificationSender
	NodeNotifier    NotificationSender
}

NotifyEndpoint holds Notifiers and channels to watch

type NotifyType

type NotifyType string

NotifyType is the type of notification to send

type SwarmListener

type SwarmListener struct {
	SSListener SwarmServiceListening
	SSClient   SwarmServiceInspector
	SSCache    SwarmServiceCacher
	SSPoller   SwarmServicePolling

	SSEventChan         chan Event
	SSNotificationChan  chan Notification
	SSInternalEventChan chan Event

	NodeListener NodeListening
	NodeClient   NodeInspector
	NodeCache    NodeCacher
	NodePoller   NodePolling

	NodeEventChan        chan Event
	NodeNotificationChan chan Notification
	NodeInteralEventChan chan Event

	NotifyDistributor NotifyDistributing

	ServiceCancelManager           CancelManaging
	NodeCancelManager              CancelManaging
	IncludeNodeInfo                bool
	UseDockerServiceEvents         bool
	UseDockerNodeEvents            bool
	NotifyCreateServiceImmediately bool
	IgnoreKey                      string
	IncludeKey                     string
	HasServiceListeners            bool
	HasNodeListeners               bool
	Log                            *log.Logger

	StopServiceEventChan chan struct{}
	StopNodeEventChan    chan struct{}
}

SwarmListener provides public api

func NewSwarmListenerFromEnv

func NewSwarmListenerFromEnv(
	retries, interval, servicePollingInterval,
	nodePollingInterval int, logger *log.Logger) (*SwarmListener, error)

NewSwarmListenerFromEnv creats `SwarmListener` from environment variables

func (*SwarmListener) CompletelyNotifyServices

func (l *SwarmListener) CompletelyNotifyServices()

CompletelyNotifyServices stops event processing and sends out create AND remove notifications based on if the service is up, down. If the service is starting up and not up get, a remove notification is send, and a create service event is place on the event queue.

func (SwarmListener) GetNodesParameters

func (l SwarmListener) GetNodesParameters(ctx context.Context) ([]map[string]string, error)

GetNodesParameters get all nodes

func (SwarmListener) GetServicesParameters

func (l SwarmListener) GetServicesParameters(ctx context.Context) ([]map[string]string, error)

GetServicesParameters get all services

func (SwarmListener) NotifyNodes

func (l SwarmListener) NotifyNodes(consultCache bool)

NotifyNodes places all services on queue to notify serivces on node events

func (SwarmListener) NotifyServices

func (l SwarmListener) NotifyServices(consultCache bool)

NotifyServices places all services on queue to notify services on service events

func (*SwarmListener) Run

func (l *SwarmListener) Run()

Run starts swarm listener

type SwarmListening

type SwarmListening interface {
	Run()
	NotifyServices(consultCache bool)
	NotifyNodes(consultCache bool)
	GetServicesParameters(ctx context.Context) ([]map[string]string, error)
	GetNodesParameters(ctx context.Context) ([]map[string]string, error)
}

SwarmListening provides public api for interacting with swarm listener

type SwarmService

type SwarmService struct {
	swarm.Service
	NodeInfo NodeIPSet
}

SwarmService defines internal structure with service information

type SwarmServiceCache

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

SwarmServiceCache implements `SwarmServiceCacher`

func NewSwarmServiceCache

func NewSwarmServiceCache() *SwarmServiceCache

NewSwarmServiceCache creates a new `NewSwarmServiceCache`

func (*SwarmServiceCache) Delete

func (c *SwarmServiceCache) Delete(ID string)

Delete delets service from cache

func (*SwarmServiceCache) Get

Get gets service from cache

func (*SwarmServiceCache) InsertAndCheck

func (c *SwarmServiceCache) InsertAndCheck(ss SwarmServiceMini) bool

InsertAndCheck inserts `SwarmServiceMini` into cache If the service is new or updated `InsertAndCheck` returns true.

func (*SwarmServiceCache) IsNewOrUpdated

func (c *SwarmServiceCache) IsNewOrUpdated(ss SwarmServiceMini) bool

IsNewOrUpdated returns true if service is new or updated

func (*SwarmServiceCache) Keys

func (c *SwarmServiceCache) Keys() map[string]struct{}

Keys returns the keys of the cache

func (*SwarmServiceCache) Len

func (c *SwarmServiceCache) Len() int

Len returns the number of items in cache

type SwarmServiceCacher

type SwarmServiceCacher interface {
	InsertAndCheck(ss SwarmServiceMini) bool
	IsNewOrUpdated(ss SwarmServiceMini) bool
	Delete(ID string)
	Get(ID string) (SwarmServiceMini, bool)
	Len() int
	Keys() map[string]struct{}
}

SwarmServiceCacher caches sevices

type SwarmServiceClient

type SwarmServiceClient struct {
	DockerClient                 *client.Client
	FilterLabel                  string
	FilterKey                    string
	ScrapeNetLabel               string
	ServiceNamePrefix            string
	IncludeTaskAddressInNodeInfo bool
	Log                          *log.Logger
}

SwarmServiceClient implements `SwarmServiceInspector` for docker

func NewSwarmServiceClient

func NewSwarmServiceClient(
	c *client.Client, filterLabel, scrapNetLabel string, serviceNamePrefix string, includeAddressInNodeInfo bool,
	logger *log.Logger) *SwarmServiceClient

NewSwarmServiceClient creates a `SwarmServiceClient`

func (SwarmServiceClient) GetNodeInfo

func (c SwarmServiceClient) GetNodeInfo(ctx context.Context, ss SwarmService) (NodeIPSet, error)

GetNodeInfo returns node info for swarm service

func (SwarmServiceClient) SwarmServiceInspect

func (c SwarmServiceClient) SwarmServiceInspect(ctx context.Context, serviceID string) (*SwarmService, error)

SwarmServiceInspect returns `SwarmService` from its ID Returns nil when service doesnt not have the `FilterLabel` When `includeNodeIPInfo` is true, return node info as well

func (SwarmServiceClient) SwarmServiceList

func (c SwarmServiceClient) SwarmServiceList(ctx context.Context) ([]SwarmService, error)

SwarmServiceList returns a list of services

func (SwarmServiceClient) SwarmServiceRunning

func (c SwarmServiceClient) SwarmServiceRunning(ctx context.Context, serviceID string) (bool, error)

SwarmServiceRunning returns true if service is running

type SwarmServiceInspector

type SwarmServiceInspector interface {
	SwarmServiceInspect(ctx context.Context, serviceID string) (*SwarmService, error)
	SwarmServiceList(ctx context.Context) ([]SwarmService, error)
	GetNodeInfo(ctx context.Context, ss SwarmService) (NodeIPSet, error)
	SwarmServiceRunning(ctx context.Context, serviceID string) (bool, error)
}

SwarmServiceInspector is able to inspect services

type SwarmServiceListener

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

SwarmServiceListener listens for docker service events

func NewSwarmServiceListener

func NewSwarmServiceListener(c *client.Client, logger *log.Logger) *SwarmServiceListener

NewSwarmServiceListener creates a `SwarmServiceListener`

func (SwarmServiceListener) ListenForServiceEvents

func (s SwarmServiceListener) ListenForServiceEvents(eventChan chan<- Event)

ListenForServiceEvents listens for events and places them on channels

type SwarmServiceListening

type SwarmServiceListening interface {
	ListenForServiceEvents(chan<- Event)
}

SwarmServiceListening listens for service events

type SwarmServiceMini

type SwarmServiceMini struct {
	ID             string
	Name           string
	Labels         map[string]string
	Global         bool
	Replicas       uint64
	ContainerImage string
	NodeInfo       NodeIPSet
}

SwarmServiceMini is a optimized version of `SwarmService` for caching purposes

func MinifySwarmService

func MinifySwarmService(ss SwarmService, ignoreKey string, includeKey string) SwarmServiceMini

MinifySwarmService minifies `SwarmService` only labels prefixed with `com.df.` will be used `ignoreKey` wll be ignored from labels `includeKey` will be included

func (SwarmServiceMini) Equal

func (ssm SwarmServiceMini) Equal(other SwarmServiceMini) bool

Equal returns when SwarmServiceMini is equal to `other`

type SwarmServicePoller

type SwarmServicePoller struct {
	SSClient        SwarmServiceInspector
	SSCache         SwarmServiceCacher
	PollingInterval int
	IncludeNodeInfo bool
	MinifyFunc      func(SwarmService) SwarmServiceMini
	Log             *log.Logger
}

SwarmServicePoller implements `SwarmServicePoller`

func NewSwarmServicePoller

func NewSwarmServicePoller(
	ssClient SwarmServiceInspector,
	ssCache SwarmServiceCacher,
	pollingInterval int,
	includeNodeInfo bool,
	minifyFunc func(SwarmService) SwarmServiceMini,
	log *log.Logger,
) *SwarmServicePoller

NewSwarmServicePoller creates a new `SwarmServicePoller`

func (SwarmServicePoller) Run

func (s SwarmServicePoller) Run(
	eventChan chan<- Event)

Run starts poller and places events onto `eventChan`

type SwarmServicePolling

type SwarmServicePolling interface {
	Run(eventChan chan<- Event)
}

SwarmServicePolling provides an interface for polling service changes

Jump to

Keyboard shortcuts

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