Provide override for replica scaling to cap out at custom value.

This commit is contained in:
Alex Ellis 2017-04-06 22:36:59 +01:00
parent 8579f3a810
commit a71329de4a
2 changed files with 16 additions and 5 deletions

View File

@ -111,6 +111,7 @@ services:
image: functions/alpine:latest image: functions/alpine:latest
labels: labels:
function: "true" function: "true"
com.faas.max_replicas: "10"
depends_on: depends_on:
- gateway - gateway
networks: networks:

View File

@ -7,6 +7,8 @@ import (
"log" "log"
"net/http" "net/http"
"strconv"
"github.com/alexellis/faas/gateway/requests" "github.com/alexellis/faas/gateway/requests"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/client" "github.com/docker/docker/client"
@ -33,7 +35,6 @@ func MakeAlertHandler(c *client.Client) http.HandlerFunc {
} }
if len(req.Alerts) > 0 { if len(req.Alerts) > 0 {
if err := scaleService(req, c); err != nil { if err := scaleService(req, c); err != nil {
log.Println(err) log.Println(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -45,15 +46,15 @@ func MakeAlertHandler(c *client.Client) http.HandlerFunc {
} }
// CalculateReplicas decides what replica count to set depending on a Prometheus alert // CalculateReplicas decides what replica count to set depending on a Prometheus alert
func CalculateReplicas(status string, currentReplicas uint64) uint64 { func CalculateReplicas(status string, currentReplicas uint64, maxReplicas uint64) uint64 {
newReplicas := currentReplicas newReplicas := currentReplicas
if status == "firing" { if status == "firing" {
if currentReplicas == 1 { if currentReplicas == 1 {
newReplicas = 5 newReplicas = 5
} else { } else {
if currentReplicas+5 > 20 { if currentReplicas+5 > maxReplicas {
newReplicas = 20 newReplicas = maxReplicas
} else { } else {
newReplicas = currentReplicas + 5 newReplicas = currentReplicas + 5
} }
@ -75,7 +76,16 @@ func scaleService(req requests.PrometheusAlert, c *client.Client) error {
currentReplicas := *service.Spec.Mode.Replicated.Replicas currentReplicas := *service.Spec.Mode.Replicated.Replicas
status := req.Status status := req.Status
newReplicas := CalculateReplicas(status, currentReplicas)
replicaLabel := service.Spec.TaskTemplate.ContainerSpec.Labels["com.faas.max_replicas"]
maxReplicas := 20
if len(replicaLabel) > 0 {
maxReplicas, err = strconv.Atoi(replicaLabel)
if err != nil {
log.Printf("Bad replica count: %s, should be uint.\n", replicaLabel)
}
}
newReplicas := CalculateReplicas(status, currentReplicas, uint64(maxReplicas))
if newReplicas == currentReplicas { if newReplicas == currentReplicas {
return nil return nil