Merge master into breakout_swarm

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis
2018-02-01 09:25:39 +00:00
parent afeb7bbce4
commit f954bf0733
1953 changed files with 614131 additions and 175582 deletions

View File

@ -4,17 +4,13 @@
package handlers
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/openfaas/faas/gateway/requests"
)
@ -27,88 +23,6 @@ const MinScaleLabel = "com.openfaas.scale.min"
// MaxScaleLabel label indicating max scale for a function
const MaxScaleLabel = "com.openfaas.scale.max"
// ServiceQuery provides interface for replica querying/setting
type ServiceQuery interface {
GetReplicas(service string) (currentReplicas uint64, maxReplicas uint64, minReplicas uint64, err error)
SetReplicas(service string, count uint64) error
}
// NewSwarmServiceQuery create new Docker Swarm implementation
func NewSwarmServiceQuery(c *client.Client) ServiceQuery {
return SwarmServiceQuery{
c: c,
}
}
// SwarmServiceQuery implementation for Docker Swarm
type SwarmServiceQuery struct {
c *client.Client
}
// GetReplicas replica count for function
func (s SwarmServiceQuery) GetReplicas(serviceName string) (uint64, uint64, uint64, error) {
var err error
var currentReplicas uint64
maxReplicas := uint64(DefaultMaxReplicas)
minReplicas := uint64(1)
opts := types.ServiceInspectOptions{
InsertDefaults: true,
}
service, _, err := s.c.ServiceInspectWithRaw(context.Background(), serviceName, opts)
if err == nil {
currentReplicas = *service.Spec.Mode.Replicated.Replicas
minScale := service.Spec.Annotations.Labels[MinScaleLabel]
maxScale := service.Spec.Annotations.Labels[MaxScaleLabel]
if len(maxScale) > 0 {
labelValue, err := strconv.Atoi(maxScale)
if err != nil {
log.Printf("Bad replica count: %s, should be uint", maxScale)
} else {
maxReplicas = uint64(labelValue)
}
}
if len(minScale) > 0 {
labelValue, err := strconv.Atoi(maxScale)
if err != nil {
log.Printf("Bad replica count: %s, should be uint", minScale)
} else {
minReplicas = uint64(labelValue)
}
}
}
return currentReplicas, maxReplicas, minReplicas, err
}
// SetReplicas update the replica count
func (s SwarmServiceQuery) SetReplicas(serviceName string, count uint64) error {
opts := types.ServiceInspectOptions{
InsertDefaults: true,
}
service, _, err := s.c.ServiceInspectWithRaw(context.Background(), serviceName, opts)
if err == nil {
service.Spec.Mode.Replicated.Replicas = &count
updateOpts := types.ServiceUpdateOptions{}
updateOpts.RegistryAuthFrom = types.RegistryAuthFromSpec
_, updateErr := s.c.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, updateOpts)
if updateErr != nil {
err = updateErr
}
}
return err
}
// MakeAlertHandler handles alerts from Prometheus Alertmanager
func MakeAlertHandler(service ServiceQuery) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {