-
Notifications
You must be signed in to change notification settings - Fork 4
/
node.go
297 lines (254 loc) · 10.9 KB
/
node.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
/**
*
* Copyright 2021- IBM Inc. All rights reserved
* SPDX-License-Identifier: Apache2.0
*
* 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 ibmcsidriver ...
package ibmcsidriver
import (
"fmt"
"os"
"time"
commonError "github.com/IBM/ibm-csi-common/pkg/messages"
"github.com/IBM/ibm-csi-common/pkg/metrics"
"github.com/IBM/ibm-csi-common/pkg/mountmanager"
"github.com/IBM/ibm-csi-common/pkg/utils"
nodeMetadata "github.com/IBM/ibmcloud-volume-file-vpc/pkg/metadata"
csi "github.com/container-storage-interface/spec/lib/go/csi"
"go.uber.org/zap"
"golang.org/x/net/context"
"golang.org/x/sys/unix"
"k8s.io/kubernetes/pkg/volume/util/fs"
mount "k8s.io/mount-utils"
)
// CSINodeServer ...
type CSINodeServer struct {
Driver *IBMCSIDriver
Mounter mountmanager.Mounter
Metadata nodeMetadata.NodeMetadata
Stats StatsUtils
// TODO: Only lock mutually exclusive calls and make locking more fine grained
mutex utils.LockStore
}
// StatsUtils ...
type StatsUtils interface {
FSInfo(path string) (int64, int64, int64, int64, int64, int64, error)
IsDevicePathNotExist(devicePath string) bool
}
// VolumeMountUtils ...
type VolumeMountUtils struct {
}
// VolumeStatUtils ...
type VolumeStatUtils struct {
}
// FSInfo ...
func (su *VolumeStatUtils) FSInfo(path string) (int64, int64, int64, int64, int64, int64, error) {
return fs.Info(path)
}
const (
// default file system type to be used when it is not provided
defaultFsType = "nfs"
)
var _ csi.NodeServer = &CSINodeServer{}
// NodePublishVolume ...
func (csiNS *CSINodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
publishContext := req.GetPublishContext()
controlleRequestID := publishContext[PublishInfoRequestID]
ctxLogger, requestID := utils.GetContextLoggerWithRequestID(ctx, false, &controlleRequestID)
ctxLogger.Info("CSINodeServer-NodePublishVolume...", zap.Reflect("Request", *req))
defer metrics.UpdateDurationFromStart(ctxLogger, "NodePublishVolume", time.Now())
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, commonError.GetCSIError(ctxLogger, commonError.EmptyVolumeID, requestID, nil)
}
source := req.GetVolumeContext()[NFSServerPath]
if len(source) == 0 {
return nil, commonError.GetCSIError(ctxLogger, commonError.NoStagingTargetPath, requestID, nil)
}
target := req.GetTargetPath()
if len(target) == 0 {
return nil, commonError.GetCSIError(ctxLogger, commonError.NoTargetPath, requestID, nil)
}
volumeCapability := req.GetVolumeCapability()
if volumeCapability == nil {
return nil, commonError.GetCSIError(ctxLogger, commonError.NoVolumeCapabilities, requestID, nil)
}
volumeCapabilities := []*csi.VolumeCapability{volumeCapability}
// Validate volume capabilities, are all capabilities supported by driver or not
if !areVolumeCapabilitiesSupported(volumeCapabilities, csiNS.Driver.vcap) {
return nil, commonError.GetCSIError(ctxLogger, commonError.VolumeCapabilitiesNotSupported, requestID, nil)
}
// Check if targetPath is already mounted. If it already moounted return OK
notMounted, err := csiNS.Mounter.IsLikelyNotMountPoint(target)
if err != nil && !os.IsNotExist(err) {
//Error other than PathNotExists
ctxLogger.Error(fmt.Sprintf("Can not validate target mount point: %s %v", target, err))
return nil, commonError.GetCSIError(ctxLogger, commonError.MountPointValidateError, requestID, err, target)
}
// Its OK if IsLikelyNotMountPoint returns PathNotExists error
if !notMounted {
// The target Path is already mounted, Retrun OK
/* TODO
1) Target Path MUST be the vol referenced by vol ID
2) Check volume capability matches for ALREADY_EXISTS
3) Readonly MUST match
*/
return &csi.NodePublishVolumeResponse{}, nil
}
mnt := volumeCapability.GetMount()
options := mnt.MountFlags
// find FS type
fsType := defaultFsType
var nodePublishResponse *csi.NodePublishVolumeResponse
var mountErr error
//Lets try to put lock at targetPath level. If we are processing same target path lets wait for other to finish.
//This will not hold other volumes and target path processing.
csiNS.mutex.Lock(target)
defer csiNS.mutex.Unlock(target)
nodePublishResponse, mountErr = csiNS.processMount(ctxLogger, requestID, source, target, fsType, options)
ctxLogger.Info("CSINodeServer-NodePublishVolume response...", zap.Reflect("Response", nodePublishResponse), zap.Error(mountErr))
return nodePublishResponse, mountErr
}
// NodeUnpublishVolume ...
func (csiNS *CSINodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
ctxLogger, requestID := utils.GetContextLogger(ctx, false)
ctxLogger.Info("CSINodeServer-NodeUnpublishVolume...", zap.Reflect("Request", *req))
defer metrics.UpdateDurationFromStart(ctxLogger, "NodeUnpublishVolume", time.Now())
// Validate Arguments
targetPath := req.GetTargetPath()
volID := req.GetVolumeId()
if len(volID) == 0 {
return nil, commonError.GetCSIError(ctxLogger, commonError.EmptyVolumeID, requestID, nil)
}
if len(targetPath) == 0 {
return nil, commonError.GetCSIError(ctxLogger, commonError.NoTargetPath, requestID, nil)
}
//Lets try to put lock at targetPath level. If we are processing same target path lets wait for other to finish.
//This will not hold other volumes and target path processing.
csiNS.mutex.Lock(targetPath)
defer csiNS.mutex.Unlock(targetPath)
ctxLogger.Info("Unmounting target path", zap.String("targetPath", targetPath))
err := mount.CleanupMountPoint(targetPath, csiNS.Mounter, false /* bind mount */)
if err != nil {
return nil, commonError.GetCSIError(ctxLogger, commonError.UnmountFailed, requestID, err, targetPath)
}
nodeUnpublishVolumeResponse := &csi.NodeUnpublishVolumeResponse{}
ctxLogger.Info("Successfully unmounted target path", zap.String("targetPath", targetPath), zap.Error(err))
return nodeUnpublishVolumeResponse, err
}
// NodeStageVolume ...
func (csiNS *CSINodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
ctxLogger, requestID := utils.GetContextLogger(ctx, false)
ctxLogger.Info("CSINodeServer-NodeStageVolume", zap.Reflect("Request", *req))
return nil, commonError.GetCSIError(ctxLogger, commonError.MethodUnsupported, requestID, nil, "NodeStageVolume")
}
// NodeUnstageVolume ...
func (csiNS *CSINodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
ctxLogger, requestID := utils.GetContextLogger(ctx, false)
ctxLogger.Info("CSINodeServer-NodeUnstageVolume", zap.Reflect("Request", *req))
return nil, commonError.GetCSIError(ctxLogger, commonError.MethodUnsupported, requestID, nil, "NodeUnstageVolume")
}
// NodeGetCapabilities ...
func (csiNS *CSINodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
ctxLogger, _ := utils.GetContextLogger(ctx, false)
ctxLogger.Info("CSINodeServer-NodeGetCapabilities... ", zap.Reflect("Request", *req))
return &csi.NodeGetCapabilitiesResponse{
Capabilities: csiNS.Driver.nscap,
}, nil
}
// NodeGetInfo ...
func (csiNS *CSINodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
ctxLogger, requestID := utils.GetContextLogger(ctx, false)
ctxLogger.Info("CSINodeServer-NodeGetInfo... ", zap.Reflect("Request", *req))
// Check if node metadata service initialized properly
if csiNS.Metadata == nil { //nolint
metadata, err := nodeMetadata.NewNodeMetadata(os.Getenv("KUBE_NODE_NAME"), ctxLogger)
if err != nil {
ctxLogger.Error("Failed to initialize node metadata", zap.Error(err))
return nil, commonError.GetCSIError(ctxLogger, commonError.NodeMetadataInitFailed, requestID, err)
}
csiNS.Metadata = metadata
}
top := &csi.Topology{
Segments: map[string]string{
utils.NodeRegionLabel: csiNS.Metadata.GetRegion(),
utils.NodeZoneLabel: csiNS.Metadata.GetZone(),
},
}
resp := &csi.NodeGetInfoResponse{
NodeId: csiNS.Metadata.GetWorkerID(),
AccessibleTopology: top,
}
ctxLogger.Info("NodeGetInfoResponse", zap.Reflect("NodeGetInfoResponse", resp))
return resp, nil
}
// NodeGetVolumeStats ...
func (csiNS *CSINodeServer) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
var resp *csi.NodeGetVolumeStatsResponse
ctxLogger, requestID := utils.GetContextLogger(ctx, false)
ctxLogger.Info("CSINodeServer-NodeGetVolumeStats... ", zap.Reflect("Request", req))
defer metrics.UpdateDurationFromStart(ctxLogger, "NodeGetVolumeStats", time.Now())
if req == nil || req.VolumeId == "" { //nolint
return nil, commonError.GetCSIError(ctxLogger, commonError.EmptyVolumeID, requestID, nil)
}
if req.VolumePath == "" {
return nil, commonError.GetCSIError(ctxLogger, commonError.EmptyVolumePath, requestID, nil)
}
volumePath := req.VolumePath
// Return if path does not exist
if csiNS.Stats.IsDevicePathNotExist(volumePath) {
return nil, commonError.GetCSIError(ctxLogger, commonError.DevicePathNotExists, requestID, nil, volumePath, req.VolumeId)
}
// else get the file system stats
available, capacity, usage, inodes, inodesFree, inodesUsed, err := csiNS.Stats.FSInfo(volumePath)
if err != nil {
return nil, commonError.GetCSIError(ctxLogger, commonError.GetFSInfoFailed, requestID, err)
}
resp = &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
{
Available: available,
Total: capacity,
Used: usage,
Unit: csi.VolumeUsage_BYTES,
},
{
Available: inodesFree,
Total: inodes,
Used: inodesUsed,
Unit: csi.VolumeUsage_INODES,
},
},
}
ctxLogger.Info("Response for Volume stats", zap.Reflect("Response", resp))
return resp, nil
}
// NodeExpandVolume ...
func (csiNS *CSINodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
ctxLogger, requestID := utils.GetContextLogger(ctx, false)
ctxLogger.Info("CSINodeServer-NodeExpandVolume", zap.Reflect("Request", *req))
return nil, commonError.GetCSIError(ctxLogger, commonError.MethodUnsupported, requestID, nil, "NodeExpandVolume")
}
// IsDevicePathNotExist ...
func (su *VolumeStatUtils) IsDevicePathNotExist(devicePath string) bool {
var stat unix.Stat_t
err := unix.Stat(devicePath, &stat)
if err != nil {
if os.IsNotExist(err) {
return true
}
}
return false
}