Add static front end + functions endpoint.

This commit is contained in:
Alex Ellis 2017-01-22 10:45:42 +00:00
parent b9bd7c8101
commit ff429ce493
10 changed files with 291 additions and 9 deletions

View File

@ -5,7 +5,7 @@ services:
- "/var/run/docker.sock:/var/run/docker.sock"
ports:
- 8080:8080
image: alexellis2/faas-gateway:latest-dev
image: alexellis2/faas-gateway:latest-dev2
networks:
- functions
deploy:
@ -94,6 +94,7 @@ services:
fprocess: "wc"
no_proxy: "gateway"
https_proxy: $https_proxy
networks:
functions:
driver: overlay

View File

@ -1,6 +1,9 @@
FROM alpine:latest
WORKDIR /root/
COPY gateway .
COPY assets assets
EXPOSE 8080
ENV http_proxy ""

View File

@ -6,6 +6,7 @@ RUN go get -d github.com/docker/docker/api/types \
&& go get -d github.com/docker/docker/client \
&& go get github.com/gorilla/mux \
&& go get github.com/prometheus/client_golang/prometheus
# RUN go get -d github.com/prometheus/client_model/go
WORKDIR /go/src/github.com/alexellis/faas/gateway
@ -14,5 +15,4 @@ COPY requests requests
COPY server.go .
COPY proxy.go .
RUN find /go/src/github.com/alexellis/faas/gateway/
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

102
gateway/assets/index.html Normal file
View File

@ -0,0 +1,102 @@
<html ng-app="faasGateway" >
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic" />
<!--<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">-->
<!-- Angular Material CSS now available via Google CDN; version 1.0.7 used here -->
<link rel="stylesheet" href="style/angular-material.min.css">
<link rel="stylesheet" href="style/bootstrap.css">
</head>
<body ng-controller="home">
<div layout="column" style="height:100%;" ng-cloak>
<section id="popupContainer" layout="row" flex>
<md-sidenav
class="md-sidenav-left"
md-component-id="left"
md-is-locked-open="$mdMedia('gt-sm')"
md-whiteframe="4" layout="column">
<md-toolbar class="md-theme-indigo">
<h1 class="md-toolbar-tools">FaaS Gateway</h1>
</md-toolbar>
<md-content layout-padding>
<md-button ng-click="newFunction()" ng-disabled="isFunctionBeingCreated" class="md-primary">New function</md-button>
<md-list>
<md-list-item ng-switch class="md-3-line" ng-click="showFunction(function)" ng-repeat="function in functions" ng-class="function.name == selectedFunction.name ? 'selected' : false">
<md-icon ng-switch-when="true" style="color: blue" md-svg-icon="person"></md-icon>
<md-icon ng-switch-when="false" md-svg-icon="person-outline"></md-icon>
<p>{{function.name}}</p>
<md-divider ng-if="!$last"></md-divider>
</md-list-item>
</md-list>
</md-content>
</md-sidenav>
<md-content flex layout-padding ng-if="!selectedFunction" ng-show="functions.length">
<div layout="column" layout-align="top center">
<p>Select a function.</p>
</div>
<div flex></div>
</md-content>
<md-content flex layout-padding ng-if="!functions.length">
<div layout="column" layout-align="top center">
<p>No functions found in FaaS.</p>
</div>
<div flex></div>
</md-content>
<md-content flex layout="column" ng-repeat="function in functions" ng-show="function.name == selectedFunction.name">
<md-card md-theme="default" md-theme-watch>
<md-card-title>
<md-card-title-text>
<span class="md-headline">
{{function.name}}
</span>
<div layout-gt-sm="row">
<md-input-container class="md-icon-float md-block">
<label>Replicas</label>
<input ng-model="function.replicas" type="text" readonly="readonly">
</md-input-container>
<md-input-container class="md-block" flex-gt-sm>
<label>Invocation count</label>
<input ng-model="function.invocationCount" type="text" readonly="readonly">
</md-input-container>
</div>
<div layout-gt-sm="row">
<md-input-container class="md-block" flex-gt-sm>
<label>Image</label>
<input ng-model="function.image" type="text" readonly="readonly">
</md-input-container>
</div>
<md-card-title-text>
</md-card-title>
</md-card>
</md-content>
</section>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
<script src="script/bootstrap.js"></script>
</body>
</html>

21
gateway/assets/script/bootstrap.js vendored Normal file
View File

@ -0,0 +1,21 @@
"use strict"
var app = angular.module('faasGateway', ['ngMaterial']);
app.controller("home", ['$scope', '$log', '$http', '$location', '$timeout', function($scope, $log, $http, $location, $timeout) {
$scope.functions = [];
$http.get("/system/functions").then(function(response) {
$scope.functions = response.data;
});
$scope.showFunction = function(fn) {
$scope.selectedFunction = fn;
};
// TODO: popup + form to create new Docker service.
$scope.newFunction = function() {
$scope.functions.push({
name: "f" +($scope.functions.length+2),
replicas: 0,
invokedCount: 0
});
};
}]);

50
gateway/assets/style/bootstrap.css vendored Normal file
View File

@ -0,0 +1,50 @@
@import url('https://fonts.googleapis.com/css?family=Rationale');
/*Taken from PWD, remove styles not used.*/
.selected button {
background-color: rgba(158,158,158,0.2);
}
md-card-content.terminal-container {
background-color: #000;
padding: 0;
}
.clock {
font-family: 'Rationale', sans-serif;
font-size: 3.0em;
color: #1da4eb;
text-align: center;
}
.welcome {
background-color: #e7e7e7;
}
.welcome > div {
text-align: center;
}
.welcome > div > img {
max-width: 100%;
}
.g-recaptcha div {
margin-left: auto;
margin-right: auto;
margin-bottom: auto;
margin-top: 50px;
}
.disconnected {
background-color: #FDF4B6;
}
md-input-container {
margin-bottom: 0;
}
md-input-container .md-errors-spacer {
height: 0;
min-height: 0;
}

View File

@ -10,4 +10,4 @@ docker rm -f gateway_extract
echo Building alexellis2/faas-gateway:latest
docker build -t alexellis2/faas-gateway:latest-dev .
docker build -t alexellis2/faas-gateway:latest-dev2 .

View File

@ -1,22 +1,78 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/alexellis/faas/gateway/metrics"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/gorilla/mux"
io_prometheus_client "github.com/prometheus/client_model/go"
)
func makeAlertHandler(c *client.Client) http.HandlerFunc {
func makeAlertHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println(c)
fmt.Println("Alert received.")
body, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(body))
// Todo: parse alert, validate alert and scale up or down function
fmt.Println("Alert received.")
w.WriteHeader(http.StatusOK)
}
}
// Function exported for system/functions endpoint
type Function struct {
Name string `json:"name"`
Image string `json:"image"`
InvocationCount float64 `json:"invocationCount"`
Replicas uint64 `json:"replicas"`
}
func makeFunctionReader(metricsOptions metrics.MetricOptions, c *client.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
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?)
functions := make([]Function, 0)
for _, service := range services {
counter, _ := metricsOptions.GatewayFunctionInvocation.GetMetricWithLabelValues(service.Spec.Name)
var pbmetric io_prometheus_client.Metric
counter.Write(&pbmetric)
invocations := pbmetric.GetCounter().GetValue()
f := Function{
Name: service.Spec.Name,
Image: service.Spec.TaskTemplate.ContainerSpec.Image,
InvocationCount: invocations,
Replicas: *service.Spec.Mode.Replicated.Replicas,
}
functions = append(functions, f)
}
functionBytes, _ := json.Marshal(functions)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(functionBytes)
}
}
@ -33,12 +89,14 @@ func main() {
r := mux.NewRouter()
r.HandleFunc("/function/{name:[a-zA-Z_]+}", MakeProxy(metricsOptions, true, dockerClient))
r.HandleFunc("/system/alert", makeAlertHandler(dockerClient))
r.HandleFunc("/", MakeProxy(metricsOptions, false, dockerClient))
r.HandleFunc("/system/alert", makeAlertHandler())
r.HandleFunc("/system/functions", makeFunctionReader(metricsOptions, dockerClient)).Methods("GET")
r.HandleFunc("/", MakeProxy(metricsOptions, false, dockerClient)).Methods("POST")
metricsHandler := metrics.PrometheusHandler()
r.Handle("/metrics", metricsHandler)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./assets/"))).Methods("GET")
s := &http.Server{
Addr: ":8080",
ReadTimeout: 8 * time.Second,

View File

@ -64,5 +64,5 @@ inhibit_rules:
receivers:
- name: 'scale-up'
webhook_configs:
- url: http://gateway:8080/function/func_webhookstash
- url: http://gateway:8080/system/alert
send_resolved: true

View File

@ -0,0 +1,47 @@
{
"receiver":"scale-up",
"status":"firing",
"alerts":[
{
"status":"firing",
"labels":{
"alertname":"APIHighInvocationRate",
"function_name":"func_echoit",
"instance":"gateway:8080",
"job":"gateway",
"monitor":"faas-monitor",
"service":"gateway",
"severity":"major",
"value":"8"
},
"annotations":{
"description":"High invocation total on gateway:8080",
"summary":"High invocation total on gateway:8080"
},
"startsAt":"2017-01-22T10:40:52.804Z",
"endsAt":"0001-01-01T00:00:00Z",
"generatorURL":"http://bb1b23e87070:9090/graph?g0.expr=rate%28gateway_function_invocation_total%5B10s%5D%29+%3E+5\u0026g0.tab=0"
}
],
"groupLabels":{
"alertname":"APIHighInvocationRate",
"service":"gateway"
},
"commonLabels":{
"alertname":"APIHighInvocationRate",
"function_name":"func_echoit",
"instance":"gateway:8080",
"job":"gateway",
"monitor":"faas-monitor",
"service":"gateway",
"severity":"major",
"value":"8"
},
"commonAnnotations":{
"description":"High invocation total on gateway:8080",
"summary":"High invocation total on gateway:8080"
},
"externalURL":"http://c052c835bcee:9093",
"version":"3",
"groupKey":18195285354214864953
}