/
csi_service_libstorage_idemp.go
190 lines (154 loc) · 4.62 KB
/
csi_service_libstorage_idemp.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
package libstorage
import (
"errors"
"fmt"
"github.com/codedellemc/gocsi/csi"
"github.com/codedellemc/gocsi/mount"
apitypes "github.com/codedellemc/rexray/libstorage/api/types"
apiutils "github.com/codedellemc/rexray/libstorage/api/utils"
)
var errMissingIDKeyPath = errors.New("missing id key path")
const resNotFound = "resource not found"
func isNotFoundErr(err error) bool {
return err.Error() == resNotFound
}
// GetVolumeName should return the name of the volume specified
// by the provided volume ID. If the volume does not exist then
// an empty string should be returned.
func (d *driver) GetVolumeName(id *csi.VolumeID) (string, error) {
idVal, ok := id.Values["id"]
if !ok {
return "", errMissingIDKeyPath
}
opts := &apitypes.VolumeInspectOpts{
Opts: apiutils.NewStore(),
}
vol, err := d.client.Storage().VolumeInspect(d.ctx, idVal, opts)
if err != nil {
// If the volume is not found then return an empty string
// for the name to indicate such.
if isNotFoundErr(err) {
return "", nil
}
return "", err
}
return vol.Name, nil
}
// GetVolumeInfo should return information about the volume
// specified by the provided volume name. If the volume does not
// exist then a nil value should be returned.
func (d *driver) GetVolumeInfo(name string) (*csi.VolumeInfo, error) {
td, ok := d.client.Storage().(apitypes.StorageDriverVolInspectByName)
if !ok {
return nil, fmt.Errorf(
"stor driver not by name: %T", d.client.Storage())
}
opts := &apitypes.VolumeInspectOpts{
Opts: apiutils.NewStore(),
}
vol, err := td.VolumeInspectByName(d.ctx, name, opts)
if err != nil {
// If the volume is not found then return nil for the
// volume info to indicate such.
if isNotFoundErr(err) {
return nil, nil
}
return nil, err
}
return toVolumeInfo(vol), nil
}
// IsControllerPublished should return publication info about
// the volume specified by the provided volume name or ID.
func (d *driver) IsControllerPublished(
id *csi.VolumeID) (*csi.PublishVolumeInfo, error) {
idVal, ok := id.Values["id"]
if !ok {
return nil, errMissingIDKeyPath
}
// Request only volumes that are attached.
opts := &apitypes.VolumeInspectOpts{
Attachments: apitypes.VolAttReqForInstance,
Opts: apiutils.NewStore(),
}
vol, err := d.client.Storage().VolumeInspect(d.ctx, idVal, opts)
if err != nil {
return nil, err
}
// If the volume is not attached to this node then do not
// indicate an error; just return nil to indicate
// the volume is not attached to this node.
if vol.AttachmentState != apitypes.VolumeAttached {
return nil, nil
}
d.attTokensRWL.RLock()
defer d.attTokensRWL.RUnlock()
return &csi.PublishVolumeInfo{
Values: map[string]string{
"token": d.attTokens[idVal],
"encrypted": fmt.Sprintf("%v", vol.Encrypted),
},
}, nil
}
// IsNodePublished should return a flag indicating whether or
// not the volume exists and is published on the current host.
func (d *driver) IsNodePublished(
id *csi.VolumeID,
pubInfo *csi.PublishVolumeInfo,
targetPath string) (bool, error) {
idVal, ok := id.Values["id"]
if !ok {
return false, errMissingIDKeyPath
}
// Request only volumes attached to this instance.
opts := &apitypes.VolumeInspectOpts{
Attachments: apitypes.VolAttReqWithDevMapForInstance,
Opts: apiutils.NewStore(),
}
vol, err := d.client.Storage().VolumeInspect(d.ctx, idVal, opts)
if err != nil {
return false, err
}
// If the volume is not attached to this node then do not
// indicate an error; just return false to indicate
// the volume is not attached to this node.
if vol.AttachmentState != apitypes.VolumeAttached {
return false, nil
}
// If the volume has no attachments then it's not possible to
// determine the node publication status.
if len(vol.Attachments) == 0 {
return false, nil
}
// Get the local mount table.
minfo, err := mount.GetMounts()
if err != nil {
return false, err
}
// Scan the mount table and get the path to which the device of
// the attached volume is mounted.
var (
volMountPath string
devPath = vol.Attachments[0].DeviceName
)
for _, mi := range minfo {
if mi.Device == devPath {
volMountPath = mi.Path
break
}
}
if volMountPath == "" {
return false, fmt.Errorf(
"unable to find attached device for volume: id=%s, dev=%s",
idVal, devPath)
}
// Scan the mount table info and if an entry's device matches
// the volume attachment's device, then it's mounted.
for _, mi := range minfo {
if mi.Source == volMountPath && mi.Path == targetPath {
return true, nil
}
}
// If no mount was discovered then indicate the volume is not
// published on this node.
return false, nil
}