This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
/
local_node.go
247 lines (205 loc) · 7.34 KB
/
local_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
package node
import (
"os"
"path/filepath"
"code.cloudfoundry.org/goshims/filepathshim"
"code.cloudfoundry.org/goshims/osshim"
"code.cloudfoundry.org/lager"
"github.com/container-storage-interface/spec/lib/go/csi"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
const (
NODE_PLUGIN_ID = "org.cloudfoundry.code.local-node-plugin"
)
type LocalVolume struct {
csi.Volume
}
//go:generate counterfeiter -o nodefakes/fake_os_helper.go . OsHelper
type OsHelper interface {
Umask(mask int) (oldmask int)
Mount(srcPath string, targetPath string) error
IsMounted(targetPath string) (bool, error)
Unmount(targetPath string) error
}
type LocalNode struct {
filepath filepathshim.Filepath
os osshim.Os
logger lager.Logger
volumesRootDir string
osHelper OsHelper
nodeId string
}
func NewLocalNode(
os osshim.Os,
osHelper OsHelper,
filepath filepathshim.Filepath,
logger lager.Logger,
volumeRootDir string,
nodeId string,
) *LocalNode {
return &LocalNode{
os: os,
filepath: filepath,
logger: logger,
volumesRootDir: volumeRootDir,
osHelper: osHelper,
nodeId: nodeId,
}
}
func (ln *LocalNode) NodeStageVolume(ctx context.Context, in *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
return &csi.NodeStageVolumeResponse{}, nil
}
func (ln *LocalNode) NodeUnstageVolume(ctx context.Context, in *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (ln *LocalNode) NodePublishVolume(ctx context.Context, in *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
logger := ln.logger.Session("node-publish-volume")
logger.Info("start")
defer logger.Info("end")
var volId string = in.GetVolumeId()
if volId == "" {
errorDescription := "Volume ID is missing in request"
return nil, grpc.Errorf(codes.InvalidArgument, errorDescription)
}
volumePath := ln.volumePath(logger, volId)
logger.Info("volume-path", lager.Data{"value": volumePath})
vc := in.GetVolumeCapability()
if vc == nil {
errorDescription := "Volume capability is missing in request"
return nil, grpc.Errorf(codes.InvalidArgument, errorDescription)
}
if vc.GetMount() == nil {
errorDescription := "Volume mount capability is not specified"
return nil, grpc.Errorf(codes.InvalidArgument, errorDescription)
}
mountPath := in.GetTargetPath()
logger.Info("mounting-volume", lager.Data{"volume id": volId, "mount point": mountPath})
mounted, err := ln.osHelper.IsMounted(mountPath)
if err != nil {
logger.Error("volume-is-mounted-failed", err)
errorDescription := "Error checking if volume is mounted"
return nil, grpc.Errorf(codes.Internal, errorDescription)
}
logger.Info("volume-mounted", lager.Data{"value": mounted})
if mounted {
logger.Info("unmount", lager.Data{"mountPath": mountPath})
err := ln.osHelper.Unmount(mountPath)
if err != nil {
logger.Error("volume-unmount-failed", err)
errorDescription := "Error unmounting volume"
return nil, grpc.Errorf(codes.Internal, errorDescription)
}
}
err = ln.mount(logger, volumePath, mountPath)
if err != nil {
logger.Error("mount-volume-failed", err)
errorDescription := "Error mounting volume"
return nil, grpc.Errorf(codes.Internal, errorDescription)
}
logger.Info("volume-mounted", lager.Data{"volume id": volId, "volume path": volumePath, "mount path": mountPath})
return &csi.NodePublishVolumeResponse{}, nil
}
func (ln *LocalNode) NodeUnpublishVolume(ctx context.Context, in *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
var volId string = in.GetVolumeId()
if volId == "" {
errorDescription := "Volume ID is missing in request"
return nil, grpc.Errorf(codes.InvalidArgument, errorDescription)
}
mountPath := in.GetTargetPath()
if mountPath == "" {
errorDescription := "Mount path is missing in request"
return nil, grpc.Errorf(codes.InvalidArgument, errorDescription)
}
ln.logger.Info("unmount", lager.Data{"volume id": volId})
mounted, err := ln.osHelper.IsMounted(mountPath)
if err != nil {
ln.logger.Error("volume-is-mounted-failed", err)
errorDescription := "Error checking if volume is mounted"
return nil, grpc.Errorf(codes.Internal, errorDescription)
}
ln.logger.Info("volume-mounted", lager.Data{"value": mounted})
if !mounted {
return &csi.NodeUnpublishVolumeResponse{}, nil
}
ln.logger.Info("umount", lager.Data{"mountPath": mountPath})
err = ln.osHelper.Unmount(mountPath)
if err != nil {
ln.logger.Error("umount-volume-failed", err)
errorDescription := "Error unmounting volume"
return nil, grpc.Errorf(codes.Internal, errorDescription)
}
err = ln.os.Remove(mountPath)
if err != nil {
ln.logger.Error("remove-mount-path-failed", err)
errorDescription := "Error removing volume mount directory"
return nil, grpc.Errorf(codes.Internal, errorDescription)
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (ln *LocalNode) NodeGetVolumeStats(ctx context.Context, in *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return &csi.NodeGetVolumeStatsResponse{}, nil
}
func (ln *LocalNode) NodeGetCapabilities(ctx context.Context, in *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
return &csi.NodeGetCapabilitiesResponse{Capabilities: []*csi.NodeServiceCapability{}}, nil
}
func (ln *LocalNode) NodeGetInfo(ctx context.Context, in *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
return &csi.NodeGetInfoResponse{
NodeId: ln.nodeId,
}, nil
}
// Identity
//
func (ln *LocalNode) GetPluginCapabilities(ctx context.Context, in *csi.GetPluginCapabilitiesRequest) (*csi.GetPluginCapabilitiesResponse, error) {
return &csi.GetPluginCapabilitiesResponse{Capabilities: []*csi.PluginCapability{}}, nil
}
func (ln *LocalNode) GetPluginInfo(ctx context.Context, in *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) {
return &csi.GetPluginInfoResponse{
Name: NODE_PLUGIN_ID,
VendorVersion: "0.1.0",
}, nil
}
func (ln *LocalNode) Probe(ctx context.Context, in *csi.ProbeRequest) (*csi.ProbeResponse, error) {
return &csi.ProbeResponse{}, nil
}
func (ns *LocalNode) volumePath(logger lager.Logger, volumeId string) string {
volumesPathRoot := filepath.Join(ns.volumesRootDir, volumeId)
orig := ns.osHelper.Umask(000)
defer ns.osHelper.Umask(orig)
err := ns.os.MkdirAll(volumesPathRoot, os.ModePerm)
if err != nil {
panic(err)
}
return volumesPathRoot
}
func (ns *LocalNode) mount(logger lager.Logger, volumePath, mountPath string) error {
err := ns.createVolumesRootifNotExist(logger, mountPath)
if err != nil {
logger.Error("create-volumes-root", err)
return err
}
logger.Info("mount", lager.Data{"src": volumePath, "tgt": mountPath})
return ns.osHelper.Mount(volumePath, mountPath)
}
func (ns *LocalNode) exists(path string) (bool, error) {
_, err := ns.os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func (ns *LocalNode) createVolumesRootifNotExist(logger lager.Logger, mountPath string) error {
mountPath, err := ns.filepath.Abs(mountPath)
if err != nil {
logger.Error("abs-failed", err)
return err
}
logger.Debug("mkdir", lager.Data{"mountPath": mountPath})
orig := ns.osHelper.Umask(000)
defer ns.osHelper.Umask(orig)
return ns.os.MkdirAll(mountPath, os.ModePerm)
}