Open squaremo opened 9 years ago
OK how about this:
We want to add a DNS entry IP->hostname
when a container with hostname
is 1. started with weave as its network, and given an endpoint with IP
; 2. a running container with hostname
is given an endpoint with IP
.
When an endpoint is created we supply its IP. When it is "joined" to a container, we find out the sandbox key, so we now have EndpointID->IP, and SandboxKey->EndpointID. Given SandboxKey->Container, we have (transitively) EndpointID->Container, and we can go and look at the hostname to add it to DNS.
How do we get SandboxKey->Container? We can see containers starting via the event stream, so we could go look at each to see its sandbox key. If the container has a name, and there's an entry for that sandbox key in SandboxKey->EndpointID, we know to register it with DNS.
For 2), if a container is given an endpoint while it's running, we will see the Join operation and already have an entry in SandboxKey->Container, so we'll know to go look for its hostname (in fact we could just store the hostname there).
Recovery: we'll need to get the SandboxKey->Container map on startup, since we won't necessarily see containers that are already running otherwise. If we restart while docker is still running, we may be able to recover endpointIDs from the containers as well.
For a simple version, I am simply watching the docker event stream and inspecting any container that is started. If it has a domain name that is a subdomain of .weave.local
, I register its .Config.NetworkSettings.IPAddress
with DNS, assuming that it has a weave-assigned IP.
(It would be nice to check that the NetworkID
is one we recognise, but the docker client lib does not have up-to-date struct definitions.)
Likewise, when a container disappears, and has a domain name in .weave.local
, I remove it from DNS.
At present the driver is not told either of the containerID or the hostname, so it is more or less impossible in remote driver terms to register containers with weaveDNS. I've filed https://github.com/docker/libnetwork/issues/234 to try and remedy the situation.
Another solution might be to watch the docker event stream for containers starting, and inspect them for weave endpoints. Of course, one problem there is that adding an endpoint to a container after the fact will not get reported in the event stream.
Once we know about a container, though, we can look it up by its SandboxKey, so perhaps that will be helpful; however, a container is not given a sandbox key until it is started.