Merge pull request #179 from sboeuf/fix_rollback

virtcontainers: Fix container creation rollback
This commit is contained in:
Peng Tao 2018-04-04 13:37:46 +08:00 committed by GitHub
commit dda4a4494a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 22 deletions

View File

@ -420,17 +420,15 @@ func newContainer(pod *Pod, contConfig ContainerConfig) (*Container, error) {
// been performed before the container creation failed. // been performed before the container creation failed.
// - Unplug CPU and memory resources from the VM. // - Unplug CPU and memory resources from the VM.
// - Unplug devices from the VM. // - Unplug devices from the VM.
func rollbackFailingContainerCreation(c *Container, err error) { func (c *Container) rollbackFailingContainerCreation() {
if err != nil && c != nil { if err := c.removeResources(); err != nil {
if err2 := c.removeResources(); err2 != nil { c.Logger().WithError(err).Error("rollback failed removeResources()")
c.Logger().WithError(err2).Error("rollback failed removeResources()")
} }
if err2 := c.detachDevices(); err2 != nil { if err := c.detachDevices(); err != nil {
c.Logger().WithError(err2).Error("rollback failed detachDevices()") c.Logger().WithError(err).Error("rollback failed detachDevices()")
}
if err2 := c.removeDrive(); err2 != nil {
c.Logger().WithError(err2).Error("rollback failed removeDrive()")
} }
if err := c.removeDrive(); err != nil {
c.Logger().WithError(err).Error("rollback failed removeDrive()")
} }
} }
@ -452,7 +450,11 @@ func createContainer(pod *Pod, contConfig ContainerConfig) (c *Container, err er
// In case the container creation fails, the following takes care // In case the container creation fails, the following takes care
// of rolling back all the actions previously performed. // of rolling back all the actions previously performed.
defer rollbackFailingContainerCreation(c, err) defer func() {
if err != nil {
c.rollbackFailingContainerCreation()
}
}()
if err = c.hotplugDrive(); err != nil { if err = c.hotplugDrive(); err != nil {
return return

View File

@ -603,8 +603,7 @@ func (k *kataAgent) appendDevices(deviceList []*grpc.Device, devices []Device) [
// been performed before the container creation failed. // been performed before the container creation failed.
// - Unmount container volumes. // - Unmount container volumes.
// - Unmount container rootfs. // - Unmount container rootfs.
func (k *kataAgent) rollbackFailingContainerCreation(c *Container, err error) { func (k *kataAgent) rollbackFailingContainerCreation(c *Container) {
if err != nil {
if c != nil { if c != nil {
if err2 := c.unmountHostMounts(); err2 != nil { if err2 := c.unmountHostMounts(); err2 != nil {
k.Logger().WithError(err2).Error("rollback failed unmountHostMounts()") k.Logger().WithError(err2).Error("rollback failed unmountHostMounts()")
@ -615,7 +614,6 @@ func (k *kataAgent) rollbackFailingContainerCreation(c *Container, err error) {
} }
} }
} }
}
func (k *kataAgent) createContainer(pod *Pod, c *Container) (p *Process, err error) { func (k *kataAgent) createContainer(pod *Pod, c *Container) (p *Process, err error) {
ociSpecJSON, ok := c.config.Annotations[vcAnnotations.ConfigJSONKey] ociSpecJSON, ok := c.config.Annotations[vcAnnotations.ConfigJSONKey]
@ -639,7 +637,11 @@ func (k *kataAgent) createContainer(pod *Pod, c *Container) (p *Process, err err
// In case the container creation fails, the following defer statement // In case the container creation fails, the following defer statement
// takes care of rolling back actions previously performed. // takes care of rolling back actions previously performed.
defer k.rollbackFailingContainerCreation(c, err) defer func() {
if err != nil {
k.rollbackFailingContainerCreation(c)
}
}()
if c.state.Fstype != "" { if c.state.Fstype != "" {
// This is a block based device rootfs. // This is a block based device rootfs.