polling docker events, update entrypoint only on change

This commit is contained in:
Manuel Bovo 2017-07-17 17:07:20 +02:00 committed by Gianluca Arbezzano
parent 29cfd3d109
commit 83ccbff108
No known key found for this signature in database
GPG Key ID: 9505D2809E1D6180
2 changed files with 36 additions and 4 deletions

View File

@ -9,10 +9,14 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"context"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/gianarb/orbiter/api" "github.com/gianarb/orbiter/api"
"github.com/gianarb/orbiter/autoscaler" "github.com/gianarb/orbiter/autoscaler"
"github.com/gianarb/orbiter/core" "github.com/gianarb/orbiter/core"
"time"
) )
type DaemonCmd struct { type DaemonCmd struct {
@ -54,19 +58,47 @@ func (c *DaemonCmd) Run(args []string) int {
} }
} else { } else {
logrus.Info("Starting in auto-detection mode.") logrus.Info("Starting in auto-detection mode.")
err = core.Autodetect(&coreEngine) /* err = core.Autodetect(&coreEngine)
if err != nil { if err != nil {
logrus.WithField("error", err).Info(err) logrus.WithField("error", err).Info(err)
os.Exit(0) os.Exit(0)
} }*/
} }
// Timer ticker
timer1 := time.NewTicker(1000 * time.Millisecond)
// Watchdog
go func() { go func() {
sigchan := make(chan os.Signal, 10) sigchan := make(chan os.Signal, 10)
signal.Notify(sigchan, os.Interrupt) signal.Notify(sigchan, os.Interrupt)
<-sigchan <-sigchan
timer1.Stop()
logrus.Info("Stopping and cleaning. Bye!") logrus.Info("Stopping and cleaning. Bye!")
os.Exit(0) os.Exit(0)
}() }()
// Background service check
go func() {
counter := 0
ctx := context.Background()
dockerClient, _ := client.NewEnvClient()
for {
<-timer1.C
services, _ := dockerClient.ServiceList(ctx, types.ServiceListOptions{})
if len(services) != counter {
logrus.Debugf("Service list changed %d -> %d", counter, len(services))
err = core.Autodetect(&coreEngine)
if err != nil {
logrus.WithField("error", err).Info(err)
}
counter = len(services)
}
}
}()
// Add routing
router := api.GetRouter(coreEngine, c.EventChannel) router := api.GetRouter(coreEngine, c.EventChannel)
logrus.Infof("API Server run on port %s", port) logrus.Infof("API Server run on port %s", port)
http.ListenAndServe(port, router) http.ListenAndServe(port, router)

View File

@ -52,7 +52,7 @@ func autoDetectSwarmMode(c *Core) {
if err != nil { if err != nil {
continue continue
} }
c.Autoscalers[fmt.Sprintf("autodetect_swarm/%s", service.Spec.Annotations.Name)] = s c.Autoscalers[fmt.Sprintf("autoswarm/%s", service.Spec.Annotations.Name)] = s
} }
} }
@ -64,7 +64,7 @@ func getAutoscalerByService(p autoscaler.Provider, an swarm.Annotations) (autosc
up := convertStringLabelToInt("orbiter.up", an.Labels) up := convertStringLabelToInt("orbiter.up", an.Labels)
down := convertStringLabelToInt("orbiter.down", an.Labels) down := convertStringLabelToInt("orbiter.down", an.Labels)
as := autoscaler.NewAutoscaler(p, an.Name, up, down) as := autoscaler.NewAutoscaler(p, an.Name, up, down)
logrus.Debugf("autodetect_swarm/%s added to orbiter. UP %d, DOWN %d", an.Name, up, down) logrus.Debugf("Registering /handle/autoswarm/%s to orbiter. (UP %d, DOWN %d)", an.Name, up, down)
return as, nil return as, nil
} }