-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllerserver.go
256 lines (201 loc) · 9.24 KB
/
controllerserver.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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sfs
import (
"fmt"
"time"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/sfs/v2/shares"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog"
)
type controllerServer struct {
Driver *SfsDriver
}
func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
klog.V(2).Infoln("*********** Creat Volume begin ***********")
startTime := time.Now().UnixMilli()
klog.V(2).Infof("CreateVolume called with request %v", *req)
if err := validateCreateVolumeRequest(req); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
client, err := cs.Driver.cloud.SFSV2Client()
if err != nil {
klog.V(2).Infof("Failed to create SFS v2 client: %v", err)
return nil, status.Error(codes.Internal, err.Error())
}
requestedSize := req.GetCapacityRange().GetRequiredBytes()
if requestedSize == 0 {
// At least 1GiB
requestedSize = 1 * bytesInGiB
}
sizeInGiB := bytesToGiB(requestedSize)
// Creating a share
createOpts := shares.CreateOpts{
ShareProto: cs.Driver.shareProto,
Size: sizeInGiB,
Name: req.GetName(),
}
share, err := createShare(client, &createOpts)
if err != nil {
klog.V(2).Infof("Failed to create SFS volume %v: %v", *req, err)
return nil, status.Error(codes.Internal, err.Error())
}
// Grant access to the share
ruleStartTime := time.Now().UnixMilli()
klog.V(2).Infof("Share: %v(ID: %v) creating an access ruleto VPC %s",
share.Name, share.ID, cs.Driver.cloud.Vpc.Id)
if err := grantAccess(client, share.ID, cs.Driver.cloud.Vpc.Id); err != nil {
klog.V(2).Infof("%v(ID: %v) Failed to create access rule for share: %v", share.Name, share.ID, err)
return nil, status.Error(codes.Internal, err.Error())
}
klog.V(2).Infof("Volume created for request %v, time used: %v (rule: %v) ms, VolumeId: %s, contentSource: %v, CapacityBytes: %v",
*req, time.Now().UnixMilli()-startTime, time.Now().UnixMilli()-ruleStartTime, share.ID, req.GetVolumeContentSource(), time.Now().UnixMilli())
klog.V(2).Infoln("*********** Create Volume over ***********")
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: share.ID,
ContentSource: req.GetVolumeContentSource(),
CapacityBytes: int64(sizeInGiB) * bytesInGiB,
},
}, nil
}
func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
klog.V(2).Infoln("*********** Delete Volume begin ***********")
klog.V(2).Infof("DeleteVolume called with request %v", *req)
volID := req.GetVolumeId()
if len(volID) == 0 {
return nil, status.Error(codes.InvalidArgument, "DeleteVolume Volume ID must be provided")
}
client, err := cs.Driver.cloud.SFSV2Client()
if err != nil {
klog.V(3).Infof("Failed to create SFS v2 client: %v", err)
return nil, status.Error(codes.Internal, err.Error())
}
err = deleteShare(client, volID)
if err != nil {
klog.V(3).Infof("Failed to DeleteVolume: %v", err)
return nil, status.Error(codes.Internal, fmt.Sprintf("DeleteVolume failed with error %v", err))
}
klog.V(4).Infof("Delete volume %s", volID)
klog.V(2).Infoln("*********** Delete Volume over ***********")
return &csi.DeleteVolumeResponse{}, nil
}
func (cs *controllerServer) ControllerGetVolume(ctx context.Context, req *csi.ControllerGetVolumeRequest) (*csi.ControllerGetVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (cs *controllerServer) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (cs *controllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (cs *controllerServer) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (cs *controllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (cs *controllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// ControllerGetCapabilities implements the default GRPC callout.
// Default supports all capabilities
func (cs *controllerServer) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
klog.V(2).Infof("ControllerGetCapabilities called with request %v", *req)
return &csi.ControllerGetCapabilitiesResponse{
Capabilities: cs.Driver.cscap,
}, nil
}
func (cs *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
klog.V(2).Infof("ValidateVolumeCapabilities called with args %+v", *req)
reqVolCap := req.GetVolumeCapabilities()
if reqVolCap == nil || len(reqVolCap) == 0 {
return nil, status.Error(codes.InvalidArgument, "ValidateVolumeCapabilities Volume Capabilities must be provided")
}
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "ValidateVolumeCapabilities Volume ID must be provided")
}
client, err := cs.Driver.cloud.SFSV2Client()
if err != nil {
klog.V(3).Infof("ValidateVolumeCapabilities Failed to create SFS v2 client: %v", err)
return nil, status.Error(codes.Internal, err.Error())
}
_, err = getShare(client, volumeID)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return nil, status.Error(codes.NotFound, fmt.Sprintf("ValidateVolumeCapabiltites Volume %s not found", volumeID))
}
return nil, status.Error(codes.Internal, fmt.Sprintf("ValidateVolumeCapabiltites %v", err))
}
for _, c := range reqVolCap {
if c.GetAccessMode().Mode == csi.VolumeCapability_AccessMode_MULTI_NODE_SINGLE_WRITER {
return &csi.ValidateVolumeCapabilitiesResponse{}, nil
}
}
confirmed := &csi.ValidateVolumeCapabilitiesResponse_Confirmed{VolumeCapabilities: reqVolCap}
return &csi.ValidateVolumeCapabilitiesResponse{Confirmed: confirmed}, nil
}
func (cs *controllerServer) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
return nil, status.Error(codes.Unimplemented, fmt.Sprintf("GetCapacity is not yet implemented"))
}
func (cs *controllerServer) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
/*
func (cs *controllerServer) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
klog.V(4).Infof("ControllerExpandVolume: called with args %+v", *req)
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
cap := req.GetCapacityRange()
if cap == nil {
return nil, status.Error(codes.InvalidArgument, "Capacity range not provided")
}
volSizeBytes := int64(req.GetCapacityRange().GetRequiredBytes())
volSizeGB := int(RoundUpSize(volSizeBytes, 1024*1024*1024))
maxVolSize := cap.GetLimitBytes()
if maxVolSize > 0 && maxVolSize < volSizeBytes {
return nil, status.Error(codes.OutOfRange, "After round-up, volume size exceeds the limit specified")
}
client, err := cs.Driver.cloud.SFSV2Client()
if err != nil {
klog.V(3).Infof("Failed to create SFS v2 client: %v", err)
return nil, status.Error(codes.Internal, err.Error())
}
_, err = getShare(client, volumeID)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return nil, status.Error(codes.NotFound, "Volume not found")
}
return nil, status.Error(codes.Internal, fmt.Sprintf("GetVolume failed with error %v", err))
}
err = expandShare(client, volumeID, volSizeGB)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("Could not resize volume %q to size %v: %v", volumeID, volSizeGB, err))
}
klog.V(4).Infof("ControllerExpandVolume resized volume %v to size %v", volumeID, volSizeGB)
return &csi.ControllerExpandVolumeResponse{
CapacityBytes: volSizeBytes,
NodeExpansionRequired: true,
}, nil
}
*/