Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:hashicorp/faas-nomad
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Sep 5, 2018
2 parents f493159 + 30ca738 commit 070c68f
Show file tree
Hide file tree
Showing 1,517 changed files with 129,898 additions and 54,509 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ before_install:
- sudo chmod +x /usr/local/bin/consul
- sudo chmod +x /usr/local/bin/nomad
- make deps
- git clone -b additions https://github.com/nicholasjackson/certify-incubator.git $GOPATH/src/github.com/openfaas/certify-incubator
- git clone -b availableReplicas https://github.com/nicholasjackson/certify-incubator.git $GOPATH/src/github.com/openfaas/certify-incubator
- docker run -d -p 5000:5000 --restart=always --name registry registry:2
- go get github.com/wadey/gocovmerge

Expand Down
70 changes: 38 additions & 32 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

[[constraint]]
name = "github.com/openfaas/faas"
version = "0.6.12"
version = "0.7.4"

[[constraint]]
name = "github.com/openfaas/faas-provider"
version = "0.4.0"
version = "0.5.0"

[[constraint]]
name = "github.com/hashicorp/nomad"
Expand Down
29 changes: 29 additions & 0 deletions handlers/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,22 @@ func MakeReplicationReader(client nomad.Job, logger hclog.Logger, stats metrics.
return
}

// get the number of available allocations from the job
allocs, err := getAllocationReadyCount(client, job, r)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(rw, err)

log.Error("Error getting job allocations", "error", err)
stats.Incr("replicationreader.error.internalerror", nil, 1)
return
}

resp := requests.Function{}
resp.AvailableReplicas = allocs
resp.Replicas = uint64(*job.TaskGroups[0].Count)
resp.Name = strings.Replace(*job.ID, nomad.JobPrefix, "", -1)

rw.Header().Set("Content-Type", "application/json")
json.NewEncoder(rw).Encode(resp)

Expand Down Expand Up @@ -95,3 +109,18 @@ func getJob(client nomad.Job, r *http.Request) (*api.Job, error) {

return job, nil
}

func getAllocationReadyCount(client nomad.Job, job *api.Job, r *http.Request) (uint64, error) {
allocs, _, err := client.Allocations(*job.ID, true, nil)
var readyCount uint64

for _, a := range allocs {
for _, ts := range a.TaskStates {
if ts.State == "running" {
readyCount += 1
}
}
}

return readyCount, err
}
69 changes: 67 additions & 2 deletions handlers/replication_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,28 @@ func TestReplicationRReturnsFunctionWhenFound(t *testing.T) {
jobName := nomad.JobPrefix + functionName

h, rr, r := setupReplicationReader(functionName)
mockJob.On("Info", jobName, mock.Anything).
Return(&api.Job{ID: &jobName}, nil, nil)
mockJob.On("Info", jobName, mock.Anything).Return(
&api.Job{
ID: &jobName,
TaskGroups: []*api.TaskGroup{&api.TaskGroup{
Count: &count,
}},
},
nil,
nil,
)

mockJob.On("Allocations", jobName, true, mock.Anything).Return(
[]*api.AllocationListStub{
{
TaskStates: map[string]*api.TaskState{
"abc": &api.TaskState{State: "running"},
},
},
},
nil,
nil,
)
h(rr, r)

f := &requests.Function{}
Expand All @@ -63,3 +82,49 @@ func TestReplicationRReturnsFunctionWhenFound(t *testing.T) {
assert.Equal(t, rr.Header().Get("Content-Type"), "application/json")
assert.Equal(t, functionName, f.Name)
}

func TestReplicationRReturnsCorrectAllocationCount(t *testing.T) {
functionName := "tester"
jobName := nomad.JobPrefix + functionName
count := 2

h, rr, r := setupReplicationReader(functionName)
mockJob.On("Info", jobName, mock.Anything).Return(
&api.Job{
ID: &jobName,
TaskGroups: []*api.TaskGroup{&api.TaskGroup{
Count: &count,
}},
},
nil,
nil,
)

mockJob.On("Allocations", jobName, true, mock.Anything).Return(
[]*api.AllocationListStub{
{
TaskStates: map[string]*api.TaskState{
"abc": &api.TaskState{State: "running"},
},
},
{
TaskStates: map[string]*api.TaskState{
"abc": &api.TaskState{State: "pending"},
},
},
},
nil,
nil,
)

h(rr, r)

f := &requests.Function{}
err := json.NewDecoder(rr.Body).Decode(f)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, uint64(1), f.AvailableReplicas)
assert.Equal(t, uint64(2), f.Replicas)
}
1 change: 1 addition & 0 deletions nomad/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type Job interface {
Info(jobID string, q *api.QueryOptions) (*api.Job, *api.QueryMeta, error)
List(q *api.QueryOptions) ([]*api.JobListStub, *api.QueryMeta, error)
Deregister(jobID string, purge bool, q *api.WriteOptions) (string, *api.WriteMeta, error)
Allocations(jobID string, allAllocs bool, q *api.QueryOptions) ([]*api.AllocationListStub, *api.QueryMeta, error)
}
16 changes: 16 additions & 0 deletions nomad/mock_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,19 @@ func (m *MockJob) Deregister(jobID string, purge bool, q *api.WriteOptions) (

return "", nil, args.Error(2)
}

func (m *MockJob) Allocations(jobID string, allAllocs bool, q *api.QueryOptions) ([]*api.AllocationListStub, *api.QueryMeta, error) {
args := m.Called(jobID, allAllocs, q)

var allocs []*api.AllocationListStub
if a := args.Get(0); a != nil {
allocs = a.([]*api.AllocationListStub)
}

var meta *api.QueryMeta
if r := args.Get(1); r != nil {
meta = r.(*api.QueryMeta)
}

return allocs, meta, args.Error(2)
}
4 changes: 2 additions & 2 deletions nomad_job_files/faas.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ job "faas-nomadd" {
driver = "docker"

config {
image = "quay.io/nicholasjackson/faas-nomad:v0.2.23"
image = "quay.io/nicholasjackson/faas-nomad:v0.2.24"

args = [
"-nomad_region", "${NOMAD_REGION}",
Expand Down Expand Up @@ -76,7 +76,7 @@ EOH
}

config {
image = "functions/gateway:0.7.0"
image = "functions/gateway:0.7.5"

port_map {
http = 8080
Expand Down
9 changes: 1 addition & 8 deletions nomad_job_files/faas_travis.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ job "faas-nomadd" {
task "nomadd" {
driver = "docker"

env {
NOMAD_REGION = "${NOMAD_REGION}"
NOMAD_ADDR = "${NOMAD_IP_http}:4646"
CONSUL_ADDR = "${NOMAD_IP_http}:8500"
STATSD_ADDR = "${NOMAD_ADDR_statsd_statsd}"
}

config {
image = "localhost:5000/faas-nomad:latest"

Expand Down Expand Up @@ -80,7 +73,7 @@ EOH
}

config {
image = "functions/gateway:0.7.0"
image = "functions/gateway:0.7.5"

port_map {
http = 8080
Expand Down
Loading

0 comments on commit 070c68f

Please sign in to comment.