diff --git a/gateway/handlers/createhandler.go b/gateway/handlers/createhandler.go index 06f783628..c813c78e9 100644 --- a/gateway/handlers/createhandler.go +++ b/gateway/handlers/createhandler.go @@ -19,6 +19,7 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" "github.com/docker/docker/registry" + ) var linuxOnlyConstraints = []string{"node.platform.os == linux"} @@ -88,6 +89,7 @@ func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64, resta ContainerSpec: swarm.ContainerSpec{ Image: request.Image, Labels: map[string]string{"function": "true"}, + Mounts: request.Mounts, }, Networks: nets, Placement: &swarm.Placement{ diff --git a/gateway/requests/requests.go b/gateway/requests/requests.go index a0a019ac9..920aa04a2 100644 --- a/gateway/requests/requests.go +++ b/gateway/requests/requests.go @@ -3,6 +3,8 @@ package requests +import "github.com/docker/docker/api/types/mount" + // CreateFunctionRequest create a function in the swarm. type CreateFunctionRequest struct { // Service corresponds to a Docker Service @@ -30,6 +32,9 @@ type CreateFunctionRequest struct { // Secrets list of secrets to be made available to function Secrets []string `json:"secrets"` + + // Mounts to be mounted by the container running the function + Mounts []mount.Mount `json:"mounts,omitempty"` } // DeleteFunctionRequest delete a deployed function diff --git a/gateway/tests/integration/createfunction_test.go b/gateway/tests/integration/createfunction_test.go index 019e49989..2657702fe 100644 --- a/gateway/tests/integration/createfunction_test.go +++ b/gateway/tests/integration/createfunction_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/openfaas/faas/gateway/requests" + "github.com/docker/docker/api/types/mount" ) func createFunction(request requests.CreateFunctionRequest) (string, int, error) { @@ -46,6 +47,43 @@ func TestCreate_ValidRequest(t *testing.T) { deleteFunction("test_resizer") } +func TestCreate_ValidMountRequest(t *testing.T) { + mounts := []mount.Mount{ + mount.Mount{ + Type: "volume", + Source: "Test1", + Target: "/tmp", + }, + mount.Mount{ + Type: "bind", + Source: "/tmp", + Target: "/tmp", + }, + } + request := requests.CreateFunctionRequest{ + Service: "test_resizer", + Image: "functions/resizer", + Network: "func_functions", + Mounts: mounts, + EnvProcess: "", + } + + _, code, err := createFunction(request) + + if err != nil { + t.Log(err) + t.Fail() + } + + expectedErrorCode := http.StatusOK + if code != expectedErrorCode { + t.Errorf("Got HTTP code: %d, want %d\n", code, expectedErrorCode) + return + } + + deleteFunction("test_resizer") +} + func TestCreate_InvalidImage(t *testing.T) { request := requests.CreateFunctionRequest{ Service: "test_resizer",