-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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) | ||
} | ||
} |
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) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
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 | ||
} |