mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 09:25:46 +03:00
Merge pull request #455 from hashicorp/f-consul-check
Refactor consul check
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
53
client/consul_test.go
Normal file
53
client/consul_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user