Bootstrap autodetection and swarm zero conf

Fixed #9

This PR bootstrap the autodetection feature:

1. It works when the configuration file is not setup. Right know I am
not going to manage the merge of double sources (autodetection and
configuration file).

2. At the moment only Docker Swarm will support this feature.
This commit is contained in:
Gianluca Arbezzano 2017-03-14 23:49:53 +00:00
parent 046d999a1b
commit db7f674976
4 changed files with 43 additions and 15 deletions

View File

@ -25,7 +25,7 @@ func (c *DaemonCmd) Run(args []string) int {
var debug bool var debug bool
cmdFlags := flag.NewFlagSet("event", flag.ExitOnError) cmdFlags := flag.NewFlagSet("event", flag.ExitOnError)
cmdFlags.StringVar(&port, "port", ":8000", "port") cmdFlags.StringVar(&port, "port", ":8000", "port")
cmdFlags.StringVar(&configPath, "config", "/etc/deamon.yml", "config") cmdFlags.StringVar(&configPath, "config", "", "config")
cmdFlags.BoolVar(&debug, "debug", false, "debug") cmdFlags.BoolVar(&debug, "debug", false, "debug")
if err := cmdFlags.Parse(args); err != nil { if err := cmdFlags.Parse(args); err != nil {
logrus.WithField("error", err).Warn("Problem to parse arguments.") logrus.WithField("error", err).Warn("Problem to parse arguments.")
@ -35,15 +35,22 @@ func (c *DaemonCmd) Run(args []string) int {
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
logrus.Debug("Daemon started in debug mode") logrus.Debug("Daemon started in debug mode")
} }
config, err := readConfiguration(configPath) var coreEngine core.Core
if err != nil { if configPath != "" {
logrus.WithField("error", err).Warn("Configuration file malformed.") config, err := readConfiguration(configPath)
return 1 if err != nil {
} logrus.WithField("error", err).Warn("Configuration file malformed.")
core, err := core.NewCore(config.AutoscalersConf) return 1
if err != nil { }
logrus.WithField("error", err).Warn(err) logrus.Infof("Starting from configuration file located %s", configPath)
return 1 coreEngine, err = core.NewCoreByConfig(config.AutoscalersConf)
if err != nil {
logrus.WithField("error", err).Warn(err)
return 1
}
} else {
logrus.Info("Starting in auto-detection mode.")
coreEngine, err = core.Autodetect()
} }
go func() { go func() {
sigchan := make(chan os.Signal, 10) sigchan := make(chan os.Signal, 10)
@ -52,7 +59,7 @@ func (c *DaemonCmd) Run(args []string) int {
logrus.Info("Stopping and cleaning. Bye!") logrus.Info("Stopping and cleaning. Bye!")
os.Exit(0) os.Exit(0)
}() }()
router := api.GetRouter(core, 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)
return 0 return 0

21
core/autodetect.go Normal file
View File

@ -0,0 +1,21 @@
package core
import "github.com/gianarb/orbiter/autoscaler"
// This function use diferent strategies to get information from
// the system itself to configure the autoloader.
// They can be environment variables for example or other systems.
func Autodetect() (Core error) {
scalers := autoscaler.Autoscalers{}
var core Core
autoDetectSwarmMode(&scalers)
return core, nil
}
func autoDetectSwarmMode(a *autoscaler.Autoscalers) {
// Create Docker Client by EnvVar and check if it's working.
// Get List of Services
// Check which services has labels and register them to orbiter.
}

View File

@ -11,7 +11,7 @@ type Core struct {
Autoscalers autoscaler.Autoscalers Autoscalers autoscaler.Autoscalers
} }
func NewCore(c map[string]AutoscalerConf) (Core, error) { func NewCoreByConfig(c map[string]AutoscalerConf) (Core, error) {
scalers := autoscaler.Autoscalers{} scalers := autoscaler.Autoscalers{}
var core Core var core Core
for scalerName, scaler := range c { for scalerName, scaler := range c {

View File

@ -29,7 +29,7 @@ func TestNewCore(t *testing.T) {
}, },
}, },
} }
core, err := NewCore(conf) core, err := NewCoreByConfig(conf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -55,7 +55,7 @@ func TestGetSingleAutoscaler(t *testing.T) {
}, },
}, },
} }
core, _ := NewCore(conf) core, _ := NewCoreByConfig(conf)
_, ok := core.Autoscalers["second/micro"] _, ok := core.Autoscalers["second/micro"]
if ok == false { if ok == false {
t.Fatal("micro exist") t.Fatal("micro exist")
@ -89,7 +89,7 @@ func TestNewCoreWithUnsupportedProvider(t *testing.T) {
}, },
}, },
} }
_, err := NewCore(conf) _, err := NewCoreByConfig(conf)
if err.Error() != "lalala not supported." { if err.Error() != "lalala not supported." {
t.Fatal(err) t.Fatal(err)
} }