forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sio_mgr.go
258 lines (218 loc) · 6.92 KB
/
sio_mgr.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
/*
Copyright 2017 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 scaleio
import (
"errors"
"strconv"
"github.com/golang/glog"
siotypes "github.com/codedellemc/goscaleio/types/v1"
)
type storageInterface interface {
CreateVolume(string, int64) (*siotypes.Volume, error)
AttachVolume(string) (string, error)
IsAttached(string) (bool, error)
DetachVolume(string) error
DeleteVolume(string) error
}
type sioMgr struct {
client sioInterface
configData map[string]string
}
func newSioMgr(configs map[string]string) (*sioMgr, error) {
if configs == nil {
return nil, errors.New("missing configuration data")
}
configs[confKey.protectionDomain] = defaultString(configs[confKey.protectionDomain], "default")
configs[confKey.storagePool] = defaultString(configs[confKey.storagePool], "default")
configs[confKey.sdcRootPath] = defaultString(configs[confKey.sdcRootPath], sdcRootPath)
configs[confKey.storageMode] = defaultString(configs[confKey.storageMode], "ThinProvisioned")
mgr := &sioMgr{configData: configs}
return mgr, nil
}
// getClient safely returns an sioInterface
func (m *sioMgr) getClient() (sioInterface, error) {
if m.client == nil {
glog.V(4).Info(log("creating scaleio client"))
configs := m.configData
username := configs[confKey.username]
password := configs[confKey.password]
gateway := configs[confKey.gateway]
b, err := strconv.ParseBool(configs[confKey.sslEnabled])
if err != nil {
glog.Error(log("failed to parse sslEnabled, must be either \"true\" or \"false\""))
return nil, err
}
certsEnabled := b
glog.V(4).Info(log("creating new client for gateway %s", gateway))
client, err := newSioClient(gateway, username, password, certsEnabled)
if err != nil {
glog.Error(log("failed to create scaleio client: %v", err))
return nil, err
}
client.sysName = configs[confKey.system]
client.pdName = configs[confKey.protectionDomain]
client.spName = configs[confKey.storagePool]
client.sdcPath = configs[confKey.sdcRootPath]
client.provisionMode = configs[confKey.storageMode]
m.client = client
glog.V(4).Info(log("client created successfully [gateway=%s]", gateway))
}
return m.client, nil
}
// CreateVolume creates a new ScaleIO volume
func (m *sioMgr) CreateVolume(volName string, sizeGB int64) (*siotypes.Volume, error) {
client, err := m.getClient()
if err != nil {
return nil, err
}
glog.V(4).Infof("scaleio: creating volume %s", volName)
vol, err := client.CreateVolume(volName, sizeGB)
if err != nil {
glog.V(4).Infof("scaleio: failed creating volume %s: %v", volName, err)
return nil, err
}
glog.V(4).Infof("scaleio: created volume %s successfully", volName)
return vol, nil
}
// AttachVolume maps a ScaleIO volume to the running node
func (m *sioMgr) AttachVolume(volName string) (string, error) {
client, err := m.getClient()
if err != nil {
glog.Error(log("attach volume failed: %v", err))
return "", err
}
glog.V(4).Infoln(log("attaching volume %s", volName))
iid, err := client.IID()
if err != nil {
glog.Error(log("failed to get instanceID"))
return "", err
}
glog.V(4).Info(log("attaching volume %s to host instance %s", volName, iid))
devs, err := client.Devs()
if err != nil {
return "", err
}
vol, err := client.FindVolume(volName)
if err != nil {
glog.Error(log("failed to find volume %s: %v", volName, err))
return "", err
}
// handle vol if already attached
if len(vol.MappedSdcInfo) > 0 {
if m.isSdcMappedToVol(iid, vol) {
glog.V(4).Info(log("skippping attachment, volume %s already attached to sdc %s", volName, iid))
return devs[vol.ID], nil
}
}
// attach volume, get deviceName
if err := client.AttachVolume(sioVolumeID(vol.ID)); err != nil {
glog.Error(log("attachment for volume %s failed :%v", volName, err))
return "", err
}
device, err := client.WaitForAttachedDevice(vol.ID)
if err != nil {
glog.Error(log("failed while waiting for device to attach: %v", err))
return "", err
}
glog.V(4).Info(log("volume %s attached successfully as %s to instance %s", volName, device, iid))
return device, nil
}
// IsAttached verifies that the named ScaleIO volume is still attached
func (m *sioMgr) IsAttached(volName string) (bool, error) {
client, err := m.getClient()
if err != nil {
return false, err
}
iid, err := client.IID()
if err != nil {
glog.Error("scaleio: failed to get instanceID")
return false, err
}
vol, err := client.FindVolume(volName)
if err != nil {
return false, err
}
return m.isSdcMappedToVol(iid, vol), nil
}
// DetachVolume detaches the name ScaleIO volume from an instance
func (m *sioMgr) DetachVolume(volName string) error {
client, err := m.getClient()
if err != nil {
return err
}
iid, err := client.IID()
if err != nil {
glog.Error(log("failed to get instanceID: %v", err))
return err
}
vol, err := client.FindVolume(volName)
if err != nil {
return err
}
if !m.isSdcMappedToVol(iid, vol) {
glog.Warning(log(
"skipping detached, vol %s not attached to instance %s",
volName, iid,
))
return nil
}
if err := client.DetachVolume(sioVolumeID(vol.ID)); err != nil {
glog.Error(log("failed to detach vol %s: %v", volName, err))
return err
}
glog.V(4).Info(log("volume %s detached successfully", volName))
return nil
}
// DeleteVolumes removes the ScaleIO volume
func (m *sioMgr) DeleteVolume(volName string) error {
client, err := m.getClient()
if err != nil {
return err
}
iid, err := client.IID()
if err != nil {
glog.Error(log("failed to get instanceID: %v", err))
return err
}
vol, err := client.FindVolume(volName)
if err != nil {
return err
}
// if still attached, stop
if m.isSdcMappedToVol(iid, vol) {
glog.Error(log("volume %s still attached, unable to delete", volName))
return errors.New("volume still attached")
}
if err := client.DeleteVolume(sioVolumeID(vol.ID)); err != nil {
glog.Error(log("failed to delete volume %s: %v", volName, err))
return err
}
glog.V(4).Info(log("deleted volume %s successfully", volName))
return nil
}
//*****************************************************************
// Helpers
//*****************************************************************
// isSdcMappedToVol returns true if the sdc is mapped to the volume
func (m *sioMgr) isSdcMappedToVol(sdcID string, vol *siotypes.Volume) bool {
if len(vol.MappedSdcInfo) == 0 {
glog.V(4).Info(log("no attachment found"))
return false
}
for _, sdcInfo := range vol.MappedSdcInfo {
if sdcInfo.SdcID == sdcID {
return true
}
}
return false
}