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

Commit

Permalink
Move to faas-provider models
Browse files Browse the repository at this point in the history
Moves to use models from faas-provider instead of from the
gateway package.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Aug 5, 2019
1 parent 6b3410d commit ef7d913
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 136 deletions.
12 changes: 6 additions & 6 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 @@ -12,11 +12,11 @@

[[constraint]]
name = "github.com/openfaas/faas"
version = "0.15.0"
version = "0.16.0"

[[constraint]]
name = "github.com/openfaas/faas-provider"
version = "0.9.3"
version = "0.9.5"

# match docker/distribution revision with moby
[[override]]
Expand Down
13 changes: 6 additions & 7 deletions handlers/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
"github.com/docker/docker/client"
"github.com/docker/docker/registry"
units "github.com/docker/go-units"

"github.com/openfaas/faas/gateway/requests"
typesv1 "github.com/openfaas/faas-provider/types"
)

const annotationLabelPrefix = "com.openfaas.annotations."
Expand All @@ -36,7 +35,7 @@ func DeployHandler(c *client.Client, maxRestarts uint64, restartDelay time.Durat
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)

request := requests.CreateFunctionRequest{}
request := typesv1.FunctionDeployment{}
err := json.Unmarshal(body, &request)
if err != nil {
log.Println("Error parsing request:", err)
Expand Down Expand Up @@ -121,7 +120,7 @@ func lookupNetwork(c *client.Client) (string, error) {
return "", nil
}

func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64, restartDelay time.Duration, secrets []*swarm.SecretReference) (swarm.ServiceSpec, error) {
func makeSpec(request *typesv1.FunctionDeployment, maxRestarts uint64, restartDelay time.Duration, secrets []*swarm.SecretReference) (swarm.ServiceSpec, error) {
constraints := []string{}

if request.Constraints != nil && len(request.Constraints) > 0 {
Expand Down Expand Up @@ -267,7 +266,7 @@ func parseCPU(value string) (int64, error) {
return v, nil
}

func buildResources(request *requests.CreateFunctionRequest) *swarm.ResourceRequirements {
func buildResources(request *typesv1.FunctionDeployment) *swarm.ResourceRequirements {
var resources *swarm.ResourceRequirements

if request.Requests != nil || request.Limits != nil {
Expand Down Expand Up @@ -336,7 +335,7 @@ func buildResources(request *requests.CreateFunctionRequest) *swarm.ResourceRequ
return resources
}

func getMinReplicas(request *requests.CreateFunctionRequest) *uint64 {
func getMinReplicas(request *typesv1.FunctionDeployment) *uint64 {
replicas := uint64(1)

if request.Labels != nil {
Expand All @@ -351,7 +350,7 @@ func getMinReplicas(request *requests.CreateFunctionRequest) *uint64 {
return &replicas
}

func buildLabels(request *requests.CreateFunctionRequest) (map[string]string, error) {
func buildLabels(request *typesv1.FunctionDeployment) (map[string]string, error) {
labels := map[string]string{
"com.openfaas.function": request.Service,
"function": "true", // backwards-compatible
Expand Down
12 changes: 7 additions & 5 deletions handlers/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package handlers

import (
"fmt"
"github.com/openfaas/faas/gateway/requests"

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

"testing"
)

func Test_BuildLabels_Defaults(t *testing.T) {
request := &requests.CreateFunctionRequest{}
request := &typesv1.FunctionDeployment{}
val, err := buildLabels(request)

if err != nil {
Expand All @@ -28,7 +30,7 @@ func Test_BuildLabels_Defaults(t *testing.T) {
}

func Test_BuildLabels_WithAnnotations(t *testing.T) {
request := &requests.CreateFunctionRequest{
request := &typesv1.FunctionDeployment{
Labels: &map[string]string{"function_name": "echo"},
Annotations: &map[string]string{"current-time": "Wed 25 Jul 06:41:43 BST 2018"},
}
Expand All @@ -49,7 +51,7 @@ func Test_BuildLabels_WithAnnotations(t *testing.T) {
}

func Test_BuildLabels_NoAnnotations(t *testing.T) {
request := &requests.CreateFunctionRequest{
request := &typesv1.FunctionDeployment{
Labels: &map[string]string{"function_name": "echo"},
}

Expand All @@ -69,7 +71,7 @@ func Test_BuildLabels_NoAnnotations(t *testing.T) {
}

func Test_BuildLabels_KeyClash(t *testing.T) {
request := &requests.CreateFunctionRequest{
request := &typesv1.FunctionDeployment{
Labels: &map[string]string{
"function_name": "echo",
fmt.Sprintf("%scurrent-time", annotationLabelPrefix): "foo",
Expand Down
9 changes: 4 additions & 5 deletions handlers/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"

"github.com/openfaas/faas/gateway/requests"
typesv1 "github.com/openfaas/faas-provider/types"
)

// FunctionReader reads functions from Swarm metadata
Expand All @@ -36,8 +35,8 @@ func FunctionReader(wildcard bool, c client.ServiceAPIClient) http.HandlerFunc {
}
}

func readServices(c client.ServiceAPIClient) ([]requests.Function, error) {
functions := []requests.Function{}
func readServices(c client.ServiceAPIClient) ([]typesv1.FunctionStatus, error) {
functions := []typesv1.FunctionStatus{}
serviceFilter := filters.NewArgs()

options := types.ServiceListOptions{
Expand All @@ -57,7 +56,7 @@ func readServices(c client.ServiceAPIClient) ([]requests.Function, error) {
// Required (copy by value)
labels, annotations := buildLabelsAndAnnotations(service.Spec.Labels)

f := requests.Function{
f := typesv1.FunctionStatus{
Name: service.Spec.Name,
Image: service.Spec.TaskTemplate.ContainerSpec.Image,
InvocationCount: 0,
Expand Down
5 changes: 3 additions & 2 deletions handlers/replica_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/gorilla/mux"
"github.com/openfaas/faas/gateway/requests"

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

// ReplicaReader reads replica and image status data from a function
Expand All @@ -31,7 +32,7 @@ func ReplicaReader(c *client.Client) http.HandlerFunc {
return
}

var found *requests.Function
var found *typesv1.FunctionStatus
for _, function := range functions {
if function.Name == functionName {
found = &function
Expand Down
56 changes: 28 additions & 28 deletions handlers/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"testing"

"github.com/openfaas/faas/gateway/requests"
typesv1 "github.com/openfaas/faas-provider/types"
)

// Test_ParseMemory exploratory testing to document how to convert
Expand All @@ -31,9 +31,9 @@ func megaBytes(mbs int64) int64 {

func Test_ParseMemoryLimits(t *testing.T) {
want := int64(256)
req := requests.CreateFunctionRequest{
Requests: &requests.FunctionResources{},
Limits: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Requests: &typesv1.FunctionResources{},
Limits: &typesv1.FunctionResources{
Memory: fmt.Sprintf("%d m", want),
},
}
Expand All @@ -47,11 +47,11 @@ func Test_ParseMemoryLimits(t *testing.T) {

func Test_ParseMemoryRequests(t *testing.T) {
want := int64(256)
req := requests.CreateFunctionRequest{
Requests: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Requests: &typesv1.FunctionResources{
Memory: fmt.Sprintf("%d m", want),
},
Limits: &requests.FunctionResources{},
Limits: &typesv1.FunctionResources{},
}

res := buildResources(&req)
Expand All @@ -62,11 +62,11 @@ func Test_ParseMemoryRequests(t *testing.T) {
}

func Test_ParseInvalidMemoryInMegabytes(t *testing.T) {
req := requests.CreateFunctionRequest{
Requests: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Requests: &typesv1.FunctionResources{
Memory: "wrong",
},
Limits: &requests.FunctionResources{},
Limits: &typesv1.FunctionResources{},
}

res := buildResources(&req)
Expand All @@ -77,11 +77,11 @@ func Test_ParseInvalidMemoryInMegabytes(t *testing.T) {
}

func TestInvalidMemoryRequests_Ignored(t *testing.T) {
req := requests.CreateFunctionRequest{
Requests: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Requests: &typesv1.FunctionResources{
Memory: "invalid",
},
Limits: &requests.FunctionResources{},
Limits: &typesv1.FunctionResources{},
}

res := buildResources(&req)
Expand All @@ -92,11 +92,11 @@ func TestInvalidMemoryRequests_Ignored(t *testing.T) {
}

func TestInvalidMemoryLimits_Ignored(t *testing.T) {
req := requests.CreateFunctionRequest{
Limits: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Limits: &typesv1.FunctionResources{
Memory: "invalid",
},
Requests: &requests.FunctionResources{},
Requests: &typesv1.FunctionResources{},
}

res := buildResources(&req)
Expand All @@ -109,9 +109,9 @@ func TestInvalidMemoryLimits_Ignored(t *testing.T) {
func TestBuildSwarmResourcesAddsCPULimits(t *testing.T) {
want := int64(1000000)

req := requests.CreateFunctionRequest{
Requests: &requests.FunctionResources{},
Limits: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Requests: &typesv1.FunctionResources{},
Limits: &typesv1.FunctionResources{
CPU: fmt.Sprintf("%d", want),
},
}
Expand All @@ -125,11 +125,11 @@ func TestBuildSwarmResourcesAddsCPULimits(t *testing.T) {

func TestBuildSwarmResourcesAddsCPUReservations(t *testing.T) {
want := int64(1000000)
req := requests.CreateFunctionRequest{
Requests: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Requests: &typesv1.FunctionResources{
CPU: fmt.Sprintf("%d", want),
},
Limits: &requests.FunctionResources{},
Limits: &typesv1.FunctionResources{},
}

res := buildResources(&req)
Expand All @@ -140,9 +140,9 @@ func TestBuildSwarmResourcesAddsCPUReservations(t *testing.T) {
}

func TestInvalidCPULimits_Ignored(t *testing.T) {
req := requests.CreateFunctionRequest{
Requests: &requests.FunctionResources{},
Limits: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Requests: &typesv1.FunctionResources{},
Limits: &typesv1.FunctionResources{
CPU: "invalid",
},
}
Expand All @@ -155,9 +155,9 @@ func TestInvalidCPULimits_Ignored(t *testing.T) {
}

func TestInvalidCPURequests_Ignored(t *testing.T) {
req := requests.CreateFunctionRequest{
Limits: &requests.FunctionResources{},
Requests: &requests.FunctionResources{
req := typesv1.FunctionDeployment{
Limits: &typesv1.FunctionResources{},
Requests: &typesv1.FunctionResources{
CPU: "invalid",
},
}
Expand Down
6 changes: 3 additions & 3 deletions handlers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"

"github.com/openfaas/faas/gateway/requests"
typesv1 "github.com/openfaas/faas-provider/types"
)

// UpdateHandler updates an existng function
Expand All @@ -26,7 +26,7 @@ func UpdateHandler(c *client.Client, maxRestarts uint64, restartDelay time.Durat
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)

request := requests.CreateFunctionRequest{}
request := typesv1.FunctionDeployment{}
err := json.Unmarshal(body, &request)
if err != nil {
log.Println("Error parsing request:", err)
Expand Down Expand Up @@ -104,7 +104,7 @@ func UpdateHandler(c *client.Client, maxRestarts uint64, restartDelay time.Durat
}
}

func updateSpec(request *requests.CreateFunctionRequest, spec *swarm.ServiceSpec, maxRestarts uint64, restartDelay time.Duration, secrets []*swarm.SecretReference) error {
func updateSpec(request *typesv1.FunctionDeployment, spec *swarm.ServiceSpec, maxRestarts uint64, restartDelay time.Duration, secrets []*swarm.SecretReference) error {

constraints := []string{}
if request.Constraints != nil && len(request.Constraints) > 0 {
Expand Down
5 changes: 3 additions & 2 deletions test/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"net/http/httptest"
"testing"

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

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

Expand All @@ -25,7 +26,7 @@ func TestMakeInfoHandler(t *testing.T) {
}

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

handler(rr, req)
body, err := ioutil.ReadAll(rr.Body)
Expand Down
5 changes: 3 additions & 2 deletions test/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/openfaas/faas-swarm/handlers"
"github.com/openfaas/faas/gateway/requests"
"golang.org/x/net/context"

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

type testServiceApiClient struct {
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestReaderSuccessReturnsCorrectBodyWithOneFunction(t *testing.T) {
r := &http.Request{}
handler.ServeHTTP(w, r)

functions := []requests.Function{
functions := []typesv1.FunctionStatus{
{
Name: "bar",
Image: "foo/bar:latest",
Expand Down
Loading

0 comments on commit ef7d913

Please sign in to comment.