Skip to content

Commit

Permalink
Register in Consul with Mesos healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Oskar Jagodzinski authored and tomez committed Mar 20, 2019
1 parent 0396532 commit 93f6c6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
7 changes: 5 additions & 2 deletions hook/consul/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/hashicorp/consul/api"
log "github.com/sirupsen/logrus"

"github.com/allegro/mesos-executor"
executor "github.com/allegro/mesos-executor"
"github.com/allegro/mesos-executor/hook"
"github.com/allegro/mesos-executor/mesosutils"
"github.com/allegro/mesos-executor/runenv"
"github.com/mesos/mesos-go/api/v1/lib"
mesos "github.com/mesos/mesos-go/api/v1/lib"
)

const (
Expand Down Expand Up @@ -199,14 +199,17 @@ func generateHealthCheck(mesosCheck mesosutils.HealthCheck, port int) *api.Agent
switch mesosCheck.Type {
case mesosutils.HTTP:
check.HTTP = generateURL(mesosCheck.HTTP.Path, port)
return &check
case mesosutils.TCP:
check.TCP = fmt.Sprintf("%s:%d", serviceHost, port)
return &check
}
return nil
}

func generateURL(path string, port int) string {
var checkURL url.URL
checkURL.Scheme = "http"
checkURL.Host = executor.HealthCheckAddress(uint32(port))
checkURL.Path = path

Expand Down
41 changes: 31 additions & 10 deletions hook/consul/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestIfUsesLabelledPortsForServiceIDGen(t *testing.T) {
},
})

expectedServiceID := createServiceId(taskID, consulName, 997)
expectedServiceID := createServiceID(taskID, consulName, 997)

// Create a test Consul server
config, server := createTestConsulServer(t)
Expand All @@ -54,6 +54,27 @@ func TestIfUsesLabelledPortsForServiceIDGen(t *testing.T) {
require.Contains(t, services[consulName], "otherTag")
}

func TestIfUsesMesosHCForConsulCheck(t *testing.T) {
consulName := "consulName"
serviceName := "service:taskID_consulName_666"
taskID := "taskID"
taskInfo := prepareTaskInfo(taskID, consulName, consulName, []string{"metrics", "otherTag"}, []mesos.Port{
{Number: 666},
})

// Create a test Consul server
config, server := createTestConsulServer(t)
client, _ := api.NewClient(config) // #nosec
defer stopConsul(server)

h := &Hook{config: Config{ConsulGlobalTag: "marathon"}, client: client}
err := h.RegisterIntoConsul(taskInfo)

checks, err := client.Agent().Checks()
require.NoError(t, err)
require.Contains(t, checks, serviceName)
}

func TestIfUsesFirstPortIfNoneIsLabelledForServiceIDGen(t *testing.T) {
consulName := "consulName"
taskID := "taskID"
Expand All @@ -62,7 +83,7 @@ func TestIfUsesFirstPortIfNoneIsLabelledForServiceIDGen(t *testing.T) {
{Number: 997},
})

expectedServiceID := createServiceId(taskID, consulName, 666)
expectedServiceID := createServiceID(taskID, consulName, 666)

// Create a test Consul server
config, server := createTestConsulServer(t)
Expand Down Expand Up @@ -111,8 +132,8 @@ func TestIfUsesLabelledPortsForServiceIDGenAndRegisterMultiplePorts(t *testing.T
},
},
})
expectedServiceID := createServiceId(taskID, consulNameFirst, 997)
expectedServiceID2 := createServiceId(taskID, consulNameSecond, 998)
expectedServiceID := createServiceID(taskID, consulNameFirst, 997)
expectedServiceID2 := createServiceID(taskID, consulNameSecond, 998)

// Create a test Consul server
config, server := createTestConsulServer(t)
Expand Down Expand Up @@ -170,13 +191,13 @@ func TestIfUsesPortLabelsForRegistration(t *testing.T) {

expectedService := instance{
consulServiceName: "consulName",
consulServiceID: createServiceId(taskID, consulName, 666),
consulServiceID: createServiceID(taskID, consulName, 666),
port: 666,
tags: []string{"hystrix", "metrics", "extras", "marathon"},
}
expectedService2 := instance{
consulServiceName: "consulName-secured",
consulServiceID: createServiceId(taskID, consulNameSecond, 997),
consulServiceID: createServiceID(taskID, consulNameSecond, 997),
port: 997,
tags: []string{"metrics", "extras", "marathon"},
}
Expand Down Expand Up @@ -221,7 +242,7 @@ func TestIfGeneratesNameIfConsulLabelTrueOrEmpty(t *testing.T) {
{Number: 666},
})

expectedServiceID := createServiceId(taskID, taskID, 666)
expectedServiceID := createServiceID(taskID, taskID, 666)

// Create a test Consul server
config, server := createTestConsulServer(t)
Expand Down Expand Up @@ -263,7 +284,7 @@ func TestIfGeneratesCorrectNameIfConsulLabelEmpty(t *testing.T) {
{Number: 666},
})

expectedServiceID := createServiceId(testData.taskID, testData.expectedName, 666)
expectedServiceID := createServiceID(testData.taskID, testData.expectedName, 666)

// Create a test Consul server
config, server := createTestConsulServer(t)
Expand Down Expand Up @@ -357,8 +378,8 @@ func stopConsul(server *testutil.TestServer) {
_ = server.Stop()
}

func createServiceId(taskId string, taskName string, port int) string {
return taskId + "_" + taskName + "_" + strconv.Itoa(port)
func createServiceID(taskID string, taskName string, port int) string {
return taskID + "_" + taskName + "_" + strconv.Itoa(port)
}

func prepareTaskInfo(taskID string, taskName string, consulName string, tags []string, ports []mesos.Port) mesosutils.TaskInfo {
Expand Down

0 comments on commit 93f6c6a

Please sign in to comment.