Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: restore config after update fail #1513

Merged
merged 1 commit into from
Jun 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,18 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
return err
}

restore := false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if we need the new variable restore. Could we just decide whether to rollback via the nullable of err? @Ace-Tang @HusterWan

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)
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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.
Expand Down