diff --git a/api/tasks.go b/api/tasks.go index 2990b5433..c378c222d 100644 --- a/api/tasks.go +++ b/api/tasks.go @@ -27,7 +27,7 @@ type ServiceCheck struct { Name string Type string Script string - Http string + Path string Protocol string Interval time.Duration Timeout time.Duration diff --git a/client/consul.go b/client/consul.go index b3fdfe6c1..a87c9dad1 100644 --- a/client/consul.go +++ b/client/consul.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/go-multierror" "github.com/hashicorp/nomad/nomad/structs" "log" + "net/url" "sync" "time" ) @@ -180,7 +181,15 @@ func (c *ConsulClient) makeChecks(service *structs.Service, ip string, port int) } switch check.Type { case structs.ServiceCheckHTTP: - c.HTTP = fmt.Sprintf("%s://%s:%d/%s", check.Protocol, ip, port, check.Http) + if check.Protocol == "" { + check.Protocol = "http" + } + url := url.URL{ + Scheme: check.Protocol, + Host: fmt.Sprintf("%s:%d", ip, port), + Path: check.Path, + } + c.HTTP = url.String() case structs.ServiceCheckTCP: c.TCP = fmt.Sprintf("%s:%d", ip, port) case structs.ServiceCheckScript: diff --git a/client/consul_test.go b/client/consul_test.go new file mode 100644 index 000000000..fb844859e --- /dev/null +++ b/client/consul_test.go @@ -0,0 +1,53 @@ +package client + +import ( + "github.com/hashicorp/nomad/nomad/structs" + "log" + "os" + "testing" + "time" +) + +func TestMakeChecks(t *testing.T) { + service := &structs.Service{ + Id: "Foo", + Name: "Bar", + Checks: []structs.ServiceCheck{ + { + Type: "http", + Path: "/foo/bar", + Interval: 10 * time.Second, + Timeout: 2 * time.Second, + }, + { + Type: "http", + Protocol: "https", + Path: "/foo/bar", + Interval: 10 * time.Second, + Timeout: 2 * time.Second, + }, + { + Type: "tcp", + Interval: 10 * time.Second, + Timeout: 2 * time.Second, + }, + }, + } + + logger := log.New(os.Stdout, "logger: ", log.Lshortfile) + + c, _ := NewConsulClient(logger, "") + checks := c.makeChecks(service, "10.10.0.1", 8090) + + if checks[0].HTTP != "http://10.10.0.1:8090/foo/bar" { + t.Fatalf("Invalid http url for check: %v", checks[0].HTTP) + } + + if checks[1].HTTP != "https://10.10.0.1:8090/foo/bar" { + t.Fatalf("Invalid http url for check: %v", checks[0].HTTP) + } + + if checks[2].TCP != "10.10.0.1:8090" { + t.Fatalf("Invalid tcp check: %v", checks[0].TCP) + } +} diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 7fde437cc..b7453b512 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1009,7 +1009,7 @@ type ServiceCheck struct { Name string // Name of the check, defaults to id Type string // Type of the check - tcp, http, docker and script Script string // Script to invoke for script check - Http string // path of the health check url for http type check + Path string // path of the health check url for http type check Protocol string // Protocol to use if check is http, defaults to http Interval time.Duration // Interval of the check Timeout time.Duration // Timeout of the response from the check before consul fails the check @@ -1017,16 +1017,16 @@ type ServiceCheck struct { func (sc *ServiceCheck) Validate() error { t := strings.ToLower(sc.Type) - if sc.Type == ServiceCheckHTTP && sc.Http == "" { + if t != ServiceCheckTCP && t != ServiceCheckHTTP { + return fmt.Errorf("Check with name %v has invalid check type: %s ", sc.Name, sc.Type) + } + if sc.Type == ServiceCheckHTTP && sc.Path == "" { return fmt.Errorf("http checks needs the Http path information.") } if sc.Type == ServiceCheckScript && sc.Script == "" { return fmt.Errorf("Script checks need the script to invoke") } - if t != ServiceCheckTCP && t != ServiceCheckHTTP { - return fmt.Errorf("Check with name %v has invalid check type: %s ", sc.Name, sc.Type) - } return nil } diff --git a/website/source/docs/jobspec/servicediscovery.html.md b/website/source/docs/jobspec/servicediscovery.html.md index 447ae1f1e..87245c136 100644 --- a/website/source/docs/jobspec/servicediscovery.html.md +++ b/website/source/docs/jobspec/servicediscovery.html.md @@ -98,7 +98,7 @@ group "database" { of the health check endpoint. * `protocol`: This indicates the protocol for the http checks. Valid options - are `http` and `https`. + are `http` and `https`. We default it to `http` ## Assumptions