-
Notifications
You must be signed in to change notification settings - Fork 0
/
linstor.go
422 lines (369 loc) · 10.8 KB
/
linstor.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package linstor
import (
"context"
"fmt"
"math/rand"
"time"
lstor "github.com/LINBIT/golinstor"
lclient "github.com/LINBIT/golinstor/client"
snapv1 "github.com/kubernetes-incubator/external-storage/snapshot/pkg/apis/crd/v1"
snapshotVolume "github.com/kubernetes-incubator/external-storage/snapshot/pkg/volume"
storkvolume "github.com/libopenstorage/stork/drivers/volume"
storkapi "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1"
"github.com/libopenstorage/stork/pkg/errors"
"github.com/portworx/sched-ops/k8s/core"
"github.com/portworx/sched-ops/k8s/storage"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
k8shelper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
)
const (
// pvcProvisionerAnnotation is the annotation on PVC which has the provisioner name
pvcProvisionerAnnotation = "volume.beta.kubernetes.io/storage-provisioner"
// pvProvisionedByAnnotation is the annotation on PV which has the provisioner name
pvProvisionedByAnnotation = "pv.kubernetes.io/provisioned-by"
provisionerName = "linstor.csi.linbit.com"
rackLabelKey = "linstor/rack"
)
type linstor struct {
cli *lclient.Client
store cache.Store
stopChannel chan struct{}
storkvolume.ClusterPairNotSupported
storkvolume.MigrationNotSupported
storkvolume.GroupSnapshotNotSupported
storkvolume.ClusterDomainsNotSupported
storkvolume.BackupRestoreNotSupported
storkvolume.CloneNotSupported
storkvolume.SnapshotRestoreNotSupported
}
func (l *linstor) linstorClient() (*lclient.Client, error) {
if l.cli == nil {
err := l.Init(nil)
if err != nil {
return nil, fmt.Errorf("failed to initialize driver: %w", err)
}
}
return l.cli, nil
}
func (l *linstor) Init(_ interface{}) error {
// Configuration of linstor client happens via environment variables:
// * LS_CONTROLLERS
// * LS_USERNAME
// * LS_PASSWORD
// * LS_USER_CERTIFICATE
// * LS_USER_KEY
// * LS_ROOT_CA
client, err := lclient.NewClient(
lclient.Log(logrus.StandardLogger()),
)
if err != nil {
return fmt.Errorf("error creating linstor client: %w", err)
}
l.cli = client
l.stopChannel = make(chan struct{})
return l.startNodeCache()
}
func (l *linstor) startNodeCache() error {
resyncPeriod := 30 * time.Second
config, err := rest.InClusterConfig()
if err != nil {
return fmt.Errorf("error getting cluster config: %v", err)
}
k8sClient, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("error getting client, %v", err)
}
restClient := k8sClient.CoreV1().RESTClient()
watchlist := cache.NewListWatchFromClient(restClient, "nodes", v1.NamespaceAll, fields.Everything())
lw := &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return watchlist.List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return watchlist.Watch(options)
},
}
store, controller := cache.NewInformer(lw, &v1.Node{}, resyncPeriod,
cache.ResourceEventHandlerFuncs{},
)
l.store = store
go controller.Run(l.stopChannel)
return nil
}
func (l *linstor) String() string {
return storkvolume.LinstorDriverName
}
func (l *linstor) InspectNode(id string) (*storkvolume.NodeInfo, error) {
return nil, &errors.ErrNotSupported{}
}
func contains(a []string, e string) bool {
for _, x := range a {
if x == e {
return true
}
}
return false
}
func (l *linstor) InspectVolume(volumeID string) (*storkvolume.Info, error) {
cli, err := l.linstorClient()
if err != nil {
return nil, err
}
rd, err := cli.ResourceDefinitions.Get(context.TODO(), volumeID)
if err != nil {
return nil, fmt.Errorf("failed to get resource definition: %w", err)
}
resources, err := cli.Resources.GetResourceView(context.TODO(), &lclient.ListOpts{
Resource: []string{volumeID},
})
if err != nil {
return nil, fmt.Errorf("failed to get resources: %w", err)
}
var nodes []string
for _, r := range resources {
state := r.Volumes[0].State.DiskState
if state != "UpToDate" {
logrus.Debugf("Resource %s is not UpToDate on node %s, skipping (is %s)",
r.Name, r.NodeName, state)
continue
}
if contains(r.Flags, lstor.FlagDiskless) {
// skip diskless deployments
logrus.Debugf("Resource %s is diskless on node %s, skipping",
r.Name, r.NodeName)
continue
}
nodes = append(nodes, r.NodeName)
}
vd, err := cli.ResourceDefinitions.GetVolumeDefinition(context.TODO(), volumeID, 0)
if err != nil {
return nil, fmt.Errorf("failed to get volume defintion: %w", err)
}
return &storkvolume.Info{
VolumeID: rd.Name,
VolumeName: rd.Name,
DataNodes: nodes,
Size: vd.SizeKib / 1048576, // KiB to GiB
}, nil
}
func (l *linstor) getNodeLabels(nodeInfo *storkvolume.NodeInfo) (map[string]string, error) {
obj, exists, err := l.store.GetByKey(nodeInfo.SchedulerID)
if err != nil {
return nil, err
} else if !exists {
obj, exists, err = l.store.GetByKey(nodeInfo.StorageID)
if err != nil {
return nil, err
} else if !exists {
obj, exists, err = l.store.GetByKey(nodeInfo.Hostname)
if err != nil {
return nil, err
} else if !exists {
return nil, fmt.Errorf("node %v/%v/%v not found in cache", nodeInfo.StorageID, nodeInfo.SchedulerID, nodeInfo.Hostname)
}
}
}
node := obj.(*v1.Node)
return node.Labels, nil
}
func (l *linstor) mapLinstorStatus(n lclient.Node) storkvolume.NodeStatus {
switch n.ConnectionStatus {
case "ONLINE":
return storkvolume.NodeOnline
default:
return storkvolume.NodeOffline
}
}
func (l *linstor) GetNodes() ([]*storkvolume.NodeInfo, error) {
cli, err := l.linstorClient()
if err != nil {
return nil, err
}
nodes, err := cli.Nodes.GetAll(context.TODO())
if err != nil {
return nil, fmt.Errorf("failed to get linstor nodes: %w", err)
}
var infos []*storkvolume.NodeInfo
for _, n := range nodes {
var ips []string
for _, iface := range n.NetInterfaces {
ips = append(ips, iface.Address)
}
newInfo := &storkvolume.NodeInfo{
StorageID: n.Name,
Hostname: n.Name,
IPs: ips,
Status: l.mapLinstorStatus(n),
}
labels, err := l.getNodeLabels(newInfo)
if err == nil {
if rack, ok := labels[rackLabelKey]; ok {
newInfo.Rack = rack
}
if zone, ok := labels[v1.LabelZoneFailureDomain]; ok {
newInfo.Zone = zone
}
if region, ok := labels[v1.LabelZoneRegion]; ok {
newInfo.Region = region
}
}
infos = append(infos, newInfo)
}
return infos, nil
}
func (l *linstor) GetPodVolumes(podSpec *v1.PodSpec, namespace string) ([]*storkvolume.Info, error) {
var volumes []*storkvolume.Info
for _, volume := range podSpec.Volumes {
volumeName := ""
if volume.PersistentVolumeClaim != nil {
pvc, err := core.Instance().GetPersistentVolumeClaim(
volume.PersistentVolumeClaim.ClaimName,
namespace)
if err != nil {
return nil, err
}
if !l.OwnsPVC(core.Instance(), pvc) {
continue
}
if pvc.Status.Phase == v1.ClaimPending {
return nil, &storkvolume.ErrPVCPending{
Name: volume.PersistentVolumeClaim.ClaimName,
}
}
volumeName = pvc.Spec.VolumeName
}
if volumeName != "" {
volumeInfo, err := l.InspectVolume(volumeName)
if err != nil {
// If the ispect volume fails return with atleast some info
volumeInfo = &storkvolume.Info{
VolumeName: volumeName,
}
}
volumes = append(volumes, volumeInfo)
}
}
return volumes, nil
}
func (l *linstor) GetVolumeClaimTemplates(templates []v1.PersistentVolumeClaim) ([]v1.PersistentVolumeClaim, error) {
var linstorTemplates []v1.PersistentVolumeClaim
for _, t := range templates {
if l.OwnsPVC(core.Instance(), &t) {
linstorTemplates = append(linstorTemplates, t)
}
}
return linstorTemplates, nil
}
func (l *linstor) OwnsPVCForBackup(coreOps core.Ops, pvc *v1.PersistentVolumeClaim, cmBackupType string, crBackupType string) bool {
return l.OwnsPVC(coreOps, pvc)
}
func (l *linstor) OwnsPVC(coreOps core.Ops, pvc *v1.PersistentVolumeClaim) bool {
provisioner := ""
// Check for the provisioner in the PVC annotation. If not populated
// try getting the provisioner from the Storage class.
if val, ok := pvc.Annotations[pvcProvisionerAnnotation]; ok {
provisioner = val
} else {
storageClassName := k8shelper.GetPersistentVolumeClaimClass(pvc)
if storageClassName != "" {
storageClass, err := storage.Instance().GetStorageClass(storageClassName)
if err == nil {
provisioner = storageClass.Provisioner
} else {
logrus.Warnf("Error getting storageclass %v for pvc %v: %v", storageClassName, pvc.Name, err)
}
}
}
if provisioner == "" {
// Try to get info from the PV since storage class could be deleted
pv, err := coreOps.GetPersistentVolume(pvc.Spec.VolumeName)
if err != nil {
logrus.Warnf("Error getting pv %v for pvc %v: %v", pvc.Spec.VolumeName, pvc.Name, err)
return false
}
return l.OwnsPV(pv)
}
if provisioner == provisionerName {
return true
}
return false
}
func (l *linstor) OwnsPV(pv *v1.PersistentVolume) bool {
provisioner := ""
// Check the annotation in the PV for the provisioner
if val, ok := pv.Annotations[pvProvisionedByAnnotation]; ok {
provisioner = val
}
if provisioner == provisionerName {
return true
}
return false
}
func (l *linstor) GetSnapshotPlugin() snapshotVolume.Plugin {
return nil
}
func (l *linstor) GetSnapshotType(snap *snapv1.VolumeSnapshot) (string, error) {
return "", &errors.ErrNotImplemented{}
}
func (l *linstor) Stop() error {
close(l.stopChannel)
return nil
}
func (l *linstor) UpdateMigratedPersistentVolumeSpec(
pv *v1.PersistentVolume,
vInfo *storkapi.ApplicationRestoreVolumeInfo,
) (*v1.PersistentVolume, error) {
return pv, nil
}
func randString(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func (l *linstor) GetClusterID() (string, error) {
cli, err := l.linstorClient()
if err != nil {
return "", err
}
props, err := cli.Controller.GetProps(context.TODO())
if err != nil {
return "", fmt.Errorf("failed to query linstor controller properties: %w", err)
}
var id string
for k, v := range props {
if k == "Aux/StorkClusterId" {
id = v
break
}
}
if id == "" {
id = randString(16)
logrus.Debugf("linstor: no cluster ID found, generating new (%s)", id)
err := cli.Controller.Modify(context.TODO(), lclient.GenericPropsModify{
OverrideProps: lclient.OverrideProps{
"Aux/StorkClusterId": id,
},
})
if err != nil {
return "", fmt.Errorf("failed to set cluster ID property: %w", err)
}
}
return id, nil
}
func init() {
l := &linstor{}
if err := storkvolume.Register(storkvolume.LinstorDriverName, l); err != nil {
logrus.Panicf("Error registering linstor volume driver: %v", err)
}
}