-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.go
300 lines (252 loc) · 8.58 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
298
299
300
package main
import (
"context"
"fmt"
"os"
"sync"
"syscall"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/onlineconf/onlineconf/updater/v3/updater"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type updaterInfo struct {
updater *updater.Updater
wg sync.WaitGroup
}
type nodeServer struct {
csi.UnimplementedNodeServer
id string
m sync.Mutex
state *state
updaters map[string]*updaterInfo
}
func newNodeServer(id string, stateFile string) (*nodeServer, error) {
state, err := readState(stateFile)
if err != nil {
return nil, fmt.Errorf("failed to open state file: %w", err)
}
return &nodeServer{
id: id,
state: state,
updaters: make(map[string]*updaterInfo),
}, nil
}
func (ns *nodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
return &csi.NodeGetInfoResponse{NodeId: ns.id}, nil
}
func (ns *nodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
return &csi.NodeGetCapabilitiesResponse{
Capabilities: []*csi.NodeServiceCapability{
{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
},
},
},
},
}, nil
}
func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
volumeId := req.GetVolumeId()
stage := req.GetStagingTargetPath()
if volumeId == "" {
return nil, status.Error(codes.InvalidArgument, "VolumeId missing in request")
}
if stage == "" {
return nil, status.Error(codes.InvalidArgument, "StagingTargetPath missing in request")
}
volCap, err := readVolumeCapability(req.GetVolumeCapability())
if err != nil {
return nil, err
}
volCtx, err := readVolumeContext(req.GetVolumeContext())
if err != nil {
return nil, err
}
ns.m.Lock()
defer ns.m.Unlock()
if us, ok := ns.state.Updaters[volumeId]; ok {
if us.DataDir == stage {
return &csi.NodeStageVolumeResponse{}, nil
} else {
return nil, status.Error(codes.InvalidArgument, "volume is already staged to another StagingTargetPath")
}
}
if _, ok := ns.updaters[stage]; ok {
return nil, status.Error(codes.InvalidArgument, "another volume is already staged to requested StagingTargetPath")
}
if err := os.MkdirAll(stage, 0750); err != nil {
log.Error().Err(err).Msg("failed to mkdir StagingTargetPath")
return nil, status.Error(codes.Internal, "failed to mkdir StagingTargetPath")
}
if volCap.chmod {
if err := os.Chmod(stage, volCap.mode); err != nil {
log.Error().Err(err).Msg("failed to chmod StagingTargetPath")
return nil, status.Error(codes.Internal, "failed to chmod StagingTargetPath")
}
}
state := updaterState{
DataDir: stage,
URI: volCtx.uri,
Username: req.GetSecrets()["username"],
Password: req.GetSecrets()["password"],
UpdateInterval: volCtx.updateInterval,
Variables: volCtx.vars,
}
if err := ns.runUpdater(volumeId, state, false); err != nil {
log.Error().Err(err).Msg("failed to run updater")
return nil, status.Error(codes.Internal, err.Error())
}
ns.state.Updaters[volumeId] = state
ns.state.save()
return &csi.NodeStageVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
volumeId := req.GetVolumeId()
stage := req.GetStagingTargetPath()
if volumeId == "" {
return nil, status.Error(codes.InvalidArgument, "VolumeId missing in request")
}
if stage == "" {
return nil, status.Error(codes.InvalidArgument, "StagingTargetPath missing in request")
}
ns.m.Lock()
defer ns.m.Unlock()
if us, ok := ns.state.Updaters[volumeId]; !(ok && us.DataDir == stage) {
return &csi.NodeUnstageVolumeResponse{}, nil
}
if ui := ns.updaters[stage]; ui != nil {
log.Info().Str("volume_id", volumeId).Msg("stopping updater")
ui.updater.Stop()
ui.wg.Wait()
}
if err := os.RemoveAll(stage); err != nil {
log.Error().Err(err).Msg("failed to remove StagingTargetDir")
return nil, status.Error(codes.Internal, "failed to remove StagingTargetPath")
}
delete(ns.state.Updaters, volumeId)
ns.state.save()
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
volumeId := req.GetVolumeId()
target := req.GetTargetPath()
stage := req.GetStagingTargetPath()
if volumeId == "" {
return nil, status.Error(codes.InvalidArgument, "VolumeId missing in request")
}
if target == "" {
return nil, status.Error(codes.InvalidArgument, "TargetPath missing in request")
}
if req.GetVolumeCapability() == nil {
return nil, status.Error(codes.InvalidArgument, "VolumeCapability missing in request")
}
if stage == "" {
return nil, status.Error(codes.FailedPrecondition, "StagingTargetPath missing in request")
}
ns.m.Lock()
defer ns.m.Unlock()
if us, ok := ns.state.Updaters[volumeId]; !ok {
return nil, status.Error(codes.NotFound, "unknown VolumeId")
} else if us.DataDir != stage {
return nil, status.Error(codes.InvalidArgument, "incompatible VolumeId and StagingTargetPath")
}
if mounts, err := readMountInfo(); err != nil {
log.Error().Err(err).Msg("failed to read mountinfo")
return nil, status.Error(codes.Internal, "failed to read mountinfo")
} else if mount := mounts.getByMountPoint(target); mount != nil {
if mounts.verifyMountSource(mount, stage) {
return &csi.NodePublishVolumeResponse{}, nil
} else {
return nil, status.Error(codes.InvalidArgument, "incompatible StagingTargetPath")
}
}
if err := os.MkdirAll(target, 0750); err != nil {
log.Error().Err(err).Msg("failed to mkdir")
return nil, status.Error(codes.Internal, "failed to mkdir TargetPath")
}
if err := syscall.Mount(stage, target, "", syscall.MS_MGC_VAL|syscall.MS_BIND|syscall.MS_RDONLY, ""); err != nil {
log.Error().Err(err).Msg("failed to mount")
return nil, status.Error(codes.Internal, "failed to mount")
}
if err := syscall.Mount(stage, target, "", syscall.MS_MGC_VAL|syscall.MS_REMOUNT|syscall.MS_BIND|syscall.MS_RDONLY, ""); err != nil {
log.Error().Err(err).Msg("failed to remount")
return nil, status.Error(codes.Internal, "failed to remount")
}
return &csi.NodePublishVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
volumeId := req.GetVolumeId()
target := req.GetTargetPath()
if volumeId == "" {
return nil, status.Error(codes.InvalidArgument, "VolumeId missing in request")
}
if target == "" {
return nil, status.Error(codes.InvalidArgument, "TargetPath missing in request")
}
ns.m.Lock()
defer ns.m.Unlock()
if err := syscall.Unmount(target, 0); err != nil && err != syscall.EINVAL && err != syscall.ENOENT {
log.Error().Err(err).Msg("failed to unmount")
return nil, status.Error(codes.Internal, "failed to unmount")
}
if err := os.RemoveAll(target); err != nil {
log.Error().Err(err).Msg("failed to remove TargetPath")
return nil, status.Error(codes.Internal, "failed to remove TargetPath")
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (ns *nodeServer) runUpdater(volumeId string, state updaterState, restore bool) error {
log.Info().Str("volume_id", volumeId).Dur("updateInterval", state.UpdateInterval).Msg("starting updater")
u := updater.NewUpdater(updater.UpdaterConfig{
Admin: updater.AdminConfig{
URI: state.URI,
Username: state.Username,
Password: state.Password,
},
UpdateInterval: state.UpdateInterval,
DataDir: state.DataDir,
Variables: state.Variables,
})
if err := u.Update(); err != nil {
if !restore {
return err
}
log.Error().Err(err).Str("volume_id", volumeId).Msg("update failed")
}
ui := &updaterInfo{updater: u}
ui.wg.Add(1)
ns.updaters[state.DataDir] = ui
log.Info().Str("volume_id", volumeId).Msg("updater started")
go func() {
u.Run()
log.Info().Str("volume_id", volumeId).Msg("updater stopped")
delete(ns.updaters, state.DataDir)
ui.wg.Done()
}()
return nil
}
func (ns *nodeServer) start() {
ns.m.Lock()
defer ns.m.Unlock()
for volumeId, state := range ns.state.Updaters {
_, err := os.Stat(state.DataDir)
if err != nil {
continue
}
ns.runUpdater(volumeId, state, true)
}
}
func (ns *nodeServer) stop() {
ns.m.Lock()
defer ns.m.Unlock()
for _, ui := range ns.updaters {
ui.updater.Stop()
}
for _, ui := range ns.updaters {
ui.wg.Wait()
}
}