Skip to content

Commit

Permalink
Docker service config and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mricher committed Jul 19, 2016
1 parent b45c5cb commit f4f7d3a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
24 changes: 20 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ func parseConfig() (*core.Config, error) {
Scalers: make(map[string]scaler.Scaler, 5),
Rules: make([]rule.Rule, 0),
}
// Parse scalers

// check endpoint
if viper.GetString("endpoint") == "" {
return nil, fmt.Errorf("No endpoint specified")
}

// parse scalers
var scalers = viper.Sub("scalers")
for _, name := range scalers.AllKeys() {
log.Info("Add scaler [" + name + "]")
Expand All @@ -58,6 +64,14 @@ func parseConfig() (*core.Config, error) {
return nil, errors.New("No target specified for docker-compose scaler [" + name + "]")
}
config.Scalers[name] = scaler.NewComposeScaler(s.GetString("target"), s.GetString("config"))
case "docker-service":
if s.GetString("service") == "" {
return nil, errors.New("No service specified for docker-service scaler [" + name + "]")
}
config.Scalers[name] = &scaler.ServiceScaler{
ServiceID: s.GetString("service"),
EngineSocket: viper.GetString("endpoint"),
}
case "":
return nil, fmt.Errorf("Unknown scaler type: %s\n", s.GetString("type"))
}
Expand All @@ -69,14 +83,14 @@ func parseConfig() (*core.Config, error) {
target := rules.Sub(r).GetString("target")
log.Info("Add service [" + target + "]")

// Check scaler
// check scaler
// TODO external function
scaler := rules.Sub(r).GetString("scaler")
if config.Scalers[scaler] == nil {
return nil, errors.New("No scaler specified for rule [" + r + "]")
}

// Parse rules
// parse rules
up, err := rule.Decode(rules.Sub(r).GetString("up"))
if err != nil {
return nil, errors.New(target + fmt.Sprintf(": %v up", err))
Expand All @@ -86,7 +100,7 @@ func parseConfig() (*core.Config, error) {
return nil, errors.New(target + fmt.Sprintf(": %v down", err))
}

// Pick probe
// pick probe
var p probe.Probe
refProbe := rules.Sub(r).GetString("probe")
splittedProbe := strings.Split(refProbe, ".")
Expand All @@ -111,6 +125,8 @@ func parseConfig() (*core.Config, error) {
p = &probe.Command{
Cmd: rules.Sub(r).GetString("cmd"),
}
case "mock":
p = &probe.DefaultScalingProbe{}
default:
return nil, errors.New("Unknown probe " + splittedProbe[0])
}
Expand Down
10 changes: 6 additions & 4 deletions core/scaler/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package scaler
import (
"fmt"

log "github.com/Sirupsen/logrus"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"golang.org/x/net/context"
)

// ServiceScaler work with docker 1.12 swarm services (API 1.24)
type ServiceScaler struct {
serviceID string
engineSocket string
ServiceID string
EngineSocket string
cli *client.Client
}

Expand Down Expand Up @@ -46,7 +47,7 @@ func (s *ServiceScaler) scaleService(scale func(uint64) uint64) error {
return err
}
ctx := context.Background()
service, _, err := cli.ServiceInspectWithRaw(ctx, s.serviceID)
service, _, err := cli.ServiceInspectWithRaw(ctx, s.ServiceID)
if err != nil {
return err
}
Expand All @@ -55,6 +56,7 @@ func (s *ServiceScaler) scaleService(scale func(uint64) uint64) error {
return fmt.Errorf("scale can only be used with replicated mode")
}
target := scale(*serviceMode.Replicated.Replicas)
log.Debugf("Scale "+s.ServiceID+" from %d to %d", *serviceMode.Replicated.Replicas, target)
serviceMode.Replicated.Replicas = &target

err = cli.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
Expand All @@ -65,7 +67,7 @@ func (s *ServiceScaler) scaleService(scale func(uint64) uint64) error {
func (s *ServiceScaler) getDocker() (cli *client.Client, err error) {
if s.cli == nil {
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
s.cli, err = client.NewClient(s.engineSocket, "v1.24", nil, defaultHeaders)
s.cli, err = client.NewClient(s.EngineSocket, "v1.24", nil, defaultHeaders)
}
return s.cli, err
}
13 changes: 13 additions & 0 deletions examples/docker-service/helloworld/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
endpoint: "unix:///var/run/docker.sock"
scalers:
helloworld:
type: "docker-service"
service: "helloworld"
rules:
sin-scale:
target: "helloworld"
probe: "mock.default"
up: "> 0.5"
down: "< 0.5"
scaler: helloworld
refresh: 3s
2 changes: 2 additions & 0 deletions examples/docker-service/helloworld/start_service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
docker service create --replicas 1 --name helloworld alpine ping docker.com

0 comments on commit f4f7d3a

Please sign in to comment.