Repository: allegro/marathon-consul (go)

Issue:
Set initial consul health check to passing
Consul allows setting initial health check value. Since we know that application is working (we register only when Marathon health check is passing) we can minimize time when service is unavailable in consul by setting health check to passing when registering service. 

https://github.com/hashicorp/consul/issues/2314#issuecomment-248630431


Pull request description:
Fixes #122 | Set healtcheck to passing

Patch:
diff --git a/consul/consul.go b/consul/consul.go
index cf27750..7daf44a 100644
--- a/consul/consul.go
+++ b/consul/consul.go
@@ -318,17 +318,20 @@ func (c *Consul) marathonToConsulChecks(task *apps.Task, healthChecks []apps.Hea
 		} else {
 			port = task.Ports[check.PortIndex]
 		}
+
+		consulCheck := consulapi.AgentServiceCheck{
+			Interval: fmt.Sprintf("%ds", check.IntervalSeconds),
+			Timeout:  fmt.Sprintf("%ds", check.TimeoutSeconds),
+			Status:   "passing",
+		}
+
 		switch check.Protocol {
 		case "HTTP", "HTTPS":
 			if parsedURL, err := url.ParseRequestURI(check.Path); err == nil {
 				parsedURL.Scheme = strings.ToLower(check.Protocol)
 				parsedURL.Host = fmt.Sprintf("%s:%d", serviceAddress, port)
-
-				checks = append(checks, &consulapi.AgentServiceCheck{
-					HTTP:     parsedURL.String(),
-					Interval: fmt.Sprintf("%ds", check.IntervalSeconds),
-					Timeout:  fmt.Sprintf("%ds", check.TimeoutSeconds),
-				})
+				consulCheck.HTTP = parsedURL.String()
+				checks = append(checks, &consulCheck)
 			} else {
 				log.WithError(err).
 					WithField("Id", task.AppID.String()).
@@ -336,17 +339,11 @@ func (c *Consul) marathonToConsulChecks(task *apps.Task, healthChecks []apps.Hea
 					Warn(fmt.Sprintf("Could not parse provided path: %s", check.Path))
 			}
 		case "TCP":
-			checks = append(checks, &consulapi.AgentServiceCheck{
-				TCP:      fmt.Sprintf("%s:%d", serviceAddress, port),
-				Interval: fmt.Sprintf("%ds", check.IntervalSeconds),
-				Timeout:  fmt.Sprintf("%ds", check.TimeoutSeconds),
-			})
+			consulCheck.TCP = fmt.Sprintf("%s:%d", serviceAddress, port)
+			checks = append(checks, &consulCheck)
 		case "COMMAND":
-			checks = append(checks, &consulapi.AgentServiceCheck{
-				Script:   check.Command.Value,
-				Interval: fmt.Sprintf("%ds", check.IntervalSeconds),
-				Timeout:  fmt.Sprintf("%ds", check.TimeoutSeconds),
-			})
+			consulCheck.Script = check.Command.Value
+			checks = append(checks, &consulCheck)
 		default:
 			log.WithField("Id", task.AppID.String()).WithField("Address", serviceAddress).
 				Warn(fmt.Sprintf("Unrecognized check protocol %s", check.Protocol))


Test patch:
diff --git a/consul/consul_test.go b/consul/consul_test.go
index 358304d..c62be6f 100644
--- a/consul/consul_test.go
+++ b/consul/consul_test.go
@@ -789,26 +789,31 @@ func TestMarathonTaskToConsulServiceMapping(t *testing.T) {
 			HTTP:     "http://127.0.0.6:8123/api/health?with=query",
 			Interval: "60s",
 			Timeout:  "20s",
+			Status:   "passing",
 		},
 		{
 			HTTP:     "https://127.0.0.6:8090/secure/health?with=query",
 			Interval: "50s",
 			Timeout:  "20s",
+			Status:   "passing",
 		},
 		{
 			TCP:      "127.0.0.6:8443",
 			Interval: "40s",
 			Timeout:  "20s",
+			Status:   "passing",
 		},
 		{
 			TCP:      "127.0.0.6:8234",
 			Interval: "40s",
 			Timeout:  "20s",
+			Status:   "passing",
 		},
 		{
 			Script:   "echo 1",
 			Interval: "30s",
 			Timeout:  "20s",
+			Status:   "passing",
 		},
 	}, service.Checks)
 }
@@ -899,11 +904,13 @@ func TestMarathonTaskToConsulServiceMapping_IgnoredHealthcheckTypes(t *testing.T
 			HTTP:     "http://127.0.0.6:8090/api/health?with=query",
 			Interval: "60s",
 			Timeout:  "20s",
+			Status:   "passing",
 		},
 		{
 			HTTP:     "https://127.0.0.6:8090/secure/health?with=query",
 			Interval: "50s",
 			Timeout:  "20s",
+			Status:   "passing",
 		},
 	}, service.Checks)
 }