Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
Add /system/info endpoint for faas-swarm
Browse files Browse the repository at this point in the history
The purpose of this change is to make faas-swarm compatible with the
latest version of the API Gateway in OpenFaaS. Each provider should
be able to advertise the underlying provider and the version of the
code.

A new version of faas-provider (0.6) is vendored into the repo
which brings in an optional health endpoint (turned off by default).

The version and SHA is being passed to the provider from server.go.
Dockerfiles are updated to inject version and SHA string at build time in
version.go.

A unit test has been added for the /system/info endpoint.

Signed-off-by: Vivek Singh <[email protected]>
  • Loading branch information
viveksyngh authored and alexellis committed Mar 26, 2018
1 parent 4924bbf commit 7efdcf4
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 17 deletions.
12 changes: 7 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ RUN mkdir -p /go/src/github.com/openfaas/faas-swarm/

WORKDIR /go/src/github.com/openfaas/faas-swarm

COPY vendor vendor
COPY handlers handlers
COPY types types
COPY server.go .
COPY . .

RUN curl -sL https://github.com/alexellis/license-check/releases/download/0.2.2/license-check > /usr/bin/license-check \
&& chmod +x /usr/bin/license-check
RUN license-check -path ./ --verbose=false "Alex Ellis" "OpenFaaS Project"

RUN gofmt -l -d $(find . -type f -name '*.go' -not -path "./vendor/*") \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o faas-swarm .
&& VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') \
&& GIT_COMMIT=$(git rev-list -1 HEAD) \
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w \
-X github.com/openfaas/faas-swarm/version.GitCommit=${GIT_COMMIT}\
-X github.com/openfaas/faas-swarm/version.Version=${VERSION}" \
-a -installsuffix cgo -o faas-swarm .

# Release stage
FROM alpine:3.6
Expand Down
13 changes: 7 additions & 6 deletions Dockerfile.armhf
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ RUN mkdir -p /go/src/github.com/openfaas/faas-swarm/

WORKDIR /go/src/github.com/openfaas/faas-swarm

COPY vendor vendor
COPY handlers handlers
COPY types types

COPY server.go .
COPY . .

#RUN curl -sL https://github.com/alexellis/license-check/releases/download/0.2.2/license-check > /usr/bin/license-check \
# && chmod +x /usr/bin/license-check
#RUN license-check -path ./ --verbose=false "Alex Ellis" "OpenFaaS Project"

RUN gofmt -l -d $(find . -type f -name '*.go' -not -path "./vendor/*") \
&& GOARM=6 CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o faas-swarm .
&& VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') \
&& GIT_COMMIT=$(git rev-list -1 HEAD) \
&& GOARM=6 CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w \
-X github.com/openfaas/faas-swarm/version.GitCommit=${GIT_COMMIT}\
-X github.com/openfaas/faas-swarm/version.Version=${VERSION}" \
-a -installsuffix cgo -o faas-swarm .

# Release stage
FROM alpine:3.6
Expand Down
6 changes: 3 additions & 3 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

# match docker/distribution revision with moby
[[override]]
Expand Down
43 changes: 43 additions & 0 deletions handlers/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handlers

import (
"encoding/json"
"net/http"

"github.com/openfaas/faas-provider/types"
)

const (
//SwarmIdentifier identifier string for swarm provider
SwarmIdentifier = "swarm"
//SwarmProvider provider string for swarm provider
SwarmProvider = "faas-swarm"
)

//MakeInfoHandler creates handler for /system/info endpoint
func MakeInfoHandler(version, sha string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
defer r.Body.Close()
}

infoRequest := types.InfoRequest{
Orchestration: SwarmIdentifier,
Provider: SwarmProvider,
Version: types.ProviderVersion{
Release: version,
SHA: sha,
},
}

jsonOut, marshalErr := json.Marshal(infoRequest)
if marshalErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonOut)
}
}
2 changes: 2 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
bootTypes "github.com/openfaas/faas-provider/types"
"github.com/openfaas/faas-swarm/handlers"
"github.com/openfaas/faas-swarm/types"
"github.com/openfaas/faas-swarm/version"
)

func main() {
Expand Down Expand Up @@ -52,6 +53,7 @@ func main() {
ReplicaUpdater: handlers.ReplicaUpdater(dockerClient),
UpdateHandler: handlers.UpdateHandler(dockerClient, maxRestarts, restartDelay),
Health: handlers.Health(),
InfoHandler: handlers.MakeInfoHandler(version.BuildVersion(), version.GitCommit),
}

bootstrapConfig := bootTypes.FaaSConfig{
Expand Down
60 changes: 60 additions & 0 deletions test/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package test

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/openfaas/faas-provider/types"
"github.com/openfaas/faas-swarm/handlers"
)

const (
infoTestVersion = "swarmtest"
infoTestSHA = "test"
)

func TestMakeInfoHandler(t *testing.T) {
rr := httptest.NewRecorder()

req, err := http.NewRequest("GET", "/system/info", nil)
if err != nil {
t.Fatal(err)
}

handler := handlers.MakeInfoHandler(infoTestVersion, infoTestSHA)
infoRequest := types.InfoRequest{}

handler(rr, req)
body, err := ioutil.ReadAll(rr.Body)
if err != nil {
t.Fatal(err)
}

err = json.Unmarshal(body, &infoRequest)
if err != nil {
t.Fatal(err)
}

if required := http.StatusOK; rr.Code != required {
t.Errorf("handler returned wrong status code - want: %v, got: %v", required, rr.Code)
}

if infoRequest.Orchestration != handlers.SwarmIdentifier {
t.Errorf("handler returned wrong orchestration - want: %v, got: %v", handlers.SwarmIdentifier, infoRequest.Orchestration)
}

if infoRequest.Provider != handlers.SwarmProvider {
t.Errorf("handler returned wrong provider - want: %v, got: %v", handlers.SwarmProvider, infoRequest.Provider)
}

if infoRequest.Version.Release != infoTestVersion {
t.Errorf("handler returned wrong release version - want: %v, got: %v", infoTestVersion, infoRequest.Version.Release)
}

if infoRequest.Version.SHA != infoTestSHA {
t.Errorf("handler returned wrong SHA string - want: %v, got: %v", infoTestSHA, infoRequest.Version.SHA)
}
}
21 changes: 21 additions & 0 deletions vendor/github.com/openfaas/faas-provider/Gopkg.lock

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

25 changes: 25 additions & 0 deletions vendor/github.com/openfaas/faas-provider/Gopkg.toml

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

1 change: 1 addition & 0 deletions vendor/github.com/openfaas/faas-provider/README.md

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

2 changes: 2 additions & 0 deletions vendor/github.com/openfaas/faas-provider/serve.go

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

1 change: 1 addition & 0 deletions vendor/github.com/openfaas/faas-provider/types/config.go

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

13 changes: 13 additions & 0 deletions vendor/github.com/openfaas/faas-provider/types/requests.go

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

2 changes: 0 additions & 2 deletions vendor/github.com/openfaas/faas-provider/vendor.conf

This file was deleted.

18 changes: 18 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package version

var (
//Version release version of the provider
Version string
//GitCommit SHA of the last git commit
GitCommit string
//DevVerison string for the development version
DevVerison = "dev"
)

//BuildVersion returns current version of the provider
func BuildVersion() string {
if len(Version) == 0 {
return DevVerison
}
return Version
}

0 comments on commit 7efdcf4

Please sign in to comment.