Example #1
0
// New creates a new instance of network controller.
func New(cfgOptions ...config.Option) (NetworkController, error) {
	var cfg *config.Config
	cfg = &config.Config{
		Daemon: config.DaemonCfg{
			DriverCfg: make(map[string]interface{}),
		},
		Scopes: make(map[string]*datastore.ScopeCfg),
	}

	if len(cfgOptions) > 0 {
		cfg.ProcessOptions(cfgOptions...)
	}
	cfg.LoadDefaultScopes(cfg.Daemon.DataDir)

	c := &controller{
		id:          stringid.GenerateRandomID(),
		cfg:         cfg,
		sandboxes:   sandboxTable{},
		drivers:     driverTable{},
		ipamDrivers: ipamTable{},
		svcDb:       make(map[string]svcMap),
	}

	if err := c.initStores(); err != nil {
		return nil, err
	}

	if cfg != nil && cfg.Cluster.Watcher != nil {
		if err := c.initDiscovery(cfg.Cluster.Watcher); err != nil {
			// Failing to initalize 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()

	if err := c.startExternalKeyListener(); err != nil {
		return nil, err
	}

	return c, nil
}