Skip to content

Commit

Permalink
bugfix: json: cannot unmarshal string into Go struct field Config.Vol…
Browse files Browse the repository at this point in the history
…umes of type struct {}

Signed-off-by: Michael Wan <[email protected]>
  • Loading branch information
HusterWan committed Apr 25, 2018
1 parent a8c1729 commit d193b49
Showing 1 changed file with 34 additions and 11 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
volumeSlice := 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 := volumeSlice[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
volumeSlice[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 := volumeSlice[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

volumeSlice[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 := volumeSlice[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.Name] = emptyStruct{}
volumeSlice[mp.Destination] = 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

0 comments on commit d193b49

Please sign in to comment.