Skip to content

Commit d0ba3a3

Browse files
committed
feature: parse volumes from image
parse volumes from image, so pouchd will create a volume that is defined in image automatically. In addition, if volume has no size, the diskquota will set to volume. Signed-off-by: Rudy Zhang <[email protected]>
1 parent 5caea36 commit d0ba3a3

File tree

7 files changed

+90
-13
lines changed

7 files changed

+90
-13
lines changed

ctrd/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ func rootFSToAPIType(rootFs *v1.RootFS) types.ImageInfoRootFS {
105105
// ociImageToPouchImage transfer the image from OCI format to Pouch format.
106106
func ociImageToPouchImage(ociImage v1.Image) (types.ImageInfo, error) {
107107
imageConfig := ociImage.Config
108+
109+
volumes := make(map[string]interface{})
110+
for k, obj := range imageConfig.Volumes {
111+
volumes[k] = obj
112+
}
108113
cfg := &types.ContainerConfig{
109114
// TODO: add more fields
110115
User: imageConfig.User,
@@ -114,6 +119,7 @@ func ociImageToPouchImage(ociImage v1.Image) (types.ImageInfo, error) {
114119
WorkingDir: imageConfig.WorkingDir,
115120
Labels: imageConfig.Labels,
116121
StopSignal: imageConfig.StopSignal,
122+
Volumes: volumes,
117123
}
118124

119125
rootFs := rootFSToAPIType(&ociImage.RootFS)

daemon/mgr/container.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,39 @@ func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta
14521452
}
14531453
}()
14541454

1455+
// parse volumes from image
1456+
image, err := mgr.ImageMgr.GetImage(ctx, meta.Image)
1457+
if err != nil {
1458+
return errors.Wrapf(err, "failed to get image: %s", meta.Image)
1459+
}
1460+
for dest := range image.Config.Volumes {
1461+
name := randomid.Generate()
1462+
if _, exist := meta.Config.Volumes[name]; exist {
1463+
continue
1464+
}
1465+
1466+
mp := new(types.MountPoint)
1467+
mp.Name = name
1468+
mp.Named = true
1469+
mp.Destination = dest
1470+
1471+
mp.Source, mp.Driver, err = mgr.bindVolume(ctx, mp.Name, meta)
1472+
if err != nil {
1473+
logrus.Errorf("failed to bind volume: %s, err: %v", mp.Name, err)
1474+
return errors.Wrap(err, "failed to bind volume")
1475+
}
1476+
1477+
err = opts.ParseBindMode(mp, "")
1478+
if err != nil {
1479+
logrus.Errorf("failed to parse mode, err: %v", err)
1480+
return err
1481+
}
1482+
1483+
meta.Config.Volumes[mp.Name] = mp.Destination
1484+
meta.Mounts = append(meta.Mounts, mp)
1485+
}
1486+
1487+
// parse volumes from other containers
14551488
for _, v := range meta.HostConfig.VolumesFrom {
14561489
var containerID, mode string
14571490
containerID, mode, err = opts.ParseVolumesFrom(v)
@@ -1631,12 +1664,27 @@ func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *Cont
16311664

16321665
for _, mp := range c.Mounts {
16331666
// skip volume mount or replace mode mount
1634-
if mp.Name != "" || mp.Replace != "" || mp.Source == "" || mp.Destination == "" {
1667+
if mp.Replace != "" || mp.Source == "" || mp.Destination == "" {
1668+
logrus.Debugf("skip volume mount or replace mode mount")
16351669
continue
16361670
}
16371671

1672+
if mp.Name != "" {
1673+
v, err := mgr.VolumeMgr.Get(ctx, mp.Name)
1674+
if err != nil {
1675+
logrus.Warnf("failed to get volume: %s", mp.Name)
1676+
continue
1677+
}
1678+
1679+
if v.Size() != "" {
1680+
logrus.Debugf("skip volume: %s with size", mp.Name)
1681+
continue
1682+
}
1683+
}
1684+
16381685
// skip non-directory path.
16391686
if fd, err := os.Stat(mp.Source); err != nil || !fd.IsDir() {
1687+
logrus.Debugf("skip non-directory path: %s", mp.Source)
16401688
continue
16411689
}
16421690

daemon/mgr/volume.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mgr
33
import (
44
"context"
55
"os"
6+
"path"
67
"strings"
78

89
"github.com/alibaba/pouch/pkg/errtypes"
@@ -82,6 +83,11 @@ func (vm *VolumeManager) Create(ctx context.Context, name, driver string, option
8283
id.Options[key] = opt
8384
}
8485

86+
// set default volume mount path
87+
if mount, ok := id.Options["mount"]; !ok || mount == "" {
88+
id.Options["mount"] = path.Dir(vm.core.VolumeMetaPath)
89+
}
90+
8591
return vm.core.CreateVolume(id)
8692
}
8793

storage/volume/core_util.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func checkVolume(v *types.Volume) error {
195195
}
196196

197197
func buildVolumeConfig(options map[string]string) (*types.VolumeConfig, error) {
198-
size := defaultSize
198+
size := ""
199199
config := &types.VolumeConfig{
200200
FileSystem: defaultFileSystem,
201201
MountOpt: defaultFileSystem,
@@ -206,11 +206,13 @@ func buildVolumeConfig(options map[string]string) (*types.VolumeConfig, error) {
206206
size = s
207207
}
208208

209-
sizeInt, err := bytefmt.ToMegabytes(size)
210-
if err != nil {
211-
return nil, err
209+
if size != "" {
210+
sizeInt, err := bytefmt.ToMegabytes(size)
211+
if err != nil {
212+
return nil, err
213+
}
214+
config.Size = strconv.Itoa(int(sizeInt)) + "M"
212215
}
213-
config.Size = strconv.Itoa(int(sizeInt)) + "M"
214216

215217
// Parse filesystem
216218
if fs, ok := options[optionFS]; ok {

test/cli_run_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,18 @@ func (suite *PouchRunSuite) TestRunWithLocalVolume(c *check.C) {
378378
name := funcname
379379

380380
command.PouchRun("volume", "create", "--name", funcname).Assert(c, icmd.Success)
381+
defer func() {
382+
command.PouchRun("volume", "remove", funcname).Assert(c, icmd.Success)
383+
}()
384+
381385
command.PouchRun("run", "--name", name, "-v", funcname+":/tmp", busyboxImage, "touch", "/tmp/test").Assert(c, icmd.Success)
386+
defer func() {
387+
DelContainerForceMultyTime(c, name)
388+
}()
382389

383-
// check the existence of /mnt/local/function/test
384-
icmd.RunCommand("stat", "/mnt/local/"+funcname+"/test").Assert(c, icmd.Success)
390+
// check the existence of /var/lib/pouch/volume/function/test
391+
icmd.RunCommand("stat", DefaultVolumeMountPath+"/"+funcname+"/test").Assert(c, icmd.Success)
385392

386-
DelContainerForceMultyTime(c, name)
387-
command.PouchRun("volume", "remove", funcname).Assert(c, icmd.Success)
388393
}
389394

390395
// checkFileContains checks the content of fname contains expt

test/cli_volume_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515
"github.com/gotestyourself/gotestyourself/icmd"
1616
)
1717

18+
var (
19+
// DefaultVolumeMountPath defines the default volume mount path.
20+
DefaultVolumeMountPath = DefaultRootDir + "/volume"
21+
)
22+
1823
// PouchVolumeSuite is the test suite for volume CLI.
1924
type PouchVolumeSuite struct{}
2025

@@ -46,7 +51,7 @@ func (suite *PouchVolumeSuite) TestVolumeWorks(c *check.C) {
4651
ExitCode: 1,
4752
Err: "No such file or directory",
4853
}
49-
err := icmd.RunCommand("stat", "/mnt/local/"+funcname).Compare(expct)
54+
err := icmd.RunCommand("stat", DefaultVolumeMountPath+"/"+funcname).Compare(expct)
5055
c.Assert(err, check.IsNil)
5156

5257
}
@@ -232,7 +237,7 @@ func (suite *PouchVolumeSuite) TestVolumeBindReplaceMode(c *check.C) {
232237

233238
found := false
234239
for _, m := range got.Mounts {
235-
if m.Replace == "dr" && m.Mode == "dr" && m.Source == "/mnt/local/volume_TestVolumeBindReplaceMode/home" {
240+
if m.Replace == "dr" && m.Mode == "dr" && m.Source == DefaultVolumeMountPath+"/volume_TestVolumeBindReplaceMode/home" {
236241
found = true
237242
}
238243
}
@@ -303,7 +308,7 @@ func (suite *PouchVolumeSuite) TestVolumeListOptions(c *check.C) {
303308
if strings.Contains(line, volumeName) {
304309
if !strings.Contains(line, "local") ||
305310
!strings.Contains(line, "g") ||
306-
!strings.Contains(line, "/mnt/local") {
311+
!strings.Contains(line, DefaultVolumeMountPath) {
307312
c.Errorf("list result have no driver or name or size or mountpoint, line: %s", line)
308313
break
309314
}

test/util_daemon.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212
"github.com/gotestyourself/gotestyourself/icmd"
1313
)
1414

15+
var (
16+
// DefaultRootDir defines the default root dir for pouchd.
17+
DefaultRootDir = "/var/lib/pouch"
18+
)
19+
1520
// StartDefaultDaemonDebug starts a deamon with default configuration and debug on.
1621
func StartDefaultDaemonDebug(args ...string) (*daemon.Config, error) {
1722
cfg := daemon.NewConfig()

0 commit comments

Comments
 (0)