mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
libnet: add ctx to Sandbox.Destroy()
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
This commit is contained in:
@@ -161,7 +161,7 @@ func (iface *lnInterface) Close() error {
|
||||
<-iface.ready
|
||||
if iface.sbx != nil {
|
||||
go func() {
|
||||
if err := iface.sbx.Delete(); err != nil {
|
||||
if err := iface.sbx.Delete(context.TODO()); err != nil {
|
||||
log.G(context.TODO()).WithError(err).Errorf("failed to delete builder network sandbox")
|
||||
}
|
||||
if err := os.RemoveAll(filepath.Join(iface.provider.Root, iface.sbx.ContainerID())); err != nil {
|
||||
|
||||
@@ -487,7 +487,7 @@ func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, c
|
||||
start := time.Now()
|
||||
|
||||
// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
|
||||
if err := daemon.netController.SandboxDestroy(container.ID); err != nil {
|
||||
if err := daemon.netController.SandboxDestroy(ctx, container.ID); err != nil {
|
||||
log.G(ctx).WithError(err).Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
|
||||
}
|
||||
|
||||
@@ -549,7 +549,7 @@ func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, c
|
||||
setNetworkSandbox(container, sb)
|
||||
defer func() {
|
||||
if retErr != nil {
|
||||
sb.Delete()
|
||||
sb.Delete(context.WithoutCancel(ctx))
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -996,7 +996,9 @@ func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID st
|
||||
return nc, nil
|
||||
}
|
||||
|
||||
func (daemon *Daemon) releaseNetwork(container *container.Container) {
|
||||
func (daemon *Daemon) releaseNetwork(ctx context.Context, container *container.Container) {
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
|
||||
start := time.Now()
|
||||
// If live-restore is enabled, the daemon cleans up dead containers when it starts up. In that case, the
|
||||
// netController hasn't been initialized yet and so we can't proceed.
|
||||
@@ -1033,12 +1035,12 @@ func (daemon *Daemon) releaseNetwork(container *container.Container) {
|
||||
|
||||
sb, err := daemon.netController.SandboxByID(sid)
|
||||
if err != nil {
|
||||
log.G(context.TODO()).Warnf("error locating sandbox id %s: %v", sid, err)
|
||||
log.G(ctx).Warnf("error locating sandbox id %s: %v", sid, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := sb.Delete(); err != nil {
|
||||
log.G(context.TODO()).Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
|
||||
if err := sb.Delete(ctx); err != nil {
|
||||
log.G(ctx).Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
|
||||
}
|
||||
|
||||
for _, nw := range networks {
|
||||
|
||||
@@ -233,7 +233,7 @@ func (daemon *Daemon) Cleanup(ctx context.Context, container *container.Containe
|
||||
}
|
||||
}
|
||||
|
||||
daemon.releaseNetwork(container)
|
||||
daemon.releaseNetwork(ctx, container)
|
||||
|
||||
if err := container.UnmountIpcMount(); err != nil {
|
||||
log.G(ctx).Warnf("%s cleanup: failed to unmount IPC: %s", container.ID, err)
|
||||
|
||||
@@ -1016,7 +1016,7 @@ func (c *Controller) SandboxByID(id string) (*Sandbox, error) {
|
||||
}
|
||||
|
||||
// SandboxDestroy destroys a sandbox given a container ID.
|
||||
func (c *Controller) SandboxDestroy(id string) error {
|
||||
func (c *Controller) SandboxDestroy(ctx context.Context, id string) error {
|
||||
var sb *Sandbox
|
||||
c.mu.Lock()
|
||||
for _, s := range c.sandboxes {
|
||||
@@ -1032,7 +1032,7 @@ func (c *Controller) SandboxDestroy(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
return sb.Delete()
|
||||
return sb.Delete(ctx)
|
||||
}
|
||||
|
||||
func (c *Controller) loadDriver(networkType string) error {
|
||||
|
||||
@@ -61,7 +61,7 @@ fe90::2 somehost.example.com somehost
|
||||
t.Fatalf("expected the hosts file to read:\n%q\nbut instead got the following:\n%q\n", expectedHostsFile, string(data))
|
||||
}
|
||||
|
||||
if err := sbx.Delete(); err != nil {
|
||||
if err := sbx.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
@@ -382,7 +382,7 @@ func TestSRVServiceQuery(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sb.Delete(); err != nil {
|
||||
if err := sb.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -481,7 +481,7 @@ func TestServiceVIPReuse(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sb.Delete(); err != nil {
|
||||
if err := sb.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -129,7 +129,7 @@ func TestNull(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := cnt.Delete(); err != nil {
|
||||
if err := cnt.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -883,7 +883,7 @@ func TestEndpointDeleteWithActiveContainer(t *testing.T) {
|
||||
libnetwork.OptionDomainname("example.com"),
|
||||
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
||||
defer func() {
|
||||
if err := cnt.Delete(); err != nil {
|
||||
if err := cnt.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -946,7 +946,7 @@ func TestEndpointMultipleJoins(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sbx1.Delete(); err != nil {
|
||||
if err := sbx1.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -956,7 +956,7 @@ func TestEndpointMultipleJoins(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sbx2.Delete(); err != nil {
|
||||
if err := sbx2.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -1040,7 +1040,7 @@ func TestLeaveAll(t *testing.T) {
|
||||
t.Fatalf("Failed to join ep2: %v", err)
|
||||
}
|
||||
|
||||
err = cnt.Delete()
|
||||
err = cnt.Delete(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1082,7 +1082,7 @@ func TestContainerInvalidLeave(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := cnt.Delete(); err != nil {
|
||||
if err := cnt.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -1147,7 +1147,7 @@ func TestEndpointUpdateParent(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sbx1.Delete(); err != nil {
|
||||
if err := sbx1.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -1161,7 +1161,7 @@ func TestEndpointUpdateParent(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sbx2.Delete(); err != nil {
|
||||
if err := sbx2.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -1319,7 +1319,7 @@ func TestHost(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sbx1.Delete(); err != nil {
|
||||
if err := sbx1.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -1333,7 +1333,7 @@ func TestHost(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sbx2.Delete(); err != nil {
|
||||
if err := sbx2.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -1383,7 +1383,7 @@ func TestHost(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := cnt3.Delete(); err != nil {
|
||||
if err := cnt3.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -1566,7 +1566,7 @@ func TestEndpointJoin(t *testing.T) {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := sb.Delete(); err != nil {
|
||||
if err := sb.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -1717,7 +1717,7 @@ func externalKeyTest(t *testing.T, reexec bool) {
|
||||
libnetwork.OptionUseExternalKey(),
|
||||
libnetwork.OptionExtraHost("web", "192.168.0.1"))
|
||||
defer func() {
|
||||
if err := cnt.Delete(); err != nil {
|
||||
if err := cnt.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
osl.GC()
|
||||
@@ -1876,7 +1876,7 @@ func TestResolvConf(t *testing.T) {
|
||||
sb, err := c.NewSandbox(context.Background(), containerID, sbOpts...)
|
||||
assert.NilError(t, err)
|
||||
defer func() {
|
||||
err := sb.Delete()
|
||||
err := sb.Delete(context.Background())
|
||||
assert.Check(t, err)
|
||||
}()
|
||||
|
||||
@@ -1954,7 +1954,7 @@ func (pt parallelTester) Do(t *testing.T, thrNumber int) error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := errors.WithStack(sb.Delete()); err != nil {
|
||||
if err := errors.WithStack(sb.Delete(context.Background())); err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.WithStack(ep.Delete(context.Background(), false))
|
||||
@@ -2064,7 +2064,7 @@ func TestBridge(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := sb.Delete(); err != nil {
|
||||
if err := sb.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -2122,7 +2122,7 @@ func (n *Network) createLoadBalancerSandbox() (retErr error) {
|
||||
}
|
||||
defer func() {
|
||||
if retErr != nil {
|
||||
if e := n.ctrlr.SandboxDestroy(sandboxName); e != nil {
|
||||
if e := n.ctrlr.SandboxDestroy(context.WithoutCancel(context.TODO()), sandboxName); e != nil {
|
||||
log.G(context.TODO()).Warnf("could not delete sandbox %s on failure on failure (%v): %v", sandboxName, retErr, e)
|
||||
}
|
||||
}
|
||||
@@ -2182,7 +2182,7 @@ func (n *Network) deleteLoadBalancerSandbox() error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := c.SandboxDestroy(sandboxName); err != nil {
|
||||
if err := c.SandboxDestroy(context.TODO(), sandboxName); err != nil {
|
||||
return fmt.Errorf("Failed to delete %s sandbox: %v", sandboxName, err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -44,7 +44,7 @@ func TestDNSIPQuery(t *testing.T) {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := sb.Delete(); err != nil {
|
||||
if err := sb.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@@ -137,7 +137,7 @@ func TestDNSProxyServFail(t *testing.T) {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := sb.Delete(); err != nil {
|
||||
if err := sb.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -131,11 +131,11 @@ func (sb *Sandbox) Labels() map[string]interface{} {
|
||||
}
|
||||
|
||||
// Delete destroys this container after detaching it from all connected endpoints.
|
||||
func (sb *Sandbox) Delete() error {
|
||||
return sb.delete(false)
|
||||
func (sb *Sandbox) Delete(ctx context.Context) error {
|
||||
return sb.delete(ctx, false)
|
||||
}
|
||||
|
||||
func (sb *Sandbox) delete(force bool) error {
|
||||
func (sb *Sandbox) delete(ctx context.Context, force bool) error {
|
||||
sb.mu.Lock()
|
||||
if sb.inDelete {
|
||||
sb.mu.Unlock()
|
||||
@@ -166,18 +166,18 @@ func (sb *Sandbox) delete(force bool) error {
|
||||
if !c.isSwarmNode() {
|
||||
retain = true
|
||||
}
|
||||
log.G(context.TODO()).Warnf("Failed getting network for ep %s during sandbox %s delete: %v", ep.ID(), sb.ID(), err)
|
||||
log.G(ctx).Warnf("Failed getting network for ep %s during sandbox %s delete: %v", ep.ID(), sb.ID(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
if !force {
|
||||
if err := ep.Leave(context.WithoutCancel(context.TODO()), sb); err != nil {
|
||||
log.G(context.TODO()).Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err)
|
||||
if err := ep.Leave(context.WithoutCancel(ctx), sb); err != nil {
|
||||
log.G(ctx).Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := ep.Delete(context.WithoutCancel(context.TODO()), force); err != nil {
|
||||
log.G(context.TODO()).Warnf("Failed deleting endpoint %s: %v\n", ep.ID(), err)
|
||||
if err := ep.Delete(context.WithoutCancel(ctx), force); err != nil {
|
||||
log.G(ctx).Warnf("Failed deleting endpoint %s: %v\n", ep.ID(), err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,12 +197,12 @@ func (sb *Sandbox) delete(force bool) error {
|
||||
|
||||
if sb.osSbox != nil && !sb.config.useDefaultSandBox {
|
||||
if err := sb.osSbox.Destroy(); err != nil {
|
||||
log.G(context.TODO()).WithError(err).Warn("error destroying network sandbox")
|
||||
log.G(ctx).WithError(err).Warn("error destroying network sandbox")
|
||||
}
|
||||
}
|
||||
|
||||
if err := sb.storeDelete(); err != nil {
|
||||
log.G(context.TODO()).Warnf("Failed to delete sandbox %s from store: %v", sb.ID(), err)
|
||||
log.G(ctx).Warnf("Failed to delete sandbox %s from store: %v", sb.ID(), err)
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
|
||||
@@ -23,7 +23,7 @@ func TestDNSOptions(t *testing.T) {
|
||||
assert.NilError(t, err)
|
||||
|
||||
cleanup := func(s *Sandbox) {
|
||||
if err := s.Delete(); err != nil {
|
||||
if err := s.Delete(context.Background()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,7 +249,7 @@ func (c *Controller) sandboxCleanup(activeSandboxes map[string]interface{}) erro
|
||||
|
||||
if _, ok := activeSandboxes[sb.ID()]; !ok {
|
||||
log.G(context.TODO()).Infof("Removing stale sandbox %s (%s)", sb.id, sb.containerID)
|
||||
if err := sb.delete(true); err != nil {
|
||||
if err := sb.delete(context.WithoutCancel(context.TODO()), true); err != nil {
|
||||
log.G(context.TODO()).Errorf("Failed to delete sandbox %s while trying to cleanup: %v", sb.id, err)
|
||||
}
|
||||
continue
|
||||
|
||||
@@ -84,7 +84,7 @@ func TestControllerGetSandbox(t *testing.T) {
|
||||
assert.Check(t, is.Equal(sb.Key(), expected.Key()))
|
||||
assert.Check(t, is.Equal(sb.ContainerID(), expected.ContainerID()))
|
||||
|
||||
err = sb.Delete()
|
||||
err = sb.Delete(context.Background())
|
||||
assert.Check(t, err)
|
||||
|
||||
sb, err = ctrlr.GetSandbox(cID)
|
||||
@@ -101,7 +101,7 @@ func TestSandboxAddEmpty(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := sbx.Delete(); err != nil {
|
||||
if err := sbx.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ func TestSandboxAddMultiPrio(t *testing.T) {
|
||||
t.Fatal("Expected ep3 to be at the top of the heap after adding ep3 back. But did not find ep3 at the top of the heap")
|
||||
}
|
||||
|
||||
if err := sbx.Delete(); err != nil {
|
||||
if err := sbx.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -278,7 +278,7 @@ func TestSandboxAddSamePrio(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := sbx.Delete(); err != nil {
|
||||
if err := sbx.Delete(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user