Generalize updater.go

This commit is contained in:
Bryan Boreham committed 2015-04-01 16:48:11 +01:00
1 parent 3dad9fb593
commit 68096b0437
3 files changed
+16 -7

No files matched your search

@@ -1,10 +1,14 @@
package nameserver
package updater
import (
"github.com/fsouza/go-dockerclient"
. "github.com/zettio/weave/common"
)
type ContainerObserver interface {
ContainerDied(ident string) error
}
func checkError(err error, apiPath string) {
if err != nil {
Error.Fatalf("[updater] Unable to connect to Docker API on %s: %s",
@@ -12,7 +16,7 @@ func checkError(err error, apiPath string) {
}
}
func StartUpdater(apiPath string, zone Zone) error {
func Start(apiPath string, ob ContainerObserver) error {
client, err := docker.NewClient(apiPath)
checkError(err, apiPath)
@@ -27,18 +31,17 @@ func StartUpdater(apiPath string, zone Zone) error {
go func() {
for event := range events {
handleEvent(zone, event, client)
handleEvent(ob, event, client)
}
}()
return nil
}
func handleEvent(zone Zone, event *docker.APIEvents, client *docker.Client) error {
func handleEvent(ob ContainerObserver, event *docker.APIEvents, client *docker.Client) error {
switch event.Status {
case "die":
id := event.ID
Info.Printf("[updater] Container %s down. Removing records", id)
zone.DeleteRecordsFor(id)
ob.ContainerDied(id)
}
return nil
}
+5
View File
@@ -139,3 +139,8 @@ func (zone *ZoneDb) DeleteRecordsFor(ident string) error {
zone.recs = zone.recs[:w]
return nil
}
func (zone *ZoneDb) ContainerDied(ident string) error {
Info.Printf("[zonedb] Container %s down. Removing records", ident)
return zone.DeleteRecordsFor(ident)
}
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"flag"
"fmt"
. "github.com/zettio/weave/common"
"github.com/zettio/weave/common/updater"
weavedns "github.com/zettio/weave/nameserver"
weavenet "github.com/zettio/weave/net"
"io"
@@ -57,7 +58,7 @@ func main() {
var zone = new(weavedns.ZoneDb)
if watch {
err := weavedns.StartUpdater(apiPath, zone)
err := updater.Start(apiPath, zone)
if err != nil {
Error.Fatal("Unable to start watcher", err)
}