Example #1
0
// initProxy reads the configuration from the engine and configures the server
func initProxy(ng engine.Engine, p proxy.Proxy) (uint64, error) {
	snapshot, err := ng.GetSnapshot()
	if err != nil {
		// Some legacy engines do not implement `GetSnapshot` so we resort to
		// a slow method of obtaining configuration from Etcd.
		if _, ok := err.(*engine.SnapshotNotSupportedError); ok {
			return 0, initProxySlow(ng, p)
		}
		return 0, err
	}
	for _, h := range snapshot.Hosts {
		if err := p.UpsertHost(h); err != nil {
			return 0, err
		}
	}
	for _, bs := range snapshot.BackendSpecs {
		if err := p.UpsertBackend(bs.Backend); err != nil {
			return 0, err
		}
		for _, server := range bs.Servers {
			if err := p.UpsertServer(bs.Backend.GetUniqueId(), server); err != nil {
				return 0, err
			}
		}
	}
	for _, l := range snapshot.Listeners {
		if err := p.UpsertListener(l); err != nil {
			return 0, err
		}
	}
	for _, fs := range snapshot.FrontendSpecs {
		if err := p.UpsertFrontend(fs.Frontend); err != nil {
			return 0, err
		}
		for _, m := range fs.Middlewares {
			if err := p.UpsertMiddleware(fs.Frontend.GetKey(), m); err != nil {
				return 0, err
			}
		}
	}
	return snapshot.Index, nil
}
Example #2
0
// initProxySlow reads configuration from Etcd piece by piece. That may take
// several minutes on large configurations!
func initProxySlow(ng engine.Engine, p proxy.Proxy) error {
	hosts, err := ng.GetHosts()
	if err != nil {
		return err
	}

	for _, h := range hosts {
		if err := p.UpsertHost(h); err != nil {
			return err
		}
	}

	bs, err := ng.GetBackends()
	if err != nil {
		return err
	}

	for _, b := range bs {
		if err := p.UpsertBackend(b); err != nil {
			return err
		}

		bk := engine.BackendKey{Id: b.Id}
		servers, err := ng.GetServers(bk)
		if err != nil {
			return err
		}

		for _, s := range servers {
			if err := p.UpsertServer(bk, s); err != nil {
				return err
			}
		}
	}

	ls, err := ng.GetListeners()
	if err != nil {
		return err
	}

	for _, l := range ls {
		if err := p.UpsertListener(l); err != nil {
			return err
		}
	}

	fs, err := ng.GetFrontends()
	if err != nil {
		return err
	}

	if len(fs) == 0 {
		log.Warningf("No frontends found")
	}

	for _, f := range fs {
		if err := p.UpsertFrontend(f); err != nil {
			log.Warningf("Cannot register frontend \"%v\", invalid backend config", f.Id)
			continue
		}
		fk := engine.FrontendKey{Id: f.Id}
		ms, err := ng.GetMiddlewares(fk)
		if err != nil {
			return err
		}
		for _, m := range ms {
			if err := p.UpsertMiddleware(fk, m); err != nil {
				log.Warningf("Cannot register frontend \"%v\", invalid middleware config", f.Id)
				continue
			}
		}
	}
	return nil
}