// New creates a new instance of network controller. func New(cfgOptions ...config.Option) (NetworkController, error) { c := &controller{ id: stringid.GenerateRandomID(), cfg: config.ParseConfigOptions(cfgOptions...), sandboxes: sandboxTable{}, svcRecords: make(map[string]svcInfo), serviceBindings: make(map[serviceKey]*service), agentInitDone: make(chan struct{}), } if err := c.initStores(); err != nil { return nil, err } drvRegistry, err := drvregistry.New(c.getStore(datastore.LocalScope), c.getStore(datastore.GlobalScope), c.RegisterDriver, nil) if err != nil { return nil, err } for _, i := range getInitializers() { var dcfg map[string]interface{} // External plugins don't need config passed through daemon. They can // bootstrap themselves if i.ntype != "remote" { dcfg = c.makeDriverConfig(i.ntype) } if err := drvRegistry.AddDriver(i.ntype, i.fn, dcfg); err != nil { return nil, err } } c.drvRegistry = drvRegistry if c.cfg != nil && c.cfg.Cluster.Watcher != nil { if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil { // Failing to initialize discovery is a bad situation to be in. // But it cannot fail creating the Controller log.Errorf("Failed to Initialize Discovery : %v", err) } } c.WalkNetworks(populateSpecial) // Reserve pools first before doing cleanup. Otherwise the // cleanups of endpoint/network and sandbox below will // generate many unnecessary warnings c.reservePools() // Cleanup resources c.sandboxCleanup(c.cfg.ActiveSandboxes) c.cleanupLocalEndpoints() c.networkCleanup() if err := c.startExternalKeyListener(); err != nil { return nil, err } return c, nil }
// New creates a new instance of network controller. func New(cfgOptions ...config.Option) (NetworkController, error) { c := &controller{ id: stringid.GenerateRandomID(), cfg: config.ParseConfigOptions(cfgOptions...), sandboxes: sandboxTable{}, svcRecords: make(map[string]svcInfo), serviceBindings: make(map[string]*service), } if err := c.agentInit(c.cfg.Daemon.Bind); err != nil { return nil, err } if err := c.agentJoin(c.cfg.Daemon.Neighbors); err != nil { return nil, err } if err := c.initStores(); err != nil { return nil, err } drvRegistry, err := drvregistry.New(c.getStore(datastore.LocalScope), c.getStore(datastore.GlobalScope), c.RegisterDriver, nil) if err != nil { return nil, err } for _, i := range getInitializers() { var dcfg map[string]interface{} // External plugins don't need config passed through daemon. They can // bootstrap themselves if i.ntype != "remote" { dcfg = c.makeDriverConfig(i.ntype) } if err := drvRegistry.AddDriver(i.ntype, i.fn, dcfg); err != nil { return nil, err } } c.drvRegistry = drvRegistry if c.cfg != nil && c.cfg.Cluster.Watcher != nil { if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil { // Failing to initialize discovery is a bad situation to be in. // But it cannot fail creating the Controller log.Errorf("Failed to Initialize Discovery : %v", err) } } c.sandboxCleanup() c.cleanupLocalEndpoints() c.networkCleanup() if err := c.startExternalKeyListener(); err != nil { return nil, err } return c, nil }
// New creates a new instance of network controller. //网络的控制器 func New(cfgOptions ...config.Option) (NetworkController, error) { //网络控制器对象,包括以下内容: /* id号,配置,包括的沙盒(namespaces),驱动,ip管理驱动,数据库 */ c := &controller{ id: stringid.GenerateRandomID(), cfg: config.ParseConfigOptions(cfgOptions...), sandboxes: sandboxTable{}, drivers: driverTable{}, ipamDrivers: ipamTable{}, svcDb: make(map[string]svcInfo), } //初始化数据库。 if err := c.initStores(); err != nil { return nil, err } //配置集群模式下的自动发现,这应该是使用swarm时候可以用overlay才需要这一步。 //周飒重点研究。 if c.cfg != nil && c.cfg.Cluster.Watcher != nil { if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil { // Failing to initialize discovery is a bad situation to be in. // But it cannot fail creating the Controller log.Errorf("Failed to Initialize Discovery : %v", err) } } //这一步会创建对应的网络吗?按理说不会,只是初始化一些配置应该。 //这里对符合要求的驱动进行注册。注册就是将controller的drivers数组中配置 //具体的driver,这里是一个driverTable包括driver的名称和driver的句柄以及capabilities。 if err := initDrivers(c); err != nil { return nil, err } //初始化IP地址管理驱动。同样的,会在controller中注册ipam的driver, //对应controller的ipamdrivers,ipamTable是由名称和ipamData构成, //ipamData是一个struct,包括某个具体的ipam的句柄,capabilities等信息。 if err := initIpams(c, c.getStore(datastore.LocalScope), c.getStore(datastore.GlobalScope)); err != nil { return nil, err } //清楚垃圾信息。 c.sandboxCleanup() c.cleanupLocalEndpoints() c.networkCleanup() //这一步应该是接受remote plugin的吧? if err := c.startExternalKeyListener(); err != nil { return nil, err } return c, nil }
// New creates a new instance of network controller. func New(cfgOptions ...config.Option) (NetworkController, error) { c := &controller{ id: stringid.GenerateRandomID(), cfg: config.ParseConfigOptions(cfgOptions...), sandboxes: sandboxTable{}, drivers: driverTable{}, ipamDrivers: ipamTable{}, svcDb: make(map[string]svcInfo), } if err := c.initStores(); err != nil { return nil, err } if c.cfg != nil && c.cfg.Cluster.Watcher != nil { if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil { // Failing to initialize discovery is a bad situation to be in. // But it cannot fail creating the Controller log.Errorf("Failed to Initialize Discovery : %v", err) } } if err := initDrivers(c); err != nil { return nil, err } if err := initIpams(c, c.getStore(datastore.LocalScope), c.getStore(datastore.GlobalScope)); err != nil { return nil, err } c.sandboxCleanup() c.cleanupLocalEndpoints() c.networkCleanup() if err := c.startExternalKeyListener(); err != nil { return nil, err } return c, nil }
func (c *controller) ReloadConfiguration(cfgOptions ...config.Option) error { procReloadConfig <- true defer func() { <-procReloadConfig }() // For now we accept the configuration reload only as a mean to provide a global store config after boot. // Refuse the configuration if it alters an existing datastore client configuration. update := false cfg := config.ParseConfigOptions(cfgOptions...) for s := range c.cfg.Scopes { if _, ok := cfg.Scopes[s]; !ok { return types.ForbiddenErrorf("cannot accept new configuration because it removes an existing datastore client") } } for s, nSCfg := range cfg.Scopes { if eSCfg, ok := c.cfg.Scopes[s]; ok { if eSCfg.Client.Provider != nSCfg.Client.Provider || eSCfg.Client.Address != nSCfg.Client.Address { return types.ForbiddenErrorf("cannot accept new configuration because it modifies an existing datastore client") } } else { if err := c.initScopedStore(s, nSCfg); err != nil { return err } update = true } } if !update { return nil } c.Lock() c.cfg = cfg c.Unlock() var dsConfig *discoverapi.DatastoreConfigData for scope, sCfg := range cfg.Scopes { if scope == datastore.LocalScope || !sCfg.IsValid() { continue } dsConfig = &discoverapi.DatastoreConfigData{ Scope: scope, Provider: sCfg.Client.Provider, Address: sCfg.Client.Address, Config: sCfg.Client.Config, } break } if dsConfig == nil { return nil } c.drvRegistry.WalkIPAMs(func(name string, driver ipamapi.Ipam, cap *ipamapi.Capability) bool { err := driver.DiscoverNew(discoverapi.DatastoreConfig, *dsConfig) if err != nil { log.Errorf("Failed to set datastore in driver %s: %v", name, err) } return false }) c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool { err := driver.DiscoverNew(discoverapi.DatastoreConfig, *dsConfig) if err != nil { log.Errorf("Failed to set datastore in driver %s: %v", name, err) } return false }) if c.discovery == nil && c.cfg.Cluster.Watcher != nil { if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil { log.Errorf("Failed to Initialize Discovery after configuration update: %v", err) } } return nil }