示例#1
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)
}
示例#2
0
// initProxy reads the configuration from the engine and configures the server
func initProxy(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 {
			return err
		}
		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 {
				return err
			}
		}
	}
	return nil
}