Ejemplo n.º 1
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
}
Ejemplo n.º 2
0
// processChange takes the backend change notification emitted by the backend and applies it to the server
func processChange(p proxy.Proxy, ch interface{}) error {
	switch change := ch.(type) {
	case *engine.HostUpserted:
		return p.UpsertHost(change.Host)
	case *engine.HostDeleted:
		return p.DeleteHost(change.HostKey)

	case *engine.ListenerUpserted:
		return p.UpsertListener(change.Listener)

	case *engine.ListenerDeleted:
		return p.DeleteListener(change.ListenerKey)

	case *engine.FrontendUpserted:
		return p.UpsertFrontend(change.Frontend)
	case *engine.FrontendDeleted:
		return p.DeleteFrontend(change.FrontendKey)

	case *engine.MiddlewareUpserted:
		return p.UpsertMiddleware(change.FrontendKey, change.Middleware)

	case *engine.MiddlewareDeleted:
		return p.DeleteMiddleware(change.MiddlewareKey)

	case *engine.BackendUpserted:
		return p.UpsertBackend(change.Backend)
	case *engine.BackendDeleted:
		return p.DeleteBackend(change.BackendKey)

	case *engine.ServerUpserted:
		return p.UpsertServer(change.BackendKey, change.Server)
	case *engine.ServerDeleted:
		return p.DeleteServer(change.ServerKey)
	}
	return fmt.Errorf("unsupported change: %#v", ch)
}
Ejemplo n.º 3
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
}