Skip to content

Commit

Permalink
Merge pull request #205 from porcupie/master
Browse files Browse the repository at this point in the history
Add MesosDnsId field to Marathon App struct
  • Loading branch information
j1n6 committed Jun 7, 2016
2 parents cbac0cd + 8b0f970 commit c5ee788
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ The default template shipped with Bamboo is aware of `BAMBOO_TCP_PORT`. When thi

In this example, both `BAMBOO_TCP_PORT` and `MY_CUSTOM_ENV` can be accessed in HAProxy template. This enables flexible template customization depending on your preferences.

#### Default Haproxy Template ACL

The default acl rule in the `haproxy_template.cfg` uses the full
marathon app id, which may include slash-separated groups.

```
# This is the default proxy criteria
acl {{ $app.EscapedId }}-aclrule path_beg -i {{ $app.Id }}
```

For example if your app is named "/mygroup/appname", your default acl
will be `path_beg -i /mygroup/appname`. This can always be changed
using the bamboo web UI.

There is also a DNS friendly version of your marathon app Id which can
be used instead of the slash-separated one. `MesosDnsId` includes the
groups as hyphenated suffixes. For example, if your appname is
"/another/group/app" then the `MesosDnsId` will be "app-group-another".

You can edit the `haproxy_template.cfg` and use the DNS friendly name
for your default ACL instead.

```
acl {{ $app.EscapedId }}-aclrule hdr_dom(host) -i {{ $app.MesosDnsId }}
```

### Environment Variables

Configuration in the `production.json` file can be overridden with environment variables below. This is generally useful when you are building a Docker image for Bamboo and HAProxy. If they are not specified then the values from the configuration file will be used.
Expand Down
2 changes: 2 additions & 0 deletions config/haproxy_template.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ frontend http-in

# This is the default proxy criteria
acl {{ $app.EscapedId }}-aclrule path_beg -i {{ $app.Id }}
# another possible default using DNS-friendly MesosDnsId:
#acl {{ $app.EscapedId }}-aclrule hdr_dom(host) -i {{ $app.MesosDnsId }}
use_backend {{ $app.EscapedId }}-cluster if {{ $app.EscapedId }}-aclrule
{{ end }} {{ end }}

Expand Down
15 changes: 15 additions & 0 deletions services/marathon/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type HealthCheck struct {
// An app may have multiple processes
type App struct {
Id string
MesosDnsId string
EscapedId string
HealthCheckPath string
HealthChecks []HealthCheck
Expand Down Expand Up @@ -196,6 +197,7 @@ func createApps(tasksById map[string]marathonTaskList, marathonApps map[string]m
// build App from marathonApp
app := App{
Id: appPath,
MesosDnsId: getMesosDnsId(appPath),
EscapedId: strings.Replace(appId, "/", "::", -1),
HealthCheckPath: parseHealthCheckPath(mApp.HealthChecks),
Env: mApp.Env,
Expand Down Expand Up @@ -236,6 +238,19 @@ func createApps(tasksById map[string]marathonTaskList, marathonApps map[string]m
return apps
}

func getMesosDnsId(appPath string) string {
// split up groups and recombine for how mesos-dns/consul/etc use service name
// "/nested/group/app" -> "app-group-nested"
groups := strings.Split(appPath, "/")
reverseGroups := []string{}
for i := len(groups) - 1; i >= 0; i-- {
if groups[i] != "" {
reverseGroups = append(reverseGroups, groups[i])
}
}
return strings.Join(reverseGroups, "-")
}

func parseHealthCheckPath(checks []marathonHealthCheck) string {
for _, check := range checks {
if check.Protocol != "HTTP" {
Expand Down
20 changes: 20 additions & 0 deletions services/marathon/marathon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ import (
"testing"
)

func TestGetMesosDnsId_Simple(t *testing.T) {
Convey("#getMesosDnsId", t, func() {
Convey("should return simple appname", func() {
So(getMesosDnsId("appname"), ShouldEqual, "appname")
})

Convey("should return simple appname if slash prefixed", func() {
So(getMesosDnsId("/appname"), ShouldEqual, "appname")
})

Convey("should return groups reverse-added to appname", func() {
So(getMesosDnsId("/group/appname"), ShouldEqual, "appname-group")
})

Convey("should return groups reverse-added to appname but no blanks", func() {
So(getMesosDnsId("//group/again//appname/"), ShouldEqual, "appname-again-group")
})
})
}

func TestParseHealthCheckPathTCP(t *testing.T) {
Convey("#parseHealthCheckPath", t, func() {
checks := []marathonHealthCheck{
Expand Down

0 comments on commit c5ee788

Please sign in to comment.