ADd consul support , multiple monitors, config file
This commit is contained in:
@@ -10,14 +10,15 @@ import (
|
||||
type MonitorType int
|
||||
|
||||
const (
|
||||
Monitor_PORT MonitorType = 1
|
||||
Monitor_EXEC MonitorType = 2
|
||||
Monitor_PORT MonitorType = 1
|
||||
Monitor_EXEC MonitorType = 2
|
||||
Monitor_CONSUL MonitorType = 3
|
||||
)
|
||||
|
||||
var Monitors = map[string]MonitorType{"port": Monitor_PORT, "exec": Monitor_EXEC}
|
||||
var MonitorMap = map[string]MonitorType{"port": Monitor_PORT, "exec": Monitor_EXEC, "consul": Monitor_CONSUL}
|
||||
|
||||
func (m MonitorType) String() string {
|
||||
for str, mtr := range Monitors {
|
||||
for str, mtr := range MonitorMap {
|
||||
if m == mtr {
|
||||
return str
|
||||
}
|
||||
@@ -32,13 +33,49 @@ type Monitor struct {
|
||||
Cmd string
|
||||
}
|
||||
|
||||
type App struct {
|
||||
Name string
|
||||
Vip *net.IPNet
|
||||
Monitor Monitor
|
||||
func (m *Monitor) Equal(other *Monitor) bool {
|
||||
return m.Type == other.Type && m.Port == other.Port && m.Protocol == other.Protocol && m.Cmd == other.Cmd
|
||||
}
|
||||
|
||||
func NewApp(appName, vip, monitor, monitorType string) (*App, error) {
|
||||
type Monitors []*Monitor
|
||||
|
||||
func (m Monitors) Contains(elem *Monitor) bool {
|
||||
for _, mon := range m {
|
||||
if mon.Equal(elem) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type App struct {
|
||||
Name string
|
||||
Vip *net.IPNet
|
||||
Monitors Monitors
|
||||
}
|
||||
|
||||
func (a *App) Equal(other *App) bool {
|
||||
if len(a.Monitors) != len(other.Monitors) {
|
||||
return false
|
||||
}
|
||||
for _, m := range other.Monitors {
|
||||
if !a.Monitors.Contains(m) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return a.Name == other.Name && a.Vip.String() == other.Vip.String()
|
||||
}
|
||||
|
||||
func (a *App) needsNatRule() (bool, *Monitor) {
|
||||
for _, m := range a.Monitors {
|
||||
if m.Type == Monitor_CONSUL && m.Port != "" {
|
||||
return true, m
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func NewApp(appName, vip string, monitors []string) (*App, error) {
|
||||
if appName == "" {
|
||||
return nil, fmt.Errorf("Invalid app name")
|
||||
}
|
||||
@@ -48,17 +85,25 @@ func NewApp(appName, vip, monitor, monitorType string) (*App, error) {
|
||||
return nil, fmt.Errorf("Invalid VIP specified, need ip/mask")
|
||||
}
|
||||
app.Vip = ipnet
|
||||
m := Monitor{Type: Monitors[monitorType]}
|
||||
switch monitorType {
|
||||
case "port":
|
||||
parts := strings.Split(monitor, ":")
|
||||
m.Protocol = parts[0]
|
||||
m.Port = parts[1]
|
||||
case "exec":
|
||||
m.Cmd = monitor
|
||||
default:
|
||||
glog.V(2).Infof("No monitor specified")
|
||||
for _, m := range monitors {
|
||||
parts := strings.Split(m, ":")
|
||||
if len(parts) != 2 && len(parts) != 3 {
|
||||
glog.Errorf("Invalid monitor specified, ignoring")
|
||||
continue
|
||||
}
|
||||
mon := &Monitor{Type: MonitorMap[parts[0]]}
|
||||
switch mon.Type.String() {
|
||||
case "port":
|
||||
mon.Protocol = parts[1]
|
||||
mon.Port = parts[2]
|
||||
case "exec":
|
||||
mon.Cmd = parts[1]
|
||||
case "consul":
|
||||
glog.V(2).Infof("Using consul health monitor")
|
||||
default:
|
||||
glog.V(2).Infof("No monitor specified")
|
||||
}
|
||||
app.Monitors = append(app.Monitors, mon)
|
||||
}
|
||||
app.Monitor = m
|
||||
return app, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user