This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
321 lines (273 loc) · 11.3 KB
/
controller.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
312
313
314
315
316
317
318
319
320
321
package driver
import (
"context"
"csi/backend"
"encoding/json"
"fmt"
"utils"
"utils/log"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
name := req.GetName()
log.Infof("Start to create volume %s", name)
capacityRange := req.GetCapacityRange()
if capacityRange == nil || capacityRange.RequiredBytes <= 0 {
msg := "CreateVolume CapacityRange must be provided"
log.Errorln(msg)
return nil, status.Error(codes.InvalidArgument, msg)
}
parameters := utils.CopyMap(req.GetParameters())
size := capacityRange.RequiredBytes
parameters["size"] = capacityRange.RequiredBytes
cloneFrom, exist := parameters["cloneFrom"].(string)
if exist && cloneFrom != "" {
parameters["backend"], parameters["cloneFrom"] = utils.SplitVolumeId(cloneFrom)
}
contentSource := req.GetVolumeContentSource()
if contentSource != nil {
if contentSnapshot := contentSource.GetSnapshot(); contentSnapshot != nil {
sourceSnapshotId := contentSnapshot.GetSnapshotId()
sourceBackendName, snapshotParentId, sourceSnapshotName := utils.SplitSnapshotId(sourceSnapshotId)
parameters["sourceSnapshotName"] = sourceSnapshotName
parameters["snapshotParentId"] = snapshotParentId
parameters["backend"] = sourceBackendName
log.Infof("Start to create volume from snapshot %s", sourceSnapshotName)
} else if contentVolume := contentSource.GetVolume(); contentVolume != nil {
sourceVolumeId := contentVolume.GetVolumeId()
sourceBackendName, sourceVolumeName := utils.SplitVolumeId(sourceVolumeId)
parameters["sourceVolumeName"] = sourceVolumeName
parameters["backend"] = sourceBackendName
log.Infof("Start to create volume from volume %s", sourceVolumeName)
} else {
log.Errorf("The source %s is not snapshot either volume", contentSource)
return nil, status.Error(codes.InvalidArgument, "no source ID provided is invalid")
}
}
localPool, remotePool, err := backend.SelectStoragePool(size, parameters)
if err != nil {
log.Errorf("Cannot select pool for volume creation: %v", err)
return nil, status.Error(codes.Internal, err.Error())
}
parameters["storagepool"] = localPool.Name
if remotePool != nil {
parameters["metroDomain"] = backend.GetMetroDomain(remotePool.Parent)
parameters["vStorePairID"] = backend.GetMetrovStorePairID(remotePool.Parent)
parameters["remoteStoragePool"] = remotePool.Name
}
volName, err := localPool.Plugin.CreateVolume(name, parameters)
if err != nil {
log.Errorf("Create volume %s error: %v", name, err)
return nil, status.Error(codes.Internal, err.Error())
}
log.Infof("Volume %s is created", name)
if contentSource != nil {
attributes := map[string]string{
"backend": localPool.Parent,
"name": volName,
}
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: localPool.Parent + "." + volName,
CapacityBytes: size,
VolumeContext: attributes,
ContentSource: req.VolumeContentSource,
},
}, nil
}
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: localPool.Parent + "." + volName,
CapacityBytes: size,
},
}, nil
}
func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
volumeId := req.GetVolumeId()
log.Infof("Start to delete volume %s", volumeId)
backendName, volName := utils.SplitVolumeId(volumeId)
backend := backend.GetBackend(backendName)
if backend == nil {
log.Warningf("Backend %s doesn't exist. Ignore this request and return success. "+
"CAUTION: volume need to manually delete from array.", backendName)
return &csi.DeleteVolumeResponse{}, nil
}
err := backend.Plugin.DeleteVolume(volName)
if err != nil {
log.Errorf("Delete volume %s error: %v", volumeId, err)
return nil, status.Error(codes.Internal, err.Error())
}
log.Infof("Volume %s is deleted", volumeId)
return &csi.DeleteVolumeResponse{}, nil
}
func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
volumeId := req.GetVolumeId()
if volumeId == "" {
return nil, status.Error(codes.InvalidArgument, "no volume ID provided")
}
log.Infof("Start to controller expand volume %s", volumeId)
if req.GetCapacityRange() == nil {
return nil, status.Error(codes.InvalidArgument, "no capacity range provided")
}
minSize := req.GetCapacityRange().GetRequiredBytes()
maxSize := req.GetCapacityRange().GetLimitBytes()
if 0 < maxSize && maxSize < minSize {
return nil, status.Error(codes.InvalidArgument, "limitBytes is smaller than requiredBytes")
}
backendName, volName := utils.SplitVolumeId(volumeId)
backend := backend.GetBackend(backendName)
if backend == nil {
msg := fmt.Sprintf("Backend %s doesn't exist", backendName)
log.Errorln(msg)
return nil, status.Error(codes.Internal, msg)
}
nodeExpansionRequired, err := backend.Plugin.ExpandVolume(volName, minSize)
if err != nil {
log.Errorf("Expand volume %s error: %v", volumeId, err)
return nil, status.Error(codes.Internal, err.Error())
}
log.Infof("Volume %s is expanded to %d, nodeExpansionRequired %t", volName, minSize, nodeExpansionRequired)
return &csi.ControllerExpandVolumeResponse{
CapacityBytes: minSize,
NodeExpansionRequired: nodeExpansionRequired,
}, nil
}
func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
// Volume attachment will be done at node stage process
return &csi.ControllerPublishVolumeResponse{}, nil
}
func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
volumeId := req.GetVolumeId()
nodeInfo := req.GetNodeId()
log.Infof("Start to controller unpublish volume %s from node %s", volumeId, nodeInfo)
backendName, volName := utils.SplitVolumeId(volumeId)
backend := backend.GetBackend(backendName)
if backend == nil {
log.Warningf("Backend %s doesn't exist. Ignore this request and return success. "+
"CAUTION: volume %s need to manually detach from array.", backendName, volName)
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
var parameters map[string]interface{}
err := json.Unmarshal([]byte(nodeInfo), ¶meters)
if err != nil {
log.Errorf("Unmarshal node info of %s error: %v", nodeInfo, err)
return nil, status.Error(codes.Internal, err.Error())
}
err = backend.Plugin.DetachVolume(volName, parameters)
if err != nil {
log.Errorf("Unpublish volume %s from node %s error: %v", volName, nodeInfo, err)
return nil, status.Error(codes.Internal, err.Error())
}
log.Infof("Volume %s is controller unpublished from node %s", volumeId, nodeInfo)
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
func (d *Driver) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
return nil, status.Error(codes.Unimplemented, "Not implemented")
}
func (d *Driver) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
return nil, status.Error(codes.Unimplemented, "Not implemented")
}
func (d *Driver) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
return nil, status.Error(codes.Unimplemented, "Not implemented")
}
func (d *Driver) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
return &csi.ControllerGetCapabilitiesResponse{
Capabilities: []*csi.ControllerServiceCapability{
&csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
},
},
},
&csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
},
},
},
&csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
},
},
},
&csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
},
},
},
&csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
},
},
},
},
}, nil
}
func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
volumeId := req.GetSourceVolumeId()
if volumeId == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
snapshotName := req.GetName()
if snapshotName == "" {
return nil, status.Error(codes.InvalidArgument, "Snapshot Name missing in request")
}
log.Infof("Start to Create snapshot %s for volume %s", snapshotName, volumeId)
backendName, volName := utils.SplitVolumeId(volumeId)
backend := backend.GetBackend(backendName)
if backend == nil {
msg := fmt.Sprintf("Backend %s doesn't exist", backendName)
log.Errorln(msg)
return nil, status.Error(codes.Internal, msg)
}
snapshot, err := backend.Plugin.CreateSnapshot(volName, snapshotName)
if err != nil {
log.Errorf("Create snapshot %s error: %v", snapshotName, err)
return nil, status.Error(codes.Internal, err.Error())
}
log.Infof("Finish to Create snapshot %s for volume %s", snapshotName, volumeId)
return &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
SizeBytes: snapshot["SizeBytes"].(int64),
SnapshotId: backendName + "." + snapshot["ParentID"].(string) + "." + snapshotName,
SourceVolumeId: volumeId,
CreationTime: ×tamp.Timestamp{Seconds: snapshot["CreationTime"].(int64)},
ReadyToUse: true,
},
}, nil
}
func (d *Driver) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
snapshotId := req.GetSnapshotId()
if snapshotId == "" {
return nil, status.Error(codes.InvalidArgument, "Snapshot ID missing in request")
}
log.Infof("Start to Delete snapshot %s.", snapshotId)
backendName, snapshotParentId, snapshotName := utils.SplitSnapshotId(snapshotId)
backend := backend.GetBackend(backendName)
if backend == nil {
msg := fmt.Sprintf("Backend %s doesn't exist", backendName)
log.Errorln(msg)
return nil, status.Error(codes.Internal, msg)
}
err := backend.Plugin.DeleteSnapshot(snapshotParentId, snapshotName)
if err != nil {
log.Errorf("Delete snapshot %s error: %v", snapshotName, err)
return nil, status.Error(codes.Internal, err.Error())
}
log.Infof("Finish to Delete snapshot %s", snapshotId)
return &csi.DeleteSnapshotResponse{}, nil
}
func (d *Driver) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}