-
Notifications
You must be signed in to change notification settings - Fork 18.7k
/
volume.go
311 lines (268 loc) · 9.13 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package convert // import "github.com/docker/docker/daemon/cluster/convert"
import (
volumetypes "github.com/docker/docker/api/types/volume"
gogotypes "github.com/gogo/protobuf/types"
swarmapi "github.com/moby/swarmkit/v2/api"
)
// VolumeFromGRPC converts a swarmkit api Volume object to a docker api Volume
// object
func VolumeFromGRPC(v *swarmapi.Volume) volumetypes.Volume {
clusterVolumeSpec := volumetypes.ClusterVolumeSpec{
Group: v.Spec.Group,
AccessMode: accessModeFromGRPC(v.Spec.AccessMode),
AccessibilityRequirements: topologyRequirementFromGRPC(v.Spec.AccessibilityRequirements),
CapacityRange: capacityRangeFromGRPC(v.Spec.CapacityRange),
Secrets: volumeSecretsFromGRPC(v.Spec.Secrets),
Availability: volumeAvailabilityFromGRPC(v.Spec.Availability),
}
clusterVolume := &volumetypes.ClusterVolume{
ID: v.ID,
Spec: clusterVolumeSpec,
PublishStatus: volumePublishStatusFromGRPC(v.PublishStatus),
Info: volumeInfoFromGRPC(v.VolumeInfo),
}
clusterVolume.Version.Index = v.Meta.Version.Index
clusterVolume.CreatedAt, _ = gogotypes.TimestampFromProto(v.Meta.CreatedAt)
clusterVolume.UpdatedAt, _ = gogotypes.TimestampFromProto(v.Meta.UpdatedAt)
return volumetypes.Volume{
ClusterVolume: clusterVolume,
CreatedAt: clusterVolume.CreatedAt.String(),
Driver: v.Spec.Driver.Name,
Labels: v.Spec.Annotations.Labels,
Name: v.Spec.Annotations.Name,
Options: v.Spec.Driver.Options,
Scope: "global",
}
}
func volumeSpecToGRPC(spec volumetypes.ClusterVolumeSpec) *swarmapi.VolumeSpec {
swarmSpec := &swarmapi.VolumeSpec{
Group: spec.Group,
}
if spec.AccessMode != nil {
swarmSpec.AccessMode = &swarmapi.VolumeAccessMode{}
switch spec.AccessMode.Scope {
case volumetypes.ScopeSingleNode:
swarmSpec.AccessMode.Scope = swarmapi.VolumeScopeSingleNode
case volumetypes.ScopeMultiNode:
swarmSpec.AccessMode.Scope = swarmapi.VolumeScopeMultiNode
}
switch spec.AccessMode.Sharing {
case volumetypes.SharingNone:
swarmSpec.AccessMode.Sharing = swarmapi.VolumeSharingNone
case volumetypes.SharingReadOnly:
swarmSpec.AccessMode.Sharing = swarmapi.VolumeSharingReadOnly
case volumetypes.SharingOneWriter:
swarmSpec.AccessMode.Sharing = swarmapi.VolumeSharingOneWriter
case volumetypes.SharingAll:
swarmSpec.AccessMode.Sharing = swarmapi.VolumeSharingAll
}
if spec.AccessMode.BlockVolume != nil {
swarmSpec.AccessMode.AccessType = &swarmapi.VolumeAccessMode_Block{
Block: &swarmapi.VolumeAccessMode_BlockVolume{},
}
}
if spec.AccessMode.MountVolume != nil {
swarmSpec.AccessMode.AccessType = &swarmapi.VolumeAccessMode_Mount{
Mount: &swarmapi.VolumeAccessMode_MountVolume{
FsType: spec.AccessMode.MountVolume.FsType,
MountFlags: spec.AccessMode.MountVolume.MountFlags,
},
}
}
}
for _, secret := range spec.Secrets {
swarmSpec.Secrets = append(swarmSpec.Secrets, &swarmapi.VolumeSecret{
Key: secret.Key,
Secret: secret.Secret,
})
}
if spec.AccessibilityRequirements != nil {
swarmSpec.AccessibilityRequirements = &swarmapi.TopologyRequirement{}
for _, top := range spec.AccessibilityRequirements.Requisite {
swarmSpec.AccessibilityRequirements.Requisite = append(
swarmSpec.AccessibilityRequirements.Requisite,
&swarmapi.Topology{
Segments: top.Segments,
},
)
}
for _, top := range spec.AccessibilityRequirements.Preferred {
swarmSpec.AccessibilityRequirements.Preferred = append(
swarmSpec.AccessibilityRequirements.Preferred,
&swarmapi.Topology{
Segments: top.Segments,
},
)
}
}
if spec.CapacityRange != nil {
swarmSpec.CapacityRange = &swarmapi.CapacityRange{
RequiredBytes: spec.CapacityRange.RequiredBytes,
LimitBytes: spec.CapacityRange.LimitBytes,
}
}
// availability is not a pointer, it is a value. if the user does not
// specify an availability, it will be inferred as the 0-value, which is
// "active".
switch spec.Availability {
case volumetypes.AvailabilityActive:
swarmSpec.Availability = swarmapi.VolumeAvailabilityActive
case volumetypes.AvailabilityPause:
swarmSpec.Availability = swarmapi.VolumeAvailabilityPause
case volumetypes.AvailabilityDrain:
swarmSpec.Availability = swarmapi.VolumeAvailabilityDrain
}
return swarmSpec
}
// VolumeCreateToGRPC takes a VolumeCreateBody and outputs the matching
// swarmapi VolumeSpec.
func VolumeCreateToGRPC(volume *volumetypes.CreateOptions) *swarmapi.VolumeSpec {
var swarmSpec *swarmapi.VolumeSpec
if volume != nil && volume.ClusterVolumeSpec != nil {
swarmSpec = volumeSpecToGRPC(*volume.ClusterVolumeSpec)
} else {
swarmSpec = &swarmapi.VolumeSpec{}
}
swarmSpec.Annotations = swarmapi.Annotations{
Name: volume.Name,
Labels: volume.Labels,
}
swarmSpec.Driver = &swarmapi.Driver{
Name: volume.Driver,
Options: volume.DriverOpts,
}
return swarmSpec
}
func volumeInfoFromGRPC(info *swarmapi.VolumeInfo) *volumetypes.Info {
if info == nil {
return nil
}
var accessibleTopology []volumetypes.Topology
if info.AccessibleTopology != nil {
accessibleTopology = make([]volumetypes.Topology, len(info.AccessibleTopology))
for i, top := range info.AccessibleTopology {
accessibleTopology[i] = topologyFromGRPC(top)
}
}
return &volumetypes.Info{
CapacityBytes: info.CapacityBytes,
VolumeContext: info.VolumeContext,
VolumeID: info.VolumeID,
AccessibleTopology: accessibleTopology,
}
}
func volumePublishStatusFromGRPC(publishStatus []*swarmapi.VolumePublishStatus) []*volumetypes.PublishStatus {
if publishStatus == nil {
return nil
}
vps := make([]*volumetypes.PublishStatus, len(publishStatus))
for i, status := range publishStatus {
var state volumetypes.PublishState
switch status.State {
case swarmapi.VolumePublishStatus_PENDING_PUBLISH:
state = volumetypes.StatePending
case swarmapi.VolumePublishStatus_PUBLISHED:
state = volumetypes.StatePublished
case swarmapi.VolumePublishStatus_PENDING_NODE_UNPUBLISH:
state = volumetypes.StatePendingNodeUnpublish
case swarmapi.VolumePublishStatus_PENDING_UNPUBLISH:
state = volumetypes.StatePendingUnpublish
}
vps[i] = &volumetypes.PublishStatus{
NodeID: status.NodeID,
State: state,
PublishContext: status.PublishContext,
}
}
return vps
}
func accessModeFromGRPC(accessMode *swarmapi.VolumeAccessMode) *volumetypes.AccessMode {
if accessMode == nil {
return nil
}
convertedAccessMode := &volumetypes.AccessMode{}
switch accessMode.Scope {
case swarmapi.VolumeScopeSingleNode:
convertedAccessMode.Scope = volumetypes.ScopeSingleNode
case swarmapi.VolumeScopeMultiNode:
convertedAccessMode.Scope = volumetypes.ScopeMultiNode
}
switch accessMode.Sharing {
case swarmapi.VolumeSharingNone:
convertedAccessMode.Sharing = volumetypes.SharingNone
case swarmapi.VolumeSharingReadOnly:
convertedAccessMode.Sharing = volumetypes.SharingReadOnly
case swarmapi.VolumeSharingOneWriter:
convertedAccessMode.Sharing = volumetypes.SharingOneWriter
case swarmapi.VolumeSharingAll:
convertedAccessMode.Sharing = volumetypes.SharingAll
}
if block := accessMode.GetBlock(); block != nil {
convertedAccessMode.BlockVolume = &volumetypes.TypeBlock{}
}
if mount := accessMode.GetMount(); mount != nil {
convertedAccessMode.MountVolume = &volumetypes.TypeMount{
FsType: mount.FsType,
MountFlags: mount.MountFlags,
}
}
return convertedAccessMode
}
func volumeSecretsFromGRPC(secrets []*swarmapi.VolumeSecret) []volumetypes.Secret {
if secrets == nil {
return nil
}
convertedSecrets := make([]volumetypes.Secret, len(secrets))
for i, secret := range secrets {
convertedSecrets[i] = volumetypes.Secret{
Key: secret.Key,
Secret: secret.Secret,
}
}
return convertedSecrets
}
func topologyRequirementFromGRPC(top *swarmapi.TopologyRequirement) *volumetypes.TopologyRequirement {
if top == nil {
return nil
}
convertedTop := &volumetypes.TopologyRequirement{}
if top.Requisite != nil {
convertedTop.Requisite = make([]volumetypes.Topology, len(top.Requisite))
for i, req := range top.Requisite {
convertedTop.Requisite[i] = topologyFromGRPC(req)
}
}
if top.Preferred != nil {
convertedTop.Preferred = make([]volumetypes.Topology, len(top.Preferred))
for i, pref := range top.Preferred {
convertedTop.Preferred[i] = topologyFromGRPC(pref)
}
}
return convertedTop
}
func topologyFromGRPC(top *swarmapi.Topology) volumetypes.Topology {
if top == nil {
return volumetypes.Topology{}
}
return volumetypes.Topology{
Segments: top.Segments,
}
}
func capacityRangeFromGRPC(capacity *swarmapi.CapacityRange) *volumetypes.CapacityRange {
if capacity == nil {
return nil
}
return &volumetypes.CapacityRange{
RequiredBytes: capacity.RequiredBytes,
LimitBytes: capacity.LimitBytes,
}
}
func volumeAvailabilityFromGRPC(availability swarmapi.VolumeSpec_VolumeAvailability) volumetypes.Availability {
switch availability {
case swarmapi.VolumeAvailabilityActive:
return volumetypes.AvailabilityActive
case swarmapi.VolumeAvailabilityPause:
return volumetypes.AvailabilityPause
}
return volumetypes.AvailabilityDrain
}