Skip to content

Commit

Permalink
Merge pull request #1213 from HusterWan/zr/fix-volume-struct
Browse files Browse the repository at this point in the history
bugfix: Volume info of inspect output is incompatible with Moby API
  • Loading branch information
rudyfly authored Apr 25, 2018
2 parents 9e7f4eb + 2e7c894 commit eee66c2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
45 changes: 34 additions & 11 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1600,12 +1600,17 @@ func (mgr *ContainerManager) bindVolume(ctx context.Context, name string, meta *
func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta) error {
logrus.Debugf("bind volumes: %v", meta.HostConfig.Binds)

type emptyStruct struct{}

var err error

if meta.Config.Volumes == nil {
meta.Config.Volumes = make(map[string]interface{})
}

// define a volume map to duplicate removal
volumeSet := map[string]emptyStruct{}

if meta.Mounts == nil {
meta.Mounts = make([]*types.MountPoint, 0)
}
Expand All @@ -1624,8 +1629,13 @@ func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta
return errors.Wrapf(err, "failed to get image: %s", meta.Image)
}
for dest := range image.Config.Volumes {
if _, exist := meta.Config.Volumes[dest]; !exist {
meta.Config.Volumes[dest] = emptyStruct{}
}

// check if volume has been created
name := randomid.Generate()
if _, exist := meta.Config.Volumes[name]; exist {
if _, exist := volumeSet[name]; exist {
continue
}

Expand All @@ -1651,7 +1661,7 @@ func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta
return err
}

meta.Config.Volumes[mp.Name] = mp.Destination
volumeSet[mp.Name] = emptyStruct{}
meta.Mounts = append(meta.Mounts, mp)
}

Expand Down Expand Up @@ -1681,15 +1691,20 @@ func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta
return errors.Errorf("unknown bind: %s", b)
}

if mp.Source == "" {
mp.Source = randomid.Generate()

// Source is empty, anonymouse volume
if _, exist := meta.Config.Volumes[mp.Destination]; !exist {
meta.Config.Volumes[mp.Destination] = emptyStruct{}
}
}

if opts.CheckDuplicateMountPoint(meta.Mounts, mp.Destination) {
logrus.Warnf("duplicate mount point: %s", mp.Destination)
continue
}

if mp.Source == "" {
mp.Source = randomid.Generate()
}

err = opts.ParseBindMode(mp, mode)
if err != nil {
logrus.Errorf("failed to parse bind mode: %s, err: %v", mode, err)
Expand All @@ -1699,14 +1714,15 @@ func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta
if !path.IsAbs(mp.Source) {
// volume bind.
name := mp.Source
if _, exist := meta.Config.Volumes[name]; !exist {
if _, exist := volumeSet[name]; !exist {
mp.Name = name
mp.Source, mp.Driver, err = mgr.bindVolume(ctx, name, meta)
if err != nil {
logrus.Errorf("failed to bind volume: %s, err: %v", name, err)
return errors.Wrap(err, "failed to bind volume")
}
meta.Config.Volumes[mp.Name] = mp.Destination

volumeSet[mp.Name] = emptyStruct{}
}

if mp.Replace != "" {
Expand Down Expand Up @@ -1771,14 +1787,16 @@ func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta
Propagation: oldMountPoint.Propagation,
}

if _, exist := meta.Config.Volumes[oldMountPoint.Name]; len(oldMountPoint.Name) > 0 && !exist {
if _, exist := volumeSet[oldMountPoint.Name]; len(oldMountPoint.Name) > 0 && !exist {
mp.Name = oldMountPoint.Name
mp.Source, mp.Driver, err = mgr.bindVolume(ctx, oldMountPoint.Name, meta)
if err != nil {
logrus.Errorf("failed to bind volume: %s, err: %v", oldMountPoint.Name, err)
return errors.Wrap(err, "failed to bind volume")
}
meta.Config.Volumes[mp.Name] = oldMountPoint.Destination

meta.Config.Volumes[mp.Destination] = emptyStruct{}
volumeSet[mp.Name] = emptyStruct{}
}

err = opts.ParseBindMode(mp, mode)
Expand Down Expand Up @@ -1901,7 +1919,12 @@ func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *Cont
}

func (mgr *ContainerManager) detachVolumes(ctx context.Context, c *ContainerMeta) error {
for name := range c.Config.Volumes {
for _, mount := range c.Mounts {
name := mount.Name
if name == "" {
continue
}

v, err := mgr.VolumeMgr.Get(ctx, name)
if err != nil {
logrus.Errorf("failed to get volume: %s", name)
Expand Down
4 changes: 2 additions & 2 deletions test/cli_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func (suite *PouchRunSuite) TestRunWithVolumesFrom(c *check.C) {

volumeFound := false
for _, line := range strings.Split(out, "\n") {
if strings.Contains(line, "\"volumesfrom-test-volume\": \"/mnt\"") {
if strings.Contains(line, "\"volumesfrom-test-volume\"") {
volumeFound = true
break
}
Expand Down Expand Up @@ -1019,7 +1019,7 @@ func (suite *PouchRunSuite) TestRunWithVolumesFromWithDupclicate(c *check.C) {

volumeFound := false
for _, line := range strings.Split(out, "\n") {
if strings.Contains(line, "\"volumesfromDupclicate-test-volume\": \"/mnt\"") {
if strings.Contains(line, "\"volumesfromDupclicate-test-volume\"") {
volumeFound = true
break
}
Expand Down

0 comments on commit eee66c2

Please sign in to comment.