Skip to content

Commit

Permalink
Add support to routers use multi-cluster concept
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Oct 20, 2020
1 parent 78f6cdc commit db6e115
Show file tree
Hide file tree
Showing 37 changed files with 937 additions and 655 deletions.
2 changes: 1 addition & 1 deletion api/app_test.go
Expand Up @@ -6335,7 +6335,7 @@ func (s *S) TestRebuildRoutes(c *check.C) {
err := app.CreateApp(context.TODO(), &a, s.user)
c.Assert(err, check.IsNil)
s.provisioner.Provision(context.TODO(), &a)
err = routertest.FakeRouter.AddRoutes(context.TODO(), a.Name, []*url.URL{
err = routertest.FakeRouter.AddRoutes(context.TODO(), &a, []*url.URL{
{Host: "h1"},
})
c.Assert(err, check.IsNil)
Expand Down
10 changes: 5 additions & 5 deletions app/actions.go
Expand Up @@ -276,7 +276,7 @@ func removeAllRoutersBackend(ctx context.Context, app *App) error {
multi.Add(err)
continue
}
err = r.RemoveBackend(ctx, app.GetName())
err = r.RemoveBackend(ctx, app)
if err != nil && err != router.ErrBackendNotFound {
multi.Add(err)
}
Expand Down Expand Up @@ -652,12 +652,12 @@ func setUnsetCnames(ctx context.Context, app *App, cnames []string, toSet bool)
}
for _, c := range cnames {
if toSet {
err = cnameRouter.SetCName(ctx, c, app.Name)
err = cnameRouter.SetCName(ctx, c, app)
if err == router.ErrCNameExists {
err = nil
}
} else {
err = cnameRouter.UnsetCName(ctx, c, app.Name)
err = cnameRouter.UnsetCName(ctx, c, app)
if err == router.ErrCNameNotFound {
err = nil
}
Expand Down Expand Up @@ -692,7 +692,7 @@ var setNewCNamesToProvisioner = action.Action{
continue
}
for _, cname := range cnames {
err = cnameRouter.SetCName(ctx.Context, cname, app.Name)
err = cnameRouter.SetCName(ctx.Context, cname, app)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -814,7 +814,7 @@ var unsetCNameFromProvisioner = action.Action{
continue
}
for _, cname := range cnames {
err = cnameRouter.UnsetCName(ctx.Context, cname, app.Name)
err = cnameRouter.UnsetCName(ctx.Context, cname, app)
if err != nil {
return nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions app/app.go
Expand Up @@ -646,7 +646,7 @@ func Delete(ctx context.Context, app *App, evt *event.Event, requestID string) e
var r router.Router
r, err = router.Get(ctx, appRouter.Name)
if err == nil {
err = r.RemoveBackend(ctx, app.Name)
err = r.RemoveBackend(ctx, app)
}
if err != nil {
logErr("Failed to remove router backend", err)
Expand Down Expand Up @@ -1329,17 +1329,17 @@ func (app *App) Sleep(ctx context.Context, w io.Writer, process, versionStr stri
return err
}
var oldRoutes []*url.URL
oldRoutes, err = r.Routes(app.ctx, app.GetName())
oldRoutes, err = r.Routes(app.ctx, app)
if err != nil {
log.Errorf("[sleep] error on sleep the app %s - %s", app.Name, err)
return err
}
err = r.RemoveRoutes(app.ctx, app.GetName(), oldRoutes)
err = r.RemoveRoutes(app.ctx, app, oldRoutes)
if err != nil {
log.Errorf("[sleep] error on sleep the app %s - %s", app.Name, err)
return err
}
err = r.AddRoutes(app.ctx, app.GetName(), []*url.URL{proxyURL})
err = r.AddRoutes(app.ctx, app, []*url.URL{proxyURL})
if err != nil {
log.Errorf("[sleep] error on sleep the app %s - %s", app.Name, err)
return err
Expand Down Expand Up @@ -2053,7 +2053,7 @@ func Swap(ctx context.Context, app1, app2 *App, cnameOnly bool) error {
app1.GetRoutersWithAddr()
app2.GetRoutersWithAddr()
}(app1, app2)
err = r1.Swap(ctx, app1.Name, app2.Name, cnameOnly)
err = r1.Swap(ctx, app1, app2, cnameOnly)
if err != nil {
return err
}
Expand Down Expand Up @@ -2157,7 +2157,7 @@ func (app *App) AddRouter(appRouter appTypes.AppRouter) error {
routers := append(app.GetRouters(), appRouter)
err = app.updateRoutersDB(routers)
if err != nil {
rollbackErr := r.RemoveBackend(app.ctx, appRouter.Name)
rollbackErr := r.RemoveBackend(app.ctx, app)
if rollbackErr != nil {
log.Errorf("unable to remove router backend rolling back add router: %v", rollbackErr)
}
Expand Down Expand Up @@ -2226,7 +2226,7 @@ func (app *App) RemoveRouter(name string) error {
if err != nil {
return err
}
err = r.RemoveBackend(app.ctx, app.Name)
err = r.RemoveBackend(app.ctx, app)
if err != nil {
log.Errorf("unable to remove router backend: %v", err)
}
Expand Down Expand Up @@ -2277,13 +2277,13 @@ func (app *App) GetRoutersWithAddr() ([]appTypes.AppRouter, error) {
multi.Add(err)
continue
}
addr, err := r.Addr(app.ctx, app.Name)
addr, err := r.Addr(app.ctx, app)
if err != nil {
multi.Add(err)
continue
}
if statusRouter, ok := r.(router.StatusRouter); ok {
status, detail, stErr := statusRouter.GetBackendStatus(app.ctx, app.Name)
status, detail, stErr := statusRouter.GetBackendStatus(app.ctx, app)
if stErr != nil {
multi.Add(err)
continue
Expand All @@ -2292,7 +2292,7 @@ func (app *App) GetRoutersWithAddr() ([]appTypes.AppRouter, error) {
routers[i].StatusDetail = detail
}
if prefixRouter, ok := r.(router.PrefixRouter); ok {
addrs, aErr := prefixRouter.Addresses(app.ctx, app.Name)
addrs, aErr := prefixRouter.Addresses(app.ctx, app)
if aErr != nil {
multi.Add(aErr)
continue
Expand Down
12 changes: 6 additions & 6 deletions app/app_test.go
Expand Up @@ -2501,7 +2501,7 @@ func (s *S) TestSleep(c *check.C) {
sleeps := s.provisioner.Sleeps(&a, "")
c.Assert(sleeps, check.Equals, 1)
c.Assert(routertest.FakeRouter.HasRoute(a.Name, proxyURL.String()), check.Equals, true)
routes, err := routertest.FakeRouter.Routes(context.TODO(), a.Name)
routes, err := routertest.FakeRouter.Routes(context.TODO(), &a)
c.Assert(err, check.IsNil)
c.Assert(routes, check.HasLen, 1)
}
Expand Down Expand Up @@ -3991,7 +3991,7 @@ func (s *S) TestStartAsleepApp(c *check.C) {
}
err = a.Start(context.TODO(), &b, "web", "")
c.Assert(err, check.IsNil)
routes, err := routertest.FakeRouter.Routes(context.TODO(), a.Name)
routes, err := routertest.FakeRouter.Routes(context.TODO(), &a)
c.Assert(err, check.IsNil)
c.Assert(routes, check.HasLen, 1)
c.Assert(routertest.FakeRouter.HasRoute(a.Name, "http://proxy:1234"), check.Equals, false)
Expand All @@ -4013,7 +4013,7 @@ func (s *S) TestRestartAsleepApp(c *check.C) {
}
err = a.Restart(context.TODO(), "web", "", &b)
c.Assert(err, check.IsNil)
routes, err := routertest.FakeRouter.Routes(context.TODO(), a.Name)
routes, err := routertest.FakeRouter.Routes(context.TODO(), &a)
c.Assert(err, check.IsNil)
c.Assert(routes, check.HasLen, 1)
c.Assert(routertest.FakeRouter.HasRoute(a.Name, "http://proxy:1234"), check.Equals, false)
Expand Down Expand Up @@ -4884,7 +4884,7 @@ func (s *S) TestUpdatePlanNoRouteChange(c *check.C) {
c.Assert(dbApp.Plan, check.DeepEquals, plan)
c.Assert(s.provisioner.Restarts(dbApp, ""), check.Equals, 0)
c.Assert(routertest.FakeRouter.HasBackend(dbApp.Name), check.Equals, true)
routes, err := routertest.FakeRouter.Routes(context.TODO(), dbApp.Name)
routes, err := routertest.FakeRouter.Routes(context.TODO(), dbApp)
c.Assert(err, check.IsNil)
routesStr := make([]string, len(routes))
for i, route := range routes {
Expand Down Expand Up @@ -4923,7 +4923,7 @@ func (s *S) TestUpdatePlanNoRouteChangeShouldRestart(c *check.C) {
c.Assert(dbApp.Plan, check.DeepEquals, plan)
c.Assert(s.provisioner.Restarts(dbApp, ""), check.Equals, 1)
c.Assert(routertest.FakeRouter.HasBackend(dbApp.Name), check.Equals, true)
routes, err := routertest.FakeRouter.Routes(context.TODO(), dbApp.Name)
routes, err := routertest.FakeRouter.Routes(context.TODO(), dbApp)
c.Assert(err, check.IsNil)
routesStr := make([]string, len(routes))
for i, route := range routes {
Expand Down Expand Up @@ -4982,7 +4982,7 @@ func (s *S) TestUpdatePlanRestartFailure(c *check.C) {
c.Assert(s.provisioner.Restarts(dbApp, ""), check.Equals, 0)
c.Assert(routertest.FakeRouter.HasBackend(dbApp.Name), check.Equals, true)
c.Assert(routertest.HCRouter.HasBackend(dbApp.Name), check.Equals, false)
routes, err := routertest.FakeRouter.Routes(context.TODO(), dbApp.Name)
routes, err := routertest.FakeRouter.Routes(context.TODO(), dbApp)
c.Assert(err, check.IsNil)
routesStr := make([]string, len(routes))
for i, route := range routes {
Expand Down
2 changes: 1 addition & 1 deletion builder/docker/actions_test.go
Expand Up @@ -273,7 +273,7 @@ func (s *S) newContainer(c *check.C, client *docker.Client) *container.Container
fakeApp := provisiontest.NewFakeApp(container.AppName, "python", 0)
version := newVersionForApp(c, client, fakeApp, nil)
routertest.FakeRouter.AddBackend(context.TODO(), routertest.FakeApp{Name: container.AppName})
routertest.FakeRouter.AddRoutes(context.TODO(), container.AppName, []*url.URL{container.Address()})
routertest.FakeRouter.AddRoutes(context.TODO(), fakeApp, []*url.URL{container.Address()})
ports := map[docker.Port]struct{}{
docker.Port(s.port + "/tcp"): {},
}
Expand Down
18 changes: 9 additions & 9 deletions provision/docker/actions.go
Expand Up @@ -491,9 +491,9 @@ var addNewRoutes = action.Action{
return newContainers, nil
}
err = runInRouters(ctx.Context, args.app, func(r router.Router) error {
return r.AddRoutes(ctx.Context, args.app.GetName(), routesToAdd)
return r.AddRoutes(ctx.Context, args.app, routesToAdd)
}, func(r router.Router) error {
return r.RemoveRoutes(ctx.Context, args.app.GetName(), routesToAdd)
return r.RemoveRoutes(ctx.Context, args.app, routesToAdd)
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -523,7 +523,7 @@ var addNewRoutes = action.Action{
return
}
err := runInRouters(ctx.Context, args.app, func(r router.Router) error {
return r.RemoveRoutes(ctx.Context, args.app.GetName(), routesToRemove)
return r.RemoveRoutes(ctx.Context, args.app, routesToRemove)
}, nil)
if err != nil {
log.Errorf("[add-new-routes:Backward] Error removing route for [%v]: %s", routesToRemove, err)
Expand Down Expand Up @@ -569,7 +569,7 @@ var setRouterHealthcheck = action.Action{
if !ok {
return nil
}
return hcRouter.SetHealthcheck(ctx.Context, args.app.GetName(), newHCData)
return hcRouter.SetHealthcheck(ctx.Context, args.app, newHCData)
}, func(r router.Router) error {
hcRouter, ok := r.(router.CustomHealthcheckRouter)
if !ok {
Expand All @@ -587,7 +587,7 @@ var setRouterHealthcheck = action.Action{
if err != nil {
return err
}
return hcRouter.SetHealthcheck(ctx.Context, args.app.GetName(), yamlData.ToRouterHC())
return hcRouter.SetHealthcheck(ctx.Context, args.app, yamlData.ToRouterHC())
})
return newContainers, err
},
Expand All @@ -612,7 +612,7 @@ var setRouterHealthcheck = action.Action{
if !ok {
return nil
}
return hcRouter.SetHealthcheck(ctx.Context, args.app.GetName(), hcData)
return hcRouter.SetHealthcheck(ctx.Context, args.app, hcData)
}, nil)
if err != nil {
log.Errorf("[set-router-healthcheck:Backward] Error setting healthcheck: %s", err)
Expand Down Expand Up @@ -671,12 +671,12 @@ var removeOldRoutes = action.Action{
return result, nil
}
err = runInRouters(ctx.Context, args.app, func(r router.Router) error {
return r.RemoveRoutes(ctx.Context, args.app.GetName(), routesToRemove)
return r.RemoveRoutes(ctx.Context, args.app, routesToRemove)
}, func(r router.Router) error {
if args.appDestroy {
return nil
}
return r.AddRoutes(ctx.Context, args.app.GetName(), routesToRemove)
return r.AddRoutes(ctx.Context, args.app, routesToRemove)
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -705,7 +705,7 @@ var removeOldRoutes = action.Action{
return
}
err := runInRouters(ctx.Context, args.app, func(r router.Router) error {
return r.AddRoutes(ctx.Context, args.app.GetName(), routesToAdd)
return r.AddRoutes(ctx.Context, args.app, routesToAdd)
}, nil)
if err != nil {
log.Errorf("[remove-old-routes:Backward] Error adding back route for [%v]: %s", routesToAdd, err)
Expand Down

0 comments on commit db6e115

Please sign in to comment.