From 3f65e55902972e20afec77a2d826f5da5dabfc2f Mon Sep 17 00:00:00 2001 From: Ace-Tang Date: Tue, 12 Jun 2018 11:54:11 +0800 Subject: [PATCH] bugfix: restore config after update fail if pouch update a container get error, restore container config, aviod container record error config. Fixes: #1512 Signed-off-by: Ace-Tang --- daemon/mgr/container.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index f3dc83059..0124e5ed3 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -752,6 +752,18 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty return err } + restore := false + configBack := *c.Config + hostconfigBack := *c.HostConfig + defer func() { + if restore { + c.Lock() + c.Config = &configBack + c.HostConfig = &hostconfigBack + c.Unlock() + } + }() + if c.IsRunning() && config.Resources.KernelMemory != 0 { return fmt.Errorf("failed to update container %s: can not update kernel memory to a running container, please stop it first", c.ID) } @@ -799,6 +811,7 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty // update Resources of a container. if err := mgr.updateContainerResources(c, config.Resources); err != nil { + restore = true return errors.Wrapf(err, "failed to update resource of container %s", c.ID) } @@ -860,17 +873,20 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty // If container is not running, update container metadata struct is enough, // resources will be updated when the container is started again, // If container is running, we need to update configs to the real world. - var updateErr error if c.IsRunning() { - updateErr = mgr.Client.UpdateResources(ctx, c.ID, c.HostConfig.Resources) + if err := mgr.Client.UpdateResources(ctx, c.ID, c.HostConfig.Resources); err != nil { + restore = true + return fmt.Errorf("failed to update resource: %s", err) + } } // store disk. - if updateErr == nil { - updateErr = c.Write(mgr.Store) + err = c.Write(mgr.Store) + if err != nil { + restore = true } - return updateErr + return err } // Remove removes a container, it may be running or stopped and so on.