Skip to content

Commit 0d9b0ed

Browse files
committed
Fix possible overlapping IPs
A node is no longer using its load balancer IP address when it no longer has tasks that use the network that requires that load balancer. When this occurs, the swarmkit manager will free that IP in IPAM, and may reaassign it. When a task shuts down cleanly, it attempts removal of the networks it uses, and if it is the last task using those networks, this removal succeeds, and the load balancer IP is freed. However, this behavior is absent if the container fails. Removal of the networks is never attempted. To address this issue, I amend the executor. Whenever a node load balancer IP is removed or changed, that information is passedd to the executor by way of the Configure method. By keeping track of the set of node NetworkAttachments from the previous call to Configure, we can determine which, if any, have been removed or changed. At first, this seems to create a race, by which a task can be attempting to start and the network is removed right out from under it. However, this is already addressed in the controller. The controller will attempt to recreate missing networks before starting a task. Signed-off-by: Drew Erny <[email protected]>
1 parent 86b4d88 commit 0d9b0ed

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

daemon/cluster/executor/container/executor.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import (
1515
"github.com/docker/docker/daemon/cluster/convert"
1616
executorpkg "github.com/docker/docker/daemon/cluster/executor"
1717
clustertypes "github.com/docker/docker/daemon/cluster/provider"
18+
"github.com/docker/docker/libnetwork"
1819
networktypes "github.com/docker/docker/libnetwork/types"
1920
"github.com/docker/swarmkit/agent"
2021
"github.com/docker/swarmkit/agent/exec"
2122
"github.com/docker/swarmkit/api"
2223
"github.com/docker/swarmkit/api/naming"
24+
"github.com/docker/swarmkit/log"
2325
"github.com/docker/swarmkit/template"
26+
"github.com/pkg/errors"
2427
"github.com/sirupsen/logrus"
2528
)
2629

@@ -32,6 +35,14 @@ type executor struct {
3235
dependencies exec.DependencyManager
3336
mutex sync.Mutex // This mutex protects the following node field
3437
node *api.NodeDescription
38+
39+
// nodeObj holds a copy of the swarmkit Node object from the time of the
40+
// last call to executor.Configure. This allows us to discover which
41+
// network attachments the node previously had, which further allows us to
42+
// determine which, if any, need to be removed. nodeObj is not protected by
43+
// a mutex, because it is only written to in the method (Configure) that it
44+
// is read from. If that changes, it may need to be guarded.
45+
nodeObj *api.Node
3546
}
3647

3748
// NewExecutor returns an executor from the docker client.
@@ -157,6 +168,40 @@ func (e *executor) Configure(ctx context.Context, node *api.Node) error {
157168
attachments[na.Network.ID] = na.Addresses[0]
158169
}
159170

171+
// discover which, if any, attachments have been removed.
172+
//
173+
// we aren't responsible directly for creating these networks. that is
174+
// handled indirectly when a container using that network is created.
175+
// however, when it comes time to remove the network, none of the relevant
176+
// tasks may exist anymore. this means we should go ahead and try to remove
177+
// any network we know to no longer be in use.
178+
179+
// removeAttachments maps the network ID to a boolean. This boolean
180+
// indicates whether the attachment in question is totally removed (true),
181+
// or has just had its IP changed (false)
182+
removeAttachments := make(map[string]bool)
183+
184+
// the first time we Configure, nodeObj wil be nil, because it will not be
185+
// set yet. in that case, skip this check.
186+
if e.nodeObj != nil {
187+
for _, na := range e.nodeObj.Attachments {
188+
// same thing as above, check sanity of the attachments so we don't
189+
// get a panic.
190+
if na == nil || na.Network == nil || len(na.Addresses) == 0 {
191+
logrus.WithField("NetworkAttachment", fmt.Sprintf("%#v", na)).
192+
Warnf("skipping nil or malformed node network attachment entry")
193+
continue
194+
}
195+
196+
// now, check if the attachment exists and shares the same IP address.
197+
if ip, ok := attachments[na.Network.ID]; !ok || na.Addresses[0] != ip {
198+
// if the map entry exists, then the network still exists, and the
199+
// IP must be what has changed
200+
removeAttachments[na.Network.ID] = !ok
201+
}
202+
}
203+
}
204+
160205
if (ingressNA == nil) && (node.Attachment != nil) && (len(node.Attachment.Addresses) > 0) {
161206
ingressNA = node.Attachment
162207
attachments[ingressNA.Network.ID] = ingressNA.Addresses[0]
@@ -197,6 +242,42 @@ func (e *executor) Configure(ctx context.Context, node *api.Node) error {
197242
return err
198243
}
199244

245+
var (
246+
activeEndpointsError *libnetwork.ActiveEndpointsError
247+
errNoSuchNetwork libnetwork.ErrNoSuchNetwork
248+
)
249+
250+
// now, finally, remove any network LB attachments that we no longer have.
251+
for nw, gone := range removeAttachments {
252+
err := e.backend.DeleteManagedNetwork(nw)
253+
switch {
254+
case err == nil:
255+
continue
256+
case errors.As(err, &activeEndpointsError):
257+
// this is the purpose of the boolean in the map. it's literally
258+
// just to log an appropriate, informative error. i'm unsure if
259+
// this can ever actually occur, but we need to know if it does.
260+
if gone {
261+
log.G(ctx).Warnf("network %s should be removed, but still has active attachments", nw)
262+
} else {
263+
log.G(ctx).Warnf(
264+
"network %s should have its node LB IP changed, but cannot be removed because of active attachments",
265+
nw,
266+
)
267+
}
268+
continue
269+
case errors.As(err, &errNoSuchNetwork):
270+
// NoSuchNetworkError indicates the network is already gone.
271+
continue
272+
default:
273+
log.G(ctx).Errorf("network %s remove failed: %v", nw, err)
274+
}
275+
}
276+
277+
// now update our copy of the node object, reset the attachment store, and
278+
// return
279+
e.nodeObj = node
280+
200281
return e.backend.GetAttachmentStore().ResetAttachments(attachments)
201282
}
202283

0 commit comments

Comments
 (0)