diff --git a/Dockerfile b/Dockerfile index 11d9b073..3974b419 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Dockerfile.armhf b/Dockerfile.armhf index 17dc58c2..492446a1 100644 --- a/Dockerfile.armhf +++ b/Dockerfile.armhf @@ -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 diff --git a/Gopkg.lock b/Gopkg.lock index 4c9f9498..64686f58 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -141,8 +141,8 @@ ".", "types" ] - revision = "c92df42e53902c7bc550436fd0ecc5790fdb11a4" - version = "0.5" + revision = "ffce01238ccf01b0f4e573fb65b92b285a547a4b" + version = "0.6" [[projects]] name = "github.com/pkg/errors" @@ -184,6 +184,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "011a3f7bbf67fa6fd4591903e0db88431a0d7ec2ebf9fa0de2f70a1e6065ac37" + inputs-digest = "98c6d4cb42aadad9ed299f56438cc8eaa3d3ed32216468b490d3e4faface3bd1" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index e744065c..21bc227b 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -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]] diff --git a/handlers/info.go b/handlers/info.go new file mode 100644 index 00000000..b21d0ee8 --- /dev/null +++ b/handlers/info.go @@ -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) + } +} diff --git a/server.go b/server.go index d0eb8360..61e35b41 100644 --- a/server.go +++ b/server.go @@ -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() { @@ -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{ diff --git a/test/info_test.go b/test/info_test.go new file mode 100644 index 00000000..28200f9b --- /dev/null +++ b/test/info_test.go @@ -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) + } +} diff --git a/vendor/github.com/openfaas/faas-provider/Gopkg.lock b/vendor/github.com/openfaas/faas-provider/Gopkg.lock new file mode 100644 index 00000000..65cc582b --- /dev/null +++ b/vendor/github.com/openfaas/faas-provider/Gopkg.lock @@ -0,0 +1,21 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/gorilla/context" + packages = ["."] + revision = "1ea25387ff6f684839d82767c1733ff4d4d15d0a" + version = "v1.1" + +[[projects]] + name = "github.com/gorilla/mux" + packages = ["."] + revision = "7f08801859139f86dfafd1c296e2cba9a80d292e" + version = "v1.6.0" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "22efb1c7d9d2403520db6d9a878b2f1e52741e51425cbda743cfd25f00c84a9b" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/vendor/github.com/openfaas/faas-provider/Gopkg.toml b/vendor/github.com/openfaas/faas-provider/Gopkg.toml new file mode 100644 index 00000000..fa1dcd42 --- /dev/null +++ b/vendor/github.com/openfaas/faas-provider/Gopkg.toml @@ -0,0 +1,25 @@ +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" + + +[[constraint]] + name = "github.com/gorilla/mux" + version = "1.6.0" diff --git a/vendor/github.com/openfaas/faas-provider/README.md b/vendor/github.com/openfaas/faas-provider/README.md index e0e3f746..691884cd 100644 --- a/vendor/github.com/openfaas/faas-provider/README.md +++ b/vendor/github.com/openfaas/faas-provider/README.md @@ -25,6 +25,7 @@ I.e.: FunctionReader: handlers.MakeFunctionReader(clientset), ReplicaReader: handlers.MakeReplicaReader(clientset), ReplicaUpdater: handlers.MakeReplicaUpdater(clientset), + InfoHandler: handlers.MakeInfoHandler(), } var port int port = 8080 diff --git a/vendor/github.com/openfaas/faas-provider/serve.go b/vendor/github.com/openfaas/faas-provider/serve.go index ffd0ebff..83c29ba8 100644 --- a/vendor/github.com/openfaas/faas-provider/serve.go +++ b/vendor/github.com/openfaas/faas-provider/serve.go @@ -37,6 +37,8 @@ func Serve(handlers *types.FaaSHandlers, config *types.FaaSConfig) { r.HandleFunc("/function/{name:[-a-zA-Z_0-9]+}", handlers.FunctionProxy) r.HandleFunc("/function/{name:[-a-zA-Z_0-9]+}/", handlers.FunctionProxy) + r.HandleFunc("/system/info", handlers.InfoHandler).Methods("GET") + if config.EnableHealth { r.HandleFunc("/healthz", handlers.Health).Methods("GET") } diff --git a/vendor/github.com/openfaas/faas-provider/types/config.go b/vendor/github.com/openfaas/faas-provider/types/config.go index c02b9807..53857ca8 100644 --- a/vendor/github.com/openfaas/faas-provider/types/config.go +++ b/vendor/github.com/openfaas/faas-provider/types/config.go @@ -17,6 +17,7 @@ type FaaSHandlers struct { // Optional: Update an existing function UpdateHandler http.HandlerFunc Health http.HandlerFunc + InfoHandler http.HandlerFunc } // FaaSConfig set config for HTTP handlers diff --git a/vendor/github.com/openfaas/faas-provider/types/requests.go b/vendor/github.com/openfaas/faas-provider/types/requests.go index 27efd0b2..4442ca21 100644 --- a/vendor/github.com/openfaas/faas-provider/types/requests.go +++ b/vendor/github.com/openfaas/faas-provider/types/requests.go @@ -7,3 +7,16 @@ type ScaleServiceRequest struct { ServiceName string `json:"serviceName"` Replicas uint64 `json:"replicas"` } + +// InfoRequest provides information about the underlying provider +type InfoRequest struct { + Provider string `json:"provider"` + Version ProviderVersion `json:"version"` + Orchestration string `json:"orchestration"` +} + +// ProviderVersion provides the commit sha and release version number of the underlying provider +type ProviderVersion struct { + SHA string `json:"sha"` + Release string `json:"release"` +} diff --git a/vendor/github.com/openfaas/faas-provider/vendor.conf b/vendor/github.com/openfaas/faas-provider/vendor.conf deleted file mode 100644 index 694188ee..00000000 --- a/vendor/github.com/openfaas/faas-provider/vendor.conf +++ /dev/null @@ -1,2 +0,0 @@ -github.com/gorilla/mux 24fca303ac6da784b9e8269f724ddeb0b2eea5e7 -github.com/gorilla/context 08b5f424b9271eedf6f9f0ce86cb9396ed337a42 diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000..cc31998f --- /dev/null +++ b/version/version.go @@ -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 +}