This is a creation in Article, where the information may have evolved or changed.
swarm/cluster.go
Belonging swarm
package
to this, it defines swarm
the driver
structure of the Cluster
body:
// Cluster is exportedtype Cluster struct { sync.RWMutex eventHandlers *cluster.EventHandlers engines map[string]*cluster.Engine pendingEngines map[string]*cluster.Engine scheduler *scheduler.Scheduler discovery discovery.Backend pendingContainers map[string]*pendingContainer overcommitRatio float64 engineOpts *cluster.EngineOpts createRetry int64 TLSConfig *tls.Config}
It is important that the cluster.Engine
definition of this struct ( cluster/engine.go
), each cluster.Engine
representing one Docker engine
, namely Docker daemon
:
// Engine represents a docker enginetype Engine struct { sync.RWMutex ID string IP string Addr string Name string Cpus int64 Memory int64 Labels map[string]string Version string stopCh chan struct{} refreshDelayer *delayer containers map[string]*Container images []*Image networksmap[string]*Network volumes map[string]*Volume client dockerclient.Client apiClient engineapi.APIClient eventHandlerEventHandler state engineState lastError string updatedAt time.Time failureCountint overcommitRatio int64 opts*EngineOpts eventsMonitor *EventsMonitor}
The cluster
most important part of creating addEngine
this method is Docker engine
adding to cluster
the medium:
func (c *cluster) addengine (addr string) bool {//Check the engine is already registered by address. If C.hasenginebyaddr (addr) {return false} Engine: = cluster. Newengine (addr, c.overcommitratio, c.engineopts) if err: = engine. Registereventhandler (c); Err! = Nil {log. Error (ERR)}//ADD it to Pending engine map, indexed by address. This would prevent//duplicates from entering C.lock () c.pendingengines[addr] = Engine C.unlock ()//Valida Tependingengine'll start a thread to validate the engine. If the engine is reachable and valid, it's ll be monitored and updated in a loop. If engine is not reachable, pending engines'll be examined once in a while Go C.validatependingengine (engine) R Eturn true}
addEngine
First check whether the current is engine
already present cluster
, if not, it will be assigned a new one engine
, add it first Cluster.pendingEngines
, and start a new goroutine
( validatePendingEngine
) to check if it is a valid one engine
.
In addition, the Engine.RegisterEventHandler
member is essentially Cluster
eventHandlers
assigned to Engine.eventHandler
:
// RegisterEventHandler registers an event handler.func (e *Engine) RegisterEventHandler(h EventHandler) error { if e.eventHandler != nil { return errors.New("event handler already set") } e.eventHandler = h return nil}
It Cluster
is eventHandlers
assigned in newprimary, so it is essentially a set of Engine
Cluster
processing functions.