-
Notifications
You must be signed in to change notification settings - Fork 670
Remove stale records for short-lived containers #865
Conversation
running = false | ||
} | ||
} else { | ||
running = container.State.Running |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
I think the abstraction isn't quite right here. There's too much machinery in weavedns and too little in updater. Bear in mind that solving #819 will likely require very similar machinery. |
@rade If with "too much machinery in weavedns" you mean lines 72-86, IMO fourteen lines are not so much machinery, specially when they are pretty specific of WeaveDNS (eg, deleting records in the Zone). Adding this mechanism in the "updater" would not be the right place either, as the "updater" is really a Docker listener, and this does not listen for anything: it just checks whether the container is running. I raised the question in HipChat whether we should have a general "docker client" in So I would wait until #819 before trying to do an early generalization... |
fourteen lines is a lot of machinery when we could have something like Also, there's all the startup machinery in weavedns/main.go |
Yes, it would be nice to have |
weavedns/main.go has no business initialising a docker client. This should be well abstracted away. The new import of "github.com/fsouza/go-dockerclient" in main.go is a dead giveaway. |
Ok, then we will have a |
client *docker.Client | ||
} | ||
|
||
// NewClint creates a new Docker client |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
} | ||
|
||
return running | ||
} |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
The wrapping can be simpler: diff --git a/common/docker/client.go b/common/docker/client.go
index ecd213d..73c5cfd 100644
--- a/common/docker/client.go
+++ b/common/docker/client.go
@@ -11,7 +11,7 @@ type ContainerObserver interface {
}
type Client struct {
- client *docker.Client
+ *docker.Client
}
// NewClient creates a new Docker client
@@ -21,10 +21,7 @@ func NewClient(apiPath string) (*Client, error) {
return nil, err
}
- c := Client{
- client: dc,
- }
- return &c, nil
+ return &Client{dc}, nil
}
// Start starts the client
@@ -47,7 +44,7 @@ func (c *Client) Start(apiPath string) error {
// AddObserver adds an observer for docker events
func (c *Client) AddObserver(ob ContainerObserver) error {
events := make(chan *docker.APIEvents)
- if err := c.client.AddEventListener(events); err != nil {
+ if err := c.AddEventListener(events); err != nil {
Error.Printf("[docker] Unable to add listener to Docker API: %s"
return err
}
@@ -69,7 +66,7 @@ func (c *Client) IsContainerRunning(idStr string) bool {
running := true
// check with Docker whether the container is really running
- if container, err := c.client.InspectContainer(idStr); err != nil {
+ if container, err := c.InspectContainer(idStr); err != nil {
if _, notThere := err.(*docker.NoSuchContainer); notThere {
running = false
} |
IMO that would not be appropriate here. Defining a struct like |
But it does. Namely the methods defined here.
I actually see no problem with that. |
what's happening with this? |
The code I posted is shorter and just as readable, if not more so, than the original. |
I also considered that solution when writing the code, and I discarded it because I think type aliases are not a good idea, even when they lead to shorter code (eg, |
Can it actually be done with a type alias? I tried and failed. |
(facepalm) |
You can do this: type s string
func (something s) IsEmpty() bool { return len(something) == 0 } so should we replace all our strings with |
I know about type aliases, but I cannot get them to work in this situation. Can you? The problem I am encountering is that we get a pointer to the docker client, which I cannot figure out how to turn into the right type, if that is even possible. |
Found the answer; so the wrapper struct actually works better than an alias. |
Changed the client... |
Does this handle the non-container ids case correctly, e.g. when we add records for |
and the linter is unhappy. |
Good point about the And I will make the linter happy again... |
To address the |
Fixed the container id check. |
|
||
// IsContainerId returns True if the string provided is a valid container id | ||
func IsContainerId(idStr string) bool { | ||
matched, err := regexp.MatchString("^[a-f0-9]+$", idStr) |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
also, it looks like this needs rebasing. |
3635671
to
89f0595
Compare
…sing information found in some other objects we are already passing, like the server)
The outdent still needs doing. Rest looks fine. |
dockerCli, err = docker.NewClient(apiPath) | ||
if err != nil { | ||
Error.Fatal("[main] Unable to start docker client", err) | ||
} |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
I'm going to take this over in @inercia's absence. |
This PR removes stale records for short-lived containers by asking Docker about the container that corresponds to the new name:IP that has been introduced.
This can be tested by following the procedure described by @rade in #821: with (and without)
docker kill $CONTAINER_ID
inweave:put_dns_fqdn()
and then running something like:Fixes #821