This repository has been archived by the owner on Aug 14, 2021. It is now read-only.
forked from kubernetes-retired/external-storage
-
Notifications
You must be signed in to change notification settings - Fork 14
/
processor.go
378 lines (316 loc) · 11.8 KB
/
processor.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
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 openebs
import (
"fmt"
"os"
"strings"
"time"
"github.com/golang/glog"
"github.com/kubernetes-incubator/external-storage/openebs/pkg/apis/openebs.io/v1alpha1"
"github.com/kubernetes-incubator/external-storage/openebs/pkg/provisioner"
mvol_v1alpha1 "github.com/kubernetes-incubator/external-storage/openebs/pkg/volume/v1alpha1"
crdv1 "github.com/kubernetes-incubator/external-storage/snapshot/pkg/apis/crd/v1"
"github.com/kubernetes-incubator/external-storage/snapshot/pkg/cloudprovider"
"github.com/kubernetes-incubator/external-storage/snapshot/pkg/volume"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
openEBSPersistentDiskPluginName = "openebs"
)
var (
// SnapSupportedCASType is a map of supported volume type for snapshot
// operations.
SnapSupportedCASType = map[string]bool{
"jiva": true,
"cstor": true,
}
)
type openEBSPlugin struct {
mvol_v1alpha1.CASVolume
}
var _ volume.Plugin = &openEBSPlugin{}
// RegisterPlugin registers the volume plugin
func RegisterPlugin() volume.Plugin {
return &openEBSPlugin{}
}
func init() {
// GetMayaService get the maya-service endpoint
_ = GetMayaService()
}
// GetPluginName gets the name of the volume plugin
func GetPluginName() string {
return "openebs"
}
func (h *openEBSPlugin) Init(_ cloudprovider.Interface) {
}
func (h *openEBSPlugin) SnapshotCreate(snapshot *crdv1.VolumeSnapshot, pv *v1.PersistentVolume, tags *map[string]string) (*crdv1.VolumeSnapshotDataSource, *[]crdv1.VolumeSnapshotCondition, error) {
spec := &pv.Spec
if spec == nil || spec.ISCSI == nil {
return nil, nil, fmt.Errorf("invalid PV spec %v", spec)
}
snapObj := (*tags)["kubernetes.io/created-for/snapshot/name"]
snapshotName := createSnapshotName(pv.Name, snapObj)
casType := pv.Annotations["openebs.io/cas-type"]
if casType == "" {
casType = "jiva"
}
ok := SnapSupportedCASType[casType]
if !ok {
return nil, nil, fmt.Errorf("aborting create snapshot operation as specified volume type (%s) does not support snapshots", casType)
}
_, err := h.CreateSnapshot(casType, pv.Name, snapshotName, pv.Spec.ClaimRef.Namespace)
if err != nil {
glog.Errorf("failed to create snapshot for volume :%v, err: %v", pv.Name, err)
return nil, nil, err
}
glog.V(1).Info("snapshot %v created successfully", snapshotName)
cond := []crdv1.VolumeSnapshotCondition{}
if err == nil {
cond = []crdv1.VolumeSnapshotCondition{
{
Status: v1.ConditionTrue,
Message: "Snapshot created successfully",
LastTransitionTime: metav1.Now(),
Type: crdv1.VolumeSnapshotConditionReady,
},
}
} else {
glog.V(2).Infof("failed to create snapshot, err: %v", err)
cond = []crdv1.VolumeSnapshotCondition{
{
Status: v1.ConditionTrue,
Message: fmt.Sprintf("Failed to create the snapshot: %v", err),
LastTransitionTime: metav1.Now(),
Type: crdv1.VolumeSnapshotConditionError,
},
}
}
sizeResource := pv.Spec.Capacity[v1.ResourceName(v1.ResourceStorage)]
res := &crdv1.VolumeSnapshotDataSource{
OpenEBSSnapshot: &crdv1.OpenEBSVolumeSnapshotSource{
SnapshotID: snapshotName,
Capacity: sizeResource.String(),
},
}
return res, &cond, err
}
func createSnapshotName(pvName string, snapObj string) string {
name := pvName + "_" + snapObj + "_" + fmt.Sprintf("%d", time.Now().UnixNano())
return name
}
func (h *openEBSPlugin) SnapshotDelete(src *crdv1.VolumeSnapshotDataSource, pv *v1.PersistentVolume) error {
if src == nil || src.OpenEBSSnapshot == nil {
return fmt.Errorf("invalid VolumeSnapshotDataSource: %v", src)
}
casType := pv.Annotations["openebs.io/cas-type"]
if casType == "" {
casType = "jiva"
}
_, err := h.DeleteSnapshot(casType, pv.Name, src.OpenEBSSnapshot.SnapshotID, pv.Spec.ClaimRef.Namespace)
if err != nil {
glog.Errorf("failed to create snapshot for volume :%v, err: %v", pv.Name, err)
return err
}
glog.V(1).Infof("snapshot deleted :%v successfully", src.OpenEBSSnapshot.SnapshotID)
return err
}
func (h *openEBSPlugin) DescribeSnapshot(snapshotData *crdv1.VolumeSnapshotData) (snapConditions *[]crdv1.VolumeSnapshotCondition, isCompleted bool, err error) {
if snapshotData == nil || snapshotData.Spec.OpenEBSSnapshot == nil {
return nil, false, fmt.Errorf("failed to retrieve Snapshot spec")
}
snapshotID := snapshotData.Spec.OpenEBSSnapshot.SnapshotID
glog.V(1).Infof("received describe request on snapshot:%v", snapshotID)
// TODO implement snapshot-info based on snapshotID
resp, err := h.SnapshotInfo(snapshotData.Spec.PersistentVolumeRef.Name, snapshotID)
if err != nil {
glog.Errorf("failed to describe snapshot:%v", snapshotID)
}
glog.V(1).Infof("snapshot details:%v", string(resp))
if len(snapshotData.Status.Conditions) == 0 {
return nil, false, fmt.Errorf("No status condtions in VoluemSnapshotData for openebs snapshot type")
}
lastCondIdx := len(snapshotData.Status.Conditions) - 1
retCondType := crdv1.VolumeSnapshotConditionError
switch snapshotData.Status.Conditions[lastCondIdx].Type {
case crdv1.VolumeSnapshotDataConditionReady:
retCondType = crdv1.VolumeSnapshotConditionReady
case crdv1.VolumeSnapshotDataConditionPending:
retCondType = crdv1.VolumeSnapshotConditionPending
// Error out.
}
retCond := []crdv1.VolumeSnapshotCondition{
{
Status: snapshotData.Status.Conditions[lastCondIdx].Status,
Message: snapshotData.Status.Conditions[lastCondIdx].Message,
LastTransitionTime: snapshotData.Status.Conditions[lastCondIdx].LastTransitionTime,
Type: retCondType,
},
}
return &retCond, true, nil
}
// FindSnapshot finds a VolumeSnapshot by matching metadata
func (h *openEBSPlugin) FindSnapshot(tags *map[string]string) (*crdv1.VolumeSnapshotDataSource, *[]crdv1.VolumeSnapshotCondition, error) {
glog.Infof("FindSnapshot by tags: %#v", *tags)
// TODO: Implement FindSnapshot
return nil, nil, fmt.Errorf("Snapshot not found")
}
// SnapshotRestore restore to any created snapshot
func (h *openEBSPlugin) SnapshotRestore(snapshotData *crdv1.VolumeSnapshotData,
pvc *v1.PersistentVolumeClaim,
pvName string,
parameters map[string]string,
) (*v1.PersistentVolumeSource, map[string]string, error) {
if snapshotData == nil || snapshotData.Spec.OpenEBSSnapshot == nil {
return nil, nil, fmt.Errorf("Invalid Snapshot spec")
}
if pvc == nil {
return nil, nil, fmt.Errorf("Invalid PVC spec")
}
// restore snapshot to a PV
var newVolume v1alpha1.CASVolume
var openebsVol mvol_v1alpha1.CASVolume
volumeSpec, class := CreateCloneVolumeSpec(snapshotData, pvc, pvName)
err := openebsVol.CreateVolume(volumeSpec)
if err != nil {
glog.Errorf("Error creating volume: %v", err)
return nil, nil, err
}
err = openebsVol.ReadVolume(pvName, pvc.Namespace, class, &newVolume)
if err != nil {
glog.Errorf("Error getting volume details: %v", err)
return nil, nil, err
}
if err != nil {
glog.Errorf("snapshot :%v restore failed, err:%v", snapshotData.Spec.OpenEBSSnapshot.SnapshotID, err)
return nil, nil, fmt.Errorf("failed to restore %s, err: %v", snapshotData.Spec.OpenEBSSnapshot.SnapshotID, err)
}
glog.V(1).Infof("snapshot restored successfully to: %v", snapshotData.Spec.OpenEBSSnapshot.SnapshotID)
vollabels := make(map[string]string)
vollabels = provisioner.Setlink(vollabels, pvName)
vollabels[string(v1alpha1.CASTypeKey)] = newVolume.Spec.CasType
vollabels[string(v1alpha1.StorageClassKey)] = class
pv := &v1.PersistentVolumeSource{
ISCSI: &v1.ISCSIPersistentVolumeSource{
TargetPortal: newVolume.Spec.TargetPortal,
IQN: newVolume.Spec.Iqn,
Lun: 0,
FSType: newVolume.Spec.FSType,
ReadOnly: false,
},
}
return pv, vollabels, nil
}
// VolumeDelete deletes the persistent volume
func (h *openEBSPlugin) VolumeDelete(pv *v1.PersistentVolume) error {
if pv == nil || pv.Spec.ISCSI == nil {
return fmt.Errorf("invalid VolumeSnapshotDataSource: %v", pv)
}
var openebsVol mvol_v1alpha1.CASVolume
err := openebsVol.DeleteVolume(pv.Name, pv.Spec.ClaimRef.Namespace)
if err != nil {
glog.Errorf("Error while deleting volume: %v", err)
return err
}
return nil
}
func GetMayaService() error {
client, err := GetK8sClient()
if err != nil {
return err
}
var openebsVol mvol_v1alpha1.CASVolume
//Get maya-apiserver IP address from cluster
addr, err := openebsVol.GetMayaClusterIP(client)
if err != nil {
glog.Errorf("Error getting maya-apiserver IP Address: %v", err)
return err
}
mayaServiceURI := "http://" + addr + ":5656"
//Set maya-apiserver IP address along with default port
os.Setenv("MAPI_ADDR", mayaServiceURI)
return nil
}
// GetPersistentVolumeClass returns StorageClassName
func GetPersistentVolumeClass(volume *v1.PersistentVolume) string {
// Use label first
if class, found := volume.Labels[string(v1alpha1.StorageClassKey)]; found {
return class
}
return volume.Spec.StorageClassName
}
// GetPersistentClass returns StoragClassName
func GetStorageClass(pvName string) (string, error) {
client, err := GetK8sClient()
if err != nil {
return "", err
}
volume, err := client.CoreV1().PersistentVolumes().Get(pvName, metav1.GetOptions{})
if err != nil {
return "", err
}
glog.Infof("Source Volume is %#v", volume)
return GetPersistentVolumeClass(volume), nil
}
// GetNameAndNameSpaceFromSnapshotName retrieves the namespace and
// the short name of a snapshot from its full name, for exmaple
// "test-ns/snap1"
func GetNameAndNameSpaceFromSnapshotName(name string) (string, string, error) {
strs := strings.Split(name, "/")
if len(strs) != 2 {
return "", "", fmt.Errorf("invalid snapshot name")
}
return strs[0], strs[1], nil
}
// CreateVolumeSpec constructs the volumeSpec for volume create request
func CreateCloneVolumeSpec(snapshotData *crdv1.VolumeSnapshotData,
pvc *v1.PersistentVolumeClaim,
pvName string,
) (vol v1alpha1.CASVolume,
class string) {
// restore snapshot to a PV
// get the snaphot ID and source volume
snapshotID := snapshotData.Spec.OpenEBSSnapshot.SnapshotID
pvRefName := snapshotData.Spec.PersistentVolumeRef.Name
//pvRefNamespace := snapshotData.Spec.PersistentVolumeRef.Namespace
casVolume := v1alpha1.CASVolume{}
mapLabels := make(map[string]string)
// Get the source PV storage class name which will be passed
// to maya-apiserver to extract volume policy while restoring snapshot as
// new volume.
pvRefStorageClass, err := GetStorageClass(pvRefName)
if err != nil {
glog.Errorf("Error getting volume details: %v", err)
}
if len(pvRefStorageClass) == 0 {
glog.Errorf("Volume has no storage class specified")
} else {
mapLabels[string("openebs.io/storageclass")] = pvRefStorageClass
casVolume.Labels = mapLabels
}
glog.Infof("Using the Storage Class %s for dynamic provisioning", pvRefStorageClass)
// construct volumespec for volume create request.
// Enable volume clone: set clone as true, enables openebs volume to be created
// as a clone volume
volSize := pvc.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
casVolume.Spec.Capacity = volSize.String()
casVolume.Namespace = pvc.Namespace
casVolume.Labels[string(v1alpha1.NamespaceKey)] = pvc.Namespace
casVolume.Labels[string(v1alpha1.PersistentVolumeClaimKey)] = pvc.ObjectMeta.Name
casVolume.ObjectMeta.Name = pvName
casVolume.CloneSpec.SnapshotName = snapshotID
casVolume.CloneSpec.IsClone = true
casVolume.CloneSpec.SourceVolume = pvRefName
return casVolume, pvRefStorageClass
}