Specify Linux constraints by default

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis 2017-09-01 09:18:55 +01:00
parent 1a78ec9234
commit 78daa9cd39
5 changed files with 185 additions and 98 deletions

View File

@ -5,16 +5,13 @@ services:
- "/var/run/docker.sock:/var/run/docker.sock"
ports:
- 8080:8080
image: functions/gateway:0.6.2
image: functions/gateway:0.6.3
networks:
- functions
environment:
dnsrr: "true" # Temporarily use dnsrr in place of VIP while issue persists on PWD
faas_nats_address: "nats"
faas_nats_port: 4222
deploy:
placement:
constraints: [node.role == manager]
# Start Add for NATS Streaming
depends_on:
- nats
@ -24,6 +21,11 @@ services:
delay: 5s
max_attempts: 20
window: 380s
placement:
constraints:
- 'node.role == manager'
- 'node.platform.os == linux'
nats:
image: nats-streaming:0.5.0
ports:
@ -32,6 +34,11 @@ services:
command: "--store memory --cluster_id faas-cluster"
networks:
- functions
deploy:
placement:
constraints:
- 'node.platform.os == linux'
queue-worker:
image: functions/queue-worker:0.1.1
networks:
@ -42,6 +49,10 @@ services:
delay: 5s
max_attempts: 20
window: 380s
placement:
constraints:
- 'node.platform.os == linux'
# End
prometheus:
@ -58,7 +69,9 @@ services:
- functions
deploy:
placement:
constraints: [node.role == manager]
constraints:
- 'node.role == manager'
- 'node.platform.os == linux'
alertmanager:
image: functions/alertmanager:latest # autobuild from Dockerfile in repo.
@ -74,7 +87,9 @@ services:
- 9093:9093
deploy:
placement:
constraints: [node.role == manager]
constraints:
- 'node.role == manager'
- 'node.platform.os == linux'
# Sample functions go here.
@ -90,6 +105,10 @@ services:
environment:
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Node.js gives OS info about the node (Host)
nodeinfo:
@ -103,6 +122,10 @@ services:
environment:
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Uses `cat` to echo back response, fastest function to execute.
echoit:
@ -117,6 +140,10 @@ services:
fprocess: "cat"
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
networks:
functions:

View File

@ -5,15 +5,16 @@ services:
- "/var/run/docker.sock:/var/run/docker.sock"
ports:
- 8080:8080
image: functions/gateway:0.6.2
image: functions/gateway:0.6.3
networks:
- functions
environment:
dnsrr: "true" # Temporarily use dnsrr in place of VIP while issue persists on PWD
deploy:
placement:
constraints: [node.role == manager]
constraints:
- 'node.role == manager'
- 'node.platform.os == linux'
prometheus:
image: functions/prometheus:latest # autobuild from Dockerfile in repo.
command: "-config.file=/etc/prometheus/prometheus.yml -storage.local.path=/prometheus -storage.local.memory-chunks=10000 --alertmanager.url=http://alertmanager:9093"
@ -28,7 +29,9 @@ services:
- functions
deploy:
placement:
constraints: [node.role == manager]
constraints:
- 'node.role == manager'
- 'node.platform.os == linux'
alertmanager:
image: functions/alertmanager:latest # autobuild from Dockerfile in repo.
@ -44,8 +47,9 @@ services:
- 9093:9093
deploy:
placement:
constraints: [node.role == manager]
constraints:
- 'node.role == manager'
- 'node.platform.os == linux'
# Sample functions go here.
# Service label of "function" allows functions to show up in UI on http://gateway:8080/
@ -60,6 +64,10 @@ services:
environment:
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Pass a username as an argument to find how many images user has pushed to Docker Hub.
hubstats:
@ -73,6 +81,11 @@ services:
environment:
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Node.js gives OS info about the node (Host)
nodeinfo:
@ -86,6 +99,11 @@ services:
environment:
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Uses `cat` to echo back response, fastest function to execute.
echoit:
@ -100,6 +118,11 @@ services:
fprocess: "cat"
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Counts words in request with `wc` utility
wordcount:
@ -115,6 +138,11 @@ services:
fprocess: "wc"
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Calculates base64 representation of request body.
base64:
@ -129,6 +157,11 @@ services:
fprocess: "base64"
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Decodes base64 representation of request body.
decodebase64:
@ -143,6 +176,10 @@ services:
fprocess: "base64 -d"
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
# Converts body in (markdown format) -> (html)
markdown:
@ -156,6 +193,10 @@ services:
environment:
no_proxy: "gateway"
https_proxy: $https_proxy
deploy:
placement:
constraints:
- 'node.platform.os == linux'
networks:
functions:

View File

@ -1,6 +1,3 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package handlers
import (
@ -9,103 +6,21 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"io/ioutil"
"github.com/alexellis/faas/gateway/metrics"
"github.com/alexellis/faas/gateway/requests"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/docker/docker/registry"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
)
func getCounterValue(service string, code string, metricsOptions *metrics.MetricOptions) float64 {
metric, err := metricsOptions.GatewayFunctionInvocation.
GetMetricWith(prometheus.Labels{"function_name": service, "code": code})
if err != nil {
return 0
}
// Get the metric's value from ProtoBuf interface (idea via Julius Volz)
var protoMetric io_prometheus_client.Metric
metric.Write(&protoMetric)
invocations := protoMetric.GetCounter().GetValue()
return invocations
}
func MakeDeleteFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
req := requests.DeleteFunctionRequest{}
defer r.Body.Close()
reqData, _ := ioutil.ReadAll(r.Body)
unmarshalErr := json.Unmarshal(reqData, &req)
if (len(req.FunctionName) == 0) || unmarshalErr != nil {
log.Printf("Error parsing request to remove service: %s\n", unmarshalErr)
w.WriteHeader(http.StatusBadRequest)
return
}
log.Printf("Attempting to remove service %s\n", req.FunctionName)
serviceFilter := filters.NewArgs()
options := types.ServiceListOptions{
Filters: serviceFilter,
}
services, err := c.ServiceList(context.Background(), options)
if err != nil {
fmt.Println(err)
}
// TODO: Filter only "faas" functions (via metadata?)
var serviceIDs []string
for _, service := range services {
isFunction := len(service.Spec.TaskTemplate.ContainerSpec.Labels["function"]) > 0
if isFunction && req.FunctionName == service.Spec.Name {
serviceIDs = append(serviceIDs, service.ID)
}
}
log.Println(len(serviceIDs))
if len(serviceIDs) == 0 {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(fmt.Sprintf("No such service found: %s.", req.FunctionName)))
return
}
var serviceRemoveErrors []error
for _, serviceID := range serviceIDs {
err := c.ServiceRemove(context.Background(), serviceID)
if err != nil {
serviceRemoveErrors = append(serviceRemoveErrors, err)
}
}
if len(serviceRemoveErrors) > 0 {
log.Printf("Error(s) removing service: %s\n", req.FunctionName)
log.Println(serviceRemoveErrors)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
}
// MakeNewFunctionHandler creates a new function (service) inside the swarm network.
func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client, maxRestarts uint64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@ -146,6 +61,7 @@ func MakeNewFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Clie
}
func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64) swarm.ServiceSpec {
linuxOnlyConstraints := []string{"node.platform.os == linux"}
nets := []swarm.NetworkAttachmentConfig{
{Target: request.Network},
@ -164,6 +80,9 @@ func makeSpec(request *requests.CreateFunctionRequest, maxRestarts uint64) swarm
Labels: map[string]string{"function": "true"},
},
Networks: nets,
Placement: &swarm.Placement{
Constraints: linuxOnlyConstraints,
},
},
Annotations: swarm.Annotations{
Name: request.Service,

View File

@ -0,0 +1,82 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package handlers
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"io/ioutil"
"github.com/alexellis/faas/gateway/metrics"
"github.com/alexellis/faas/gateway/requests"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
func MakeDeleteFunctionHandler(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
req := requests.DeleteFunctionRequest{}
defer r.Body.Close()
reqData, _ := ioutil.ReadAll(r.Body)
unmarshalErr := json.Unmarshal(reqData, &req)
if (len(req.FunctionName) == 0) || unmarshalErr != nil {
log.Printf("Error parsing request to remove service: %s\n", unmarshalErr)
w.WriteHeader(http.StatusBadRequest)
return
}
log.Printf("Attempting to remove service %s\n", req.FunctionName)
serviceFilter := filters.NewArgs()
options := types.ServiceListOptions{
Filters: serviceFilter,
}
services, err := c.ServiceList(context.Background(), options)
if err != nil {
fmt.Println(err)
}
// TODO: Filter only "faas" functions (via metadata?)
var serviceIDs []string
for _, service := range services {
isFunction := len(service.Spec.TaskTemplate.ContainerSpec.Labels["function"]) > 0
if isFunction && req.FunctionName == service.Spec.Name {
serviceIDs = append(serviceIDs, service.ID)
}
}
log.Println(len(serviceIDs))
if len(serviceIDs) == 0 {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(fmt.Sprintf("No such service found: %s.", req.FunctionName)))
return
}
var serviceRemoveErrors []error
for _, serviceID := range serviceIDs {
err := c.ServiceRemove(context.Background(), serviceID)
if err != nil {
serviceRemoveErrors = append(serviceRemoveErrors, err)
}
}
if len(serviceRemoveErrors) > 0 {
log.Printf("Error(s) removing service: %s\n", req.FunctionName)
log.Println(serviceRemoveErrors)
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
}

View File

@ -16,6 +16,8 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
)
// MakeFunctionReader gives a summary of Function structs with Docker service stats overlaid with Prometheus counters.
@ -68,3 +70,19 @@ func MakeFunctionReader(metricsOptions metrics.MetricOptions, c *client.Client)
w.Write(functionBytes)
}
}
func getCounterValue(service string, code string, metricsOptions *metrics.MetricOptions) float64 {
metric, err := metricsOptions.GatewayFunctionInvocation.
GetMetricWith(prometheus.Labels{"function_name": service, "code": code})
if err != nil {
return 0
}
// Get the metric's value from ProtoBuf interface (idea via Julius Volz)
var protoMetric io_prometheus_client.Metric
metric.Write(&protoMetric)
invocations := protoMetric.GetCounter().GetValue()
return invocations
}