Skip to content

Commit

Permalink
provision,app: use pool constraints for default router
Browse files Browse the repository at this point in the history
With this commit, the first router from the pool constraint
values will be used as the default value for that pool. We will discard
this if the first value has the character "*" or the constraint is a
blacklist. The global default value is used as fallback in those cases
and when there is no constraint for the pool.
  • Loading branch information
andrestc committed May 22, 2017
1 parent f1070fd commit 596d322
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
16 changes: 10 additions & 6 deletions app/app.go
Expand Up @@ -297,19 +297,23 @@ func CreateApp(app *App, user *auth.User) error {
if err != nil {
return err
}
app.Plan = *plan
err = app.SetPool()
if err != nil {
return err
}
if app.Router == "" {
app.Router, err = router.Default()
pool, err := provision.GetPoolByName(app.GetPool())
if err != nil {
return err
}
app.Router, err = pool.GetDefaultRouter()
} else {
_, err = router.Get(app.Router)
}
if err != nil {
return err
}
app.Plan = *plan
err = app.SetPool()
if err != nil {
return err
}
app.Teams = []string{app.TeamOwner}
app.Owner = user.Email
app.Tags = processTags(app.Tags)
Expand Down
23 changes: 23 additions & 0 deletions app/app_test.go
Expand Up @@ -229,6 +229,29 @@ func (s *S) TestCreateAppDefaultPlan(c *check.C) {
c.Assert(err, check.IsNil)
}

func (s *S) TestCreateAppDefaultRouterForPool(c *check.C) {
provision.SetPoolConstraint(&provision.PoolConstraint{
PoolExpr: "pool1",
Field: "router",
Values: []string{"fake-tls", "fake"},
})
a := App{
Name: "appname",
Platform: "python",
TeamOwner: s.team.Name,
}
expectedHost := "localhost"
config.Set("host", expectedHost)
s.conn.Users().Update(bson.M{"email": s.user.Email}, bson.M{"$set": bson.M{"quota.limit": 1}})
config.Set("quota:units-per-app", 3)
defer config.Unset("quota:units-per-app")
err := CreateApp(&a, s.user)
c.Assert(err, check.IsNil)
retrievedApp, err := GetByName(a.Name)
c.Assert(err, check.IsNil)
c.Assert(retrievedApp.Router, check.Equals, "fake-tls")
}

func (s *S) TestCreateAppWithoutDefaultPlan(c *check.C) {
s.conn.Plans().RemoveAll(nil)
defer s.conn.Plans().Insert(s.defaultPlan)
Expand Down
22 changes: 22 additions & 0 deletions provision/pool.go
Expand Up @@ -81,6 +81,28 @@ func (p *Pool) GetRouters() ([]string, error) {
return nil, ErrPoolHasNoRouter
}

func (p *Pool) GetDefaultRouter() (string, error) {
constraints, err := getConstraintsForPool(p.Name, "router")
if err != nil {
return "", err
}
constraint := constraints["router"]
if constraint == nil || constraint.Blacklist ||
len(constraint.Values) == 0 || strings.Contains(constraint.Values[0], "*") {
return router.Default()
}
routers, err := routersNames()
if err != nil {
return "", err
}
for _, r := range routers {
if constraint.Values[0] == r {
return r, nil
}
}
return router.Default()
}

func (p *Pool) allowedValues() (map[string][]string, error) {
teams, err := teamsNames()
if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions provision/pool_test.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/tsuru/tsuru/auth"
"github.com/tsuru/tsuru/db"
"github.com/tsuru/tsuru/db/dbtest"
"github.com/tsuru/tsuru/router"
"gopkg.in/check.v1"
"gopkg.in/mgo.v2/bson"
)
Expand Down Expand Up @@ -530,6 +531,50 @@ func (s *S) TestGetRouters(c *check.C) {
c.Assert(routers, check.DeepEquals, []string{"router1", "router2"})
}

func (s *S) TestGetDefaultRouterFromConstraint(c *check.C) {
config.Set("routers:router1:type", "hipache")
config.Set("routers:router2:type", "hipache")
defer config.Unset("routers")
err := AddPool(AddPoolOptions{Name: "pool1"})
c.Assert(err, check.IsNil)
err = SetPoolConstraint(&PoolConstraint{PoolExpr: "pool*", Field: "router", Values: []string{"router2"}, Blacklist: false})
c.Assert(err, check.IsNil)
pool, err := GetPoolByName("pool1")
c.Assert(err, check.IsNil)
r, err := pool.GetDefaultRouter()
c.Assert(err, check.IsNil)
c.Assert(r, check.Equals, "router2")
}

func (s *S) TestGetDefaultRouterNoDefault(c *check.C) {
config.Set("routers:router1:type", "hipache")
config.Set("routers:router2:type", "hipache")
defer config.Unset("routers")
err := AddPool(AddPoolOptions{Name: "pool1"})
c.Assert(err, check.IsNil)
err = SetPoolConstraint(&PoolConstraint{PoolExpr: "pool*", Field: "router", Values: []string{"*"}, Blacklist: false})
c.Assert(err, check.IsNil)
pool, err := GetPoolByName("pool1")
c.Assert(err, check.IsNil)
r, err := pool.GetDefaultRouter()
c.Assert(err, check.Equals, router.ErrDefaultRouterNotFound)
c.Assert(r, check.Equals, "")
}

func (s *S) TestGetDefaultFallbackFromConfig(c *check.C) {
config.Set("routers:router1:type", "hipache")
config.Set("routers:router2:type", "hipache")
config.Set("routers:router2:default", true)
defer config.Unset("routers")
err := AddPool(AddPoolOptions{Name: "pool1"})
c.Assert(err, check.IsNil)
pool, err := GetPoolByName("pool1")
c.Assert(err, check.IsNil)
r, err := pool.GetDefaultRouter()
c.Assert(err, check.Equals, nil)
c.Assert(r, check.Equals, "router2")
}

func (s *S) TestPoolAllowedValues(c *check.C) {
config.Set("routers:router:type", "hipache")
config.Set("routers:router1:type", "hipache")
Expand Down

0 comments on commit 596d322

Please sign in to comment.