示例#1
0
func run(be backend.Backend, sm *subnet.SubnetManager, exit chan int) {
	var err error
	defer func() {
		if err == nil || err == task.ErrCanceled {
			exit <- 0
		} else {
			log.Error(err)
			exit <- 1
		}
	}()

	iface, ipaddr, err := lookupIface()
	if err != nil {
		return
	}

	if iface.MTU == 0 {
		err = fmt.Errorf("Failed to determine MTU for %s interface", ipaddr)
		return
	}

	log.Infof("Using %s as external interface", ipaddr)

	sn, err := be.Init(iface, ipaddr, opts.httpPort, opts.ipMasq)
	if err != nil {
		return
	}

	writeSubnetFile(sn)
	notifyWebhook(sn)
	daemon.SdNotify("READY=1")

	if err = httpServer(sm, ipaddr.String(), opts.httpPort); err != nil {
		err = fmt.Errorf("error starting HTTP server: %s", err)
		return
	}
	if opts.discoverdURL != "" {
		disc.NewClientWithURL(opts.discoverdURL).AddServiceAndRegister("flannel", net.JoinHostPort(ipaddr.String(), opts.httpPort))
	}

	log.Infof("%s mode initialized", be.Name())
	be.Run()
}
示例#2
0
func makeSubnetManager() *subnet.SubnetManager {
	var registryFn func() (subnet.Registry, error)
	if opts.discoverdURL != "" {
		client := disc.NewClientWithURL(opts.discoverdURL)
		registryFn = func() (subnet.Registry, error) {
			return discoverd.NewRegistry(client, "flannel")
		}
	} else {
		cfg := &subnet.EtcdConfig{
			Endpoints: strings.Split(opts.etcdEndpoints, ","),
			Keyfile:   opts.etcdKeyfile,
			Certfile:  opts.etcdCertfile,
			CAFile:    opts.etcdCAFile,
			Prefix:    opts.etcdPrefix,
		}
		registryFn = func() (subnet.Registry, error) {
			return subnet.NewEtcdSubnetRegistry(cfg)
		}
	}

	for {
		reg, err := registryFn()
		if err != nil {
			log.Error("Failed to create subnet registry: ", err)
			time.Sleep(time.Second)
			continue
		}

		sm, err := subnet.NewSubnetManager(reg)
		if err == nil {
			return sm
		}

		log.Error("Failed to create SubnetManager: ", err)
		time.Sleep(time.Second)
	}
}