forked from AliyunContainerService/pouch
/
volume.go
142 lines (116 loc) · 3.67 KB
/
volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package mgr
import (
"context"
"os"
"path"
"strings"
"github.com/alibaba/pouch/daemon/meta"
"github.com/alibaba/pouch/volume"
"github.com/alibaba/pouch/volume/types"
)
// VolumeMgr defines interface to manage container volume.
type VolumeMgr interface {
// Create is used to create volume.
Create(ctx context.Context, name, driver string, options, labels map[string]string) error
// Remove is used to delete an existing volume.
Remove(ctx context.Context, name string) error
// List returns all volumes on this host.
List(ctx context.Context, labels map[string]string) ([]string, error)
// Get returns the information of volume that specified name/id.
Get(ctx context.Context, name string) (*types.Volume, error)
// Path returns the mount path of volume.
Path(ctx context.Context, name string) (string, error)
// Attach is used to bind a volume to container.
Attach(ctx context.Context, name string, options map[string]string) (*types.Volume, error)
// Detach is used to unbind a volume from container.
Detach(ctx context.Context, name string, options map[string]string) (*types.Volume, error)
}
// VolumeManager is the default implement of interface VolumeMgr.
type VolumeManager struct {
core *volume.Core
store *meta.Store
}
// NewVolumeManager creates a brand new volume manager.
func NewVolumeManager(ms *meta.Store, cfg volume.Config) (*VolumeManager, error) {
// init voluem config
cfg.RemoveVolume = true
cfg.VolumeMetaPath = path.Join(path.Dir(ms.BaseDir), "volume", "volume.db")
cfg.DefaultBackend = "local"
core, err := volume.NewCore(cfg)
if err != nil {
return nil, err
}
return &VolumeManager{
core: core,
store: ms,
}, nil
}
// Create is used to create volume.
func (vm *VolumeManager) Create(ctx context.Context, name, driver string, options, labels map[string]string) error {
id := types.VolumeID{
Name: name,
Driver: driver,
Options: map[string]string{},
Selectors: map[string]string{},
}
if labels != nil {
id.Labels = labels
} else {
id.Labels = map[string]string{}
}
for key, opt := range options {
if strings.HasPrefix(key, "selector.") {
key = strings.TrimPrefix(key, "selector.")
id.Selectors[key] = opt
continue
}
id.Options[key] = opt
}
return vm.core.CreateVolume(id)
}
// Remove is used to delete an existing volume.
func (vm *VolumeManager) Remove(ctx context.Context, name string) error {
id := types.VolumeID{
Name: name,
}
return vm.core.RemoveVolume(id)
}
// List returns all volumes on this host.
func (vm *VolumeManager) List(ctx context.Context, labels map[string]string) ([]string, error) {
if _, ok := labels["hostname"]; !ok {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
labels["hostname"] = hostname
}
return vm.core.ListVolumeName(labels)
}
// Get returns the information of volume that specified name/id.
func (vm *VolumeManager) Get(ctx context.Context, name string) (*types.Volume, error) {
id := types.VolumeID{
Name: name,
}
return vm.core.GetVolume(id)
}
// Path returns the mount path of volume.
func (vm *VolumeManager) Path(ctx context.Context, name string) (string, error) {
id := types.VolumeID{
Name: name,
}
return vm.core.VolumePath(id)
}
// Attach is used to bind a volume to container.
func (vm *VolumeManager) Attach(ctx context.Context, name string, options map[string]string) (*types.Volume, error) {
id := types.VolumeID{
Name: name,
}
return vm.core.AttachVolume(id, options)
}
// Detach is used to unbind a volume from container.
func (vm *VolumeManager) Detach(ctx context.Context, name string, options map[string]string) (*types.Volume, error) {
id := types.VolumeID{
Name: name,
}
return vm.core.DetachVolume(id, options)
}