Skip to content

Commit 50fca21

Browse files
committed
Merge branch 'refactor/volume' into 'master'
Refactor/volume See merge request !76
2 parents 13b1896 + 5e9c8eb commit 50fca21

File tree

7 files changed

+33
-60
lines changed

7 files changed

+33
-60
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ build:
3939

4040
test:
4141
go tool vet .
42-
go test ./...
42+
go test -v ./...

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.27a
1+
0.7.27b

cluster/calcium/create_container.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -555,19 +555,12 @@ func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs,
555555
env = append(env, fmt.Sprintf("ERU_NODE_IP=%s", nodeIP))
556556
env = append(env, fmt.Sprintf("ERU_NODE_NAME=%s", node.Name))
557557
env = append(env, fmt.Sprintf("ERU_ZONE=%s", c.config.Zone))
558+
env = append(env, fmt.Sprintf("PERMDIR=%s", filepath.Join(c.config.PermDir, specs.Appname)))
559+
env = append(env, fmt.Sprintf("APPDIR=%s", filepath.Join(c.config.AppDir, specs.Appname)))
558560

559561
// mount paths
560562
binds, volumes := makeMountPaths(specs, c.config)
561-
562-
// add permdir to container
563-
if entry.PermDir {
564-
permDir := filepath.Join("/", specs.Appname, "permdir")
565-
volumes[permDir] = struct{}{}
566-
567-
permDirHost := filepath.Join(c.config.PermDir, specs.Appname)
568-
binds = append(binds, strings.Join([]string{permDirHost, permDir, "rw"}, ":"))
569-
env = append(env, fmt.Sprintf("ERU_PERMDIR=%s", permDir))
570-
}
563+
log.Debugf("App %s will bind %v", specs.Appname, binds)
571564

572565
// log config
573566
// 默认是配置里的driver, 如果entrypoint有指定json-file就用json-file.
@@ -923,7 +916,7 @@ func (c *calcium) doUpgradeContainer(containers []*types.Container, image string
923916
Force: false,
924917
PruneChildren: true,
925918
}
926-
for image, _ := range imagesToDelete {
919+
for image := range imagesToDelete {
927920
log.Debugf("Try to remove image %q while upgrade container", image)
928921
engine.ImageRemove(context.Background(), image, rmiOpts)
929922
}

cluster/calcium/helper.go

+11-41
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"io/ioutil"
7+
"os"
78
"path/filepath"
89
"strings"
910

@@ -69,44 +70,24 @@ func trimLeftSlash(name string) string {
6970
}
7071

7172
// make mount paths
72-
// app.yaml支持三种写法.
73-
// app.yaml里可以支持mount_paths的写法, 例如
74-
// mount_paths:
75-
// - "/var/www/html"
76-
// - "/data/eggsy"
77-
// 这样的路径会被直接挂载到permdir下面去, 例如上面的路径就是
78-
// /mnt/mfs/permdirs/eggsy/data/eggsy
79-
// /mnt/mfs/permdirs/eggsy/var/www/html
80-
// 而且这些路径是可以读写的.
81-
//
82-
// 或者使用volumes, 参数格式跟docker一样, 例如
73+
// 使用volumes, 参数格式跟docker一样, 支持 $PERMDIR $APPDIR 的展开
8374
// volumes:
84-
// - "/data/test:/test:ro"
85-
// - "/data/testx:/testx"
86-
// 说明把宿主机的/data/test映射到容器里的/test, 只读, 同时
87-
// 把宿主机的/data/tests映射到容器里的/testx, 读写.
88-
//
89-
// 或者使用binds, 例如
90-
// binds:
91-
// "/host/path":
92-
// bind: "/container/path"
93-
// ro: true
94-
// 说明把宿主机的/host/path映射到容器里的/container/path, 并且只读
75+
// - "$PERMDIR/foo-data:$APPDIR/foodata:rw"
9576
func makeMountPaths(specs types.Specs, config types.Config) ([]string, map[string]struct{}) {
9677
binds := []string{}
9778
volumes := make(map[string]struct{})
98-
permDirHost := filepath.Join(config.PermDir, specs.Appname)
9979

100-
// mount_paths
101-
for _, path := range specs.MountPaths {
102-
hostPath := filepath.Join(permDirHost, path)
103-
binds = append(binds, fmt.Sprintf("%s:%s:rw", hostPath, path))
104-
volumes[path] = struct{}{}
80+
var expandENV = func(env string) string {
81+
envMap := make(map[string]string)
82+
envMap["PERMDIR"] = filepath.Join(config.PermDir, specs.Appname)
83+
envMap["APPDIR"] = filepath.Join(config.AppDir, specs.Appname)
84+
return envMap[env]
10585
}
10686

10787
// volumes
10888
for _, path := range specs.Volumes {
109-
parts := strings.Split(path, ":")
89+
expanded := os.Expand(path, expandENV)
90+
parts := strings.Split(expanded, ":")
11091
if len(parts) == 2 {
11192
binds = append(binds, fmt.Sprintf("%s:%s:ro", parts[0], parts[1]))
11293
volumes[parts[1]] = struct{}{}
@@ -116,21 +97,10 @@ func makeMountPaths(specs types.Specs, config types.Config) ([]string, map[strin
11697
}
11798
}
11899

119-
// binds
120-
var mode string
121-
for hostPath, bind := range specs.Binds {
122-
if bind.ReadOnly {
123-
mode = "ro"
124-
} else {
125-
mode = "rw"
126-
}
127-
binds = append(binds, fmt.Sprintf("%s:%s:%s", hostPath, bind.InContainerPath, mode))
128-
volumes[bind.InContainerPath] = struct{}{}
129-
}
130-
131100
// /proc/sys
132101
volumes["/writable-proc/sys"] = struct{}{}
133102
binds = append(binds, "/proc/sys:/writable-proc/sys:rw")
103+
volumes["/writable-sys/kernel/mm/transparent_hugepage"] = struct{}{}
134104
binds = append(binds, "/sys/kernel/mm/transparent_hugepage:/writable-sys/kernel/mm/transparent_hugepage:rw")
135105
return binds, volumes
136106
}

cluster/calcium/helper_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package calcium
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"gitlab.ricebook.net/platform/core/types"
8+
)
9+
10+
func TestMakeMountPaths(t *testing.T) {
11+
config := types.Config{PermDir: "/mnt/mfs/permdirs", AppDir: "/home"}
12+
specs := types.Specs{Appname: "foo", Volumes: []string{"$PERMDIR/foo-data:$APPDIR/foo-data"}}
13+
binds, volumes := makeMountPaths(specs, config)
14+
assert.Equal(t, binds, []string{"/mnt/mfs/permdirs/foo/foo-data:/home/foo/foo-data:ro", "/proc/sys:/writable-proc/sys:rw", "/sys/kernel/mm/transparent_hugepage:/writable-sys/kernel/mm/transparent_hugepage:rw"}, "binds should be the same")
15+
assert.Equal(t, volumes, map[string]struct{}{"/home/foo/foo-data": struct{}{}, "/writable-proc/sys": struct{}{}, "/writable-sys/kernel/mm/transparent_hugepage": struct{}{}})
16+
}

devtools/test.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ build:
5252
- "curl www.baidu.com"
5353
- "pip install -r requirements.txt"
5454
base: "hub.ricebook.net/base/centos:python-latest"
55-
mount_paths:
56-
- "/var/www/html"
57-
- "/data/test-ci"
5855
permitted_users:
5956
- "cmgs"
6057
combos:

types/specs.go

-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ type Specs struct {
1313
Entrypoints map[string]Entrypoint `yaml:"entrypoints,omitempty,flow"`
1414
Build []string `yaml:"build,omitempty,flow"`
1515
Volumes []string `yaml:"volumes,omitempty,flow"`
16-
Binds map[string]Bind `yaml:"binds,omitempty,flow"`
1716
Meta map[string]string `yaml:"meta,omitempty,flow"`
1817
Base string `yaml:"base"`
19-
MountPaths []string `yaml:"mount_paths,omitempty,flow"`
2018
DNS []string `yaml:"dns,omitempty,flow"`
2119
}
2220

@@ -32,7 +30,6 @@ type Entrypoint struct {
3230
HealthCheckUrl string `yaml:"healthcheck_url,omitempty"`
3331
HealthCheckExpectedCode int `yaml:"healthcheck_expected_code,omitempty"`
3432
ExtraHosts []string `yaml:"hosts,omitempty,flow"`
35-
PermDir bool `yaml:"permdir,omitempty"`
3633
Privileged string `yaml:"privileged,omitempty"`
3734
LogConfig string `yaml:"log_config,omitempty"`
3835
WorkingDir string `yaml:"working_dir,omitempty"`

0 commit comments

Comments
 (0)