/
node_server.go
247 lines (223 loc) · 8.88 KB
/
node_server.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
/*
Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
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 csidriver
import (
"fmt"
"os"
"path/filepath"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/drivers/pkg/csi-common"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/PaddlePaddle/PaddleFlow/pkg/common/schema"
"github.com/PaddlePaddle/PaddleFlow/pkg/fs/client/ufs"
"github.com/PaddlePaddle/PaddleFlow/pkg/fs/common"
"github.com/PaddlePaddle/PaddleFlow/pkg/fs/csiplugin/csiconfig"
"github.com/PaddlePaddle/PaddleFlow/pkg/fs/csiplugin/mount"
"github.com/PaddlePaddle/PaddleFlow/pkg/fs/utils"
)
type nodeServer struct {
nodeId string
*csicommon.DefaultNodeServer
}
func (ns *nodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
nscap := &csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
},
},
}
return &csi.NodeGetCapabilitiesResponse{Capabilities: []*csi.NodeServiceCapability{nscap}}, nil
}
func (ns *nodeServer) NodePublishVolume(ctx context.Context,
req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
log.Infof("Node publish volume request [%+v]", *req)
targetPath := req.GetTargetPath()
if exist, err := utils.Exist(targetPath); err != nil {
log.Errorf("check path[%s] exist failed: %v", targetPath, err)
return &csi.NodePublishVolumeResponse{}, status.Error(codes.Internal, err.Error())
} else if !exist {
if err := os.MkdirAll(targetPath, 0750); err != nil {
log.Errorf("create targetPath[%s] failed: %v", targetPath, err)
return nil, status.Error(codes.Internal, err.Error())
}
}
volumeID := req.VolumeId
volumeContext := req.GetVolumeContext()
csiconfig.ClusterID = volumeContext[schema.PFSClusterID]
k8sClient, err := utils.GetK8sClient()
if err != nil {
log.Errorf("get k8s client failed: %v", err)
return nil, err
}
server := csiconfig.PaddleFlowServerAddress
log.Infof("server %s", server)
if server == "" {
server = volumeContext[schema.PFSServer]
}
mountInfo, err := mount.ConstructMountInfo(server, volumeContext[schema.PFSInfo], volumeContext[schema.PFSCache],
targetPath, k8sClient, req.GetReadonly() || req.VolumeCapability.AccessMode.GetMode() == csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY)
if err != nil {
log.Errorf("ConstructMountInfo err: %v", err)
return nil, status.Error(codes.Internal, err.Error())
}
log.Infof("Node publish mountInfo [%+v]", mountInfo)
if err = mountVolume(volumeID, mountInfo); err != nil {
log.Errorf("mount filesystem[%s] failed: %v", volumeContext[schema.PFSID], err)
return &csi.NodePublishVolumeResponse{}, status.Error(codes.Internal, err.Error())
}
return &csi.NodePublishVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context,
req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
targetPath := req.GetTargetPath()
mountInfo := mount.Info{
TargetPath: targetPath,
}
if err := mount.PodUnmount(req.VolumeId, mountInfo); err != nil {
log.Errorf("[UMount]: volumeID[%s] and targetPath[%s] with err: %s", req.VolumeId, mountInfo.TargetPath, err.Error())
return nil, err
}
pathsToCleanup := []string{targetPath}
// clean source path for process mount
// pod mount no source path to clean, and is ignored in the func
sourcePath := utils.GetSourceMountPath(filepath.Dir(targetPath))
pathsToCleanup = append(pathsToCleanup, sourcePath)
if err := utils.CleanUpMountPoints(pathsToCleanup); err != nil {
log.Errorf("NodeUnpublishVolume: cleanup mount points err: %v", err)
return nil, status.Error(codes.Internal, err.Error())
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (ns *nodeServer) NodeStageVolume(ctx context.Context,
req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
log.Debugf("start to stage volume[%s], do nothing...", req.GetVolumeId())
return &csi.NodeStageVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnstageVolume(ctx context.Context,
req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
log.Debugf("start to unstage volume[%s], do nothing...", req.GetVolumeId())
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (ns *nodeServer) NodeExpandVolume(ctx context.Context,
req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "NodeExpandVolume is not implemented")
}
func mountVolume(volumeID string, mountInfo mount.Info) error {
log.Infof("mountVolume: indepedentMp:%t, readOnly:%t", mountInfo.FS.IndependentMountProcess, mountInfo.ReadOnly)
if !mountInfo.FS.IndependentMountProcess && mountInfo.FS.Type != common.GlusterFSType && mountInfo.FS.Type != common.CFSType && mountInfo.FS.Type != common.AFSType {
// business pods use a separate source path
if err := mount.PFSMount(volumeID, mountInfo); err != nil {
log.Errorf("MountThroughPod err: %v", err)
return err
}
} else {
// mount source path
if err := os.MkdirAll(mountInfo.SourcePath, 0750); err != nil {
err := fmt.Errorf("process mount [%s] failed when makedir of source path %s, err: %v",
mountInfo.FS.ID, mountInfo.SourcePath, err)
log.Error(err.Error())
return err
}
if mountInfo.FS.Type == common.CFSType {
properties := make(map[string]interface{})
for k, v := range mountInfo.FS.PropertiesMap {
properties[k] = v
}
properties[common.Type] = common.CFSType
properties[common.SubPath] = mountInfo.FS.SubPath
properties[common.Address] = mountInfo.FS.ServerAddress
// mkdir not exist dir for cfs
_, err := ufs.NewLocalMountFileSystem(properties)
if err != nil {
log.Errorf("NewLocalMountFileSystem err: %v", err)
return err
}
}
log.Infof("mount with cmd %s and args %v", mountInfo.Cmd, mountInfo.Args)
output, err := utils.ExecCmdWithTimeout(mountInfo.Cmd, mountInfo.Args)
if err != nil {
log.Errorf("exec mount failed: [%v], output[%v]", err, string(output))
return err
}
}
// bind to target path
if err := bindMountVolume(mountInfo.SourcePath, mountInfo.TargetPath, mountInfo.ReadOnly); err != nil {
err := fmt.Errorf("bindMountVolume[%s] of fs[%s] failed when bind from %s to %s, err: %v",
volumeID, mountInfo.FS.ID, mountInfo.SourcePath, mountInfo.TargetPath, err)
log.Error(err.Error())
return err
}
return nil
}
// bindMountVolume bind source path to target path
func bindMountVolume(sourcePath, mountPath string, readOnly bool) error {
log.Infof("bindMountVolume source[%s] target[%s]", sourcePath, mountPath)
if err := os.MkdirAll(mountPath, 0750); err != nil {
log.Errorf("mkdir volume bindMountPath[%s] failed: %v", mountPath, err)
return err
}
// check bind source
isMountPoint, err := utils.IsMountPoint(sourcePath)
if err != nil {
log.Errorf("bind source %s has err :%v. unmounting ...", sourcePath, err)
err := utils.ManualUnmount(sourcePath)
if err != nil {
log.Errorf("unmount mountPoint[%s] failed: %v", sourcePath, err)
return err
}
log.Infof("bind source %s unmounted", sourcePath)
// check again
isMountPoint, err = utils.IsMountPoint(sourcePath)
if err != nil {
err := fmt.Errorf("unmount bind source %s failed: %v", sourcePath, err)
log.Errorf(err.Error())
return err
}
}
if !isMountPoint {
err := fmt.Errorf("bindMountVolume failed as sourcePath %s is not a valid mountpoint. Please check fuse pod", sourcePath)
log.Errorf(err.Error())
return err
}
// check bind target
isMountPoint, err = utils.IsMountPoint(mountPath)
if err != nil {
log.Errorf("bind target %s has err :%v. unmounting ...", mountPath, err)
err := utils.ManualUnmount(mountPath)
if err != nil {
log.Errorf("unmount mountPoint[%s] failed: %v", mountPath, err)
return err
}
// check again
isMountPoint, err = utils.IsMountPoint(mountPath)
if err != nil {
err := fmt.Errorf("unmount bind target %s failed: %v", mountPath, err)
log.Errorf(err.Error())
return err
}
log.Infof("bind target %s unmounted", mountPath)
}
if !isMountPoint {
output, err := utils.ExecMountBind(sourcePath, mountPath, readOnly)
if err != nil {
log.Errorf("exec mount bind failed: %v, output[%s]", err, string(output))
return err
}
}
log.Infof("bindMountVolume from [%v] to [%v]", sourcePath, mountPath)
return nil
}