Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Check with Docker whether the container is really running
Browse files Browse the repository at this point in the history
  • Loading branch information
inercia committed Jun 8, 2015
1 parent 0ff4fc3 commit 10b3481
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
15 changes: 13 additions & 2 deletions common/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ func (c *Client) AddObserver(ob ContainerObserver) error {
}

// IsContainerRunning returns true if the container is up and running
func (c *Client) IsContainerRunning(id string) bool {
return false
func (c *Client) IsContainerRunning(idStr string) bool {
running := true

// check with Docker whether the container is really running, removing otherwise...
if container, err := c.client.InspectContainer(idStr); err != nil {
if _, notThere := err.(*docker.NoSuchContainer); notThere {
running = false
}
} else {
running = container.State.Running
}

return running
}
8 changes: 7 additions & 1 deletion nameserver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gorilla/mux"
"github.com/miekg/dns"
. "github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/common/docker"
)

func httpErrorAndLog(level *log.Logger, w http.ResponseWriter, msg string,
Expand All @@ -24,7 +25,7 @@ func extractFQDN(r *http.Request) (string, string) {
return "*", ""
}

func ServeHTTP(listener net.Listener, version string, server *DNSServer) {
func ServeHTTP(listener net.Listener, version string, server *DNSServer, dockerCli *docker.Client) {

muxRouter := mux.NewRouter()

Expand Down Expand Up @@ -66,6 +67,11 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer) {
return
} // oh, I already know this. whatever.
}

if dockerCli != nil && !dockerCli.IsContainerRunning(idStr) {
Info.Printf("[http] '%s' is not running: removing", idStr)
server.Zone.DeleteRecords(idStr, name, ip)
}
} else {
Info.Printf("[http] Ignoring name %s, not in %s", name, domain)
}
Expand Down
2 changes: 1 addition & 1 deletion nameserver/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestHttp(t *testing.T) {
srv, _ := NewDNSServer(DNSServerConfig{Zone: zone})

port := httpListener.Addr().(*net.TCPAddr).Port
go ServeHTTP(httpListener, "", srv)
go ServeHTTP(httpListener, "", srv, nil)

// Ask the http server to add our test address into the database
addrParts := strings.Split(testAddr1, "/")
Expand Down
2 changes: 1 addition & 1 deletion prog/weavedns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func main() {
}

go SignalHandlerLoop(srv)
go weavedns.ServeHTTP(httpListener, version, srv)
go weavedns.ServeHTTP(httpListener, version, srv, dockerCli)

err = srv.Start()
if err != nil {
Expand Down

0 comments on commit 10b3481

Please sign in to comment.