forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume.go
More file actions
214 lines (174 loc) · 5.4 KB
/
Copy pathvolume.go
File metadata and controls
214 lines (174 loc) · 5.4 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package mgr
import (
"context"
"strings"
"github.com/alibaba/pouch/daemon/events"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/utils"
"github.com/alibaba/pouch/storage/volume"
"github.com/alibaba/pouch/storage/volume/types"
"github.com/pkg/errors"
)
// 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) (*types.Volume, error)
// Get returns the information of volume that specified name/id.
Get(ctx context.Context, name string) (*types.Volume, error)
// List returns all volumes on this host.
List(ctx context.Context, labels map[string]string) ([]*types.Volume, error)
// Remove is used to delete an existing volume.
Remove(ctx context.Context, name string) 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
eventsService *events.Events
}
// NewVolumeManager creates a brand new volume manager.
func NewVolumeManager(cfg volume.Config, eventsService *events.Events) (*VolumeManager, error) {
// init volume config
cfg.RemoveVolume = true
cfg.DefaultBackend = types.DefaultBackend
core, err := volume.NewCore(cfg)
if err != nil {
return nil, err
}
return &VolumeManager{
core: core,
eventsService: eventsService,
}, nil
}
// Create is used to create volume.
func (vm *VolumeManager) Create(ctx context.Context, name, driver string, options, labels map[string]string) (*types.Volume, error) {
if driver == "" {
driver = types.DefaultBackend
}
id := types.VolumeContext{
Name: name,
Driver: driver,
Options: map[string]string{},
Labels: map[string]string{},
}
if labels != nil {
id.Labels = labels
}
if options != nil {
id.Options = options
}
v, err := vm.core.CreateVolume(id)
if err != nil {
if errtypes.IsVolumeExisted(err) {
return v, nil
}
return nil, err
}
vm.LogVolumeEvent(ctx, name, "create", map[string]string{"driver": driver})
return v, nil
}
// Get returns the information of volume that specified name/id.
func (vm *VolumeManager) Get(ctx context.Context, name string) (*types.Volume, error) {
id := types.VolumeContext{
Name: name,
}
vol, err := vm.core.GetVolume(id)
if err != nil {
if strings.Contains(err.Error(), "not found") {
return nil, errors.Wrap(errtypes.ErrVolumeNotFound, err.Error())
}
return nil, err
}
return vol, nil
}
// List returns all volumes on this host.
func (vm *VolumeManager) List(ctx context.Context, labels map[string]string) ([]*types.Volume, error) {
return vm.core.ListVolumes(labels)
}
// Remove is used to delete an existing volume.
func (vm *VolumeManager) Remove(ctx context.Context, name string) error {
vol, err := vm.Get(ctx, name)
if err != nil {
return errors.Wrapf(err, "failed to get volume(%s)", name)
}
ref := vol.Option(types.OptionRef)
if ref != "" {
return errors.Wrapf(errtypes.ErrVolumeInUse, "failed to remove volume(%s)", name)
}
id := types.VolumeContext{
Name: name,
}
if err := vm.core.RemoveVolume(id); err != nil {
if strings.Contains(err.Error(), "not found") {
return errors.Wrap(errtypes.ErrVolumeNotFound, err.Error())
}
return err
}
vm.LogVolumeEvent(ctx, name, "destroy", map[string]string{"driver": vol.Driver()})
return nil
}
// Path returns the mount path of volume.
func (vm *VolumeManager) Path(ctx context.Context, name string) (string, error) {
id := types.VolumeContext{
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.VolumeContext{
Name: name,
}
v, err := vm.Get(ctx, name)
if err != nil {
return nil, errors.Errorf("failed to get volume(%s): %v", name, err)
}
if options == nil {
options = make(map[string]string)
}
cid, ok := options[types.OptionRef]
if ok && cid != "" {
ref := v.Option(types.OptionRef)
if ref == "" {
options[types.OptionRef] = cid
} else if !strings.Contains(ref, cid) {
options[types.OptionRef] = strings.Join([]string{ref, cid}, ",")
}
}
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.VolumeContext{
Name: name,
}
v, err := vm.Get(ctx, name)
if err != nil {
return nil, errors.Wrapf(err, "failed to get volume(%s)", name)
}
if options == nil {
options = make(map[string]string)
}
cid, ok := options[types.OptionRef]
if ok && cid != "" {
ref := v.Option(types.OptionRef)
if !strings.Contains(ref, cid) {
return v, nil
}
if ref != "" {
ids := strings.Split(ref, ",")
ids = utils.StringSliceDelete(ids, cid)
if len(ids) > 0 {
options[types.OptionRef] = strings.Join(ids, ",")
} else {
options[types.OptionRef] = ""
}
}
}
return vm.core.DetachVolume(id, options)
}