-
Notifications
You must be signed in to change notification settings - Fork 6
/
node.go
235 lines (212 loc) · 6.63 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
package csi
import (
"context"
"os"
"sync"
"github.com/oneconcern/datamon/pkg/storage/localfs"
"github.com/spf13/afero"
"github.com/oneconcern/datamon/pkg/core"
"github.com/oneconcern/datamon/pkg/storage"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/container-storage-interface/spec/lib/go/csi/v0"
)
type downloadedBundle struct {
repo string
bundleID string
path string
refCount int
mounted bool
fs *core.ReadOnlyFS
}
type nodeServer struct {
l *zap.Logger
meta storage.Store
blob storage.Store
localFS string
bundleMap map[string]*downloadedBundle
lock sync.Mutex
driver *Driver
}
func newNodeServer(driver *Driver) *nodeServer {
return &nodeServer{
driver: driver,
l: driver.config.Logger,
meta: driver.metadataStore,
blob: driver.blobStore,
localFS: driver.config.LocalFS,
bundleMap: make(map[string]*downloadedBundle),
lock: sync.Mutex{},
}
}
func (n *nodeServer) NodeStageVolume(context context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
repo, ok := req.VolumeAttributes["repo"]
if !ok {
n.l.Error("repo not set for volume", zap.String("req", req.String()))
return nil, status.Error(codes.InvalidArgument, "datamon repo not set, req="+req.String())
}
bundle, ok := req.VolumeAttributes["hash"]
if !ok {
n.l.Info("latest commit for main branch", zap.String("repo", repo), zap.String("req", req.String()))
}
n.l.Info("Stage volume done",
zap.String("volume", req.VolumeId),
zap.String("repo", repo),
zap.String("bundle", bundle))
return &csi.NodeStageVolumeResponse{}, nil
}
func (n *nodeServer) prepBundle(repo string, bundle string, volumeID string) error {
// Check if the bundle has been downloaded, if not download it.
_, ok := n.bundleMap[volumeID]
if !ok {
path := getPathToLocalFS(n.localFS, bundle, repo)
err := os.MkdirAll(path, 0777)
if err != nil {
n.l.Error("failed to create mount path", zap.Error(err))
return err
}
localFS := localfs.New(afero.NewBasePathFs(afero.NewOsFs(), path))
bd := core.NewBDescriptor()
b := core.New(bd,
core.Repo(repo),
core.BundleID(bundle),
core.BlobStore(n.blob),
core.ConsumableStore(localFS),
core.MetaStore(n.meta),
)
fs, err := core.NewReadOnlyFS(b, n.l)
if err != nil {
n.l.Error("failed to initialize bundle",
zap.String("repo", repo),
zap.String("bundle", bundle),
zap.Error(err))
return status.Error(codes.Internal, "failed to initialize repo:bundle "+repo+":"+bundle)
}
n.bundleMap[volumeID] = &downloadedBundle{
repo: repo,
bundleID: bundle,
path: "", // TODO
refCount: 1,
fs: fs,
mounted: false,
}
n.l.Info("volume ready to be published",
zap.String("volumeID", volumeID),
zap.String("repo", repo),
zap.String("bundle", bundle))
}
n.l.Info("Prep Bundle finished")
return nil
}
func (n *nodeServer) NodeUnstageVolume(context.Context, *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (n *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
n.lock.Lock()
defer n.lock.Unlock()
repo, ok := req.VolumeAttributes["repo"]
if !ok {
n.l.Error("repo not set for volume", zap.String("req", req.String()))
return nil, status.Error(codes.InvalidArgument, "repo not set for volume seen first time")
}
bundle, ok := req.VolumeAttributes["hash"]
if !ok {
n.l.Info("latest commit for main branch", zap.String("repo", repo), zap.String("req", req.String()))
}
_, ok = n.bundleMap[req.VolumeId]
if !ok {
err := n.prepBundle(repo, bundle, req.VolumeId)
if err != nil {
return nil, err
}
} else {
n.l.Info("mounting an existing bundle", zap.String("req", req.String()))
}
downloadedBundle, ok := n.bundleMap[req.VolumeId]
if !ok {
return nil, status.Error(codes.Internal, "fsMap missing entry: "+req.String())
}
if !downloadedBundle.mounted {
err := downloadedBundle.fs.MountReadOnly(req.TargetPath)
if err != nil {
return nil, err
}
downloadedBundle.mounted = true
}
downloadedBundle.refCount++
n.l.Info("Publish volume done",
zap.String("volume", req.VolumeId),
zap.String("id", downloadedBundle.bundleID),
zap.String("targetPath", req.TargetPath))
return &csi.NodePublishVolumeResponse{}, nil
}
func getPathToLocalFS(basePath string, repo string, bundle string) string {
path := basePath + "/" + repo + "/" + bundle
return path
}
func (n *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
n.lock.Lock()
defer n.lock.Unlock()
downloadedBundle, ok := n.bundleMap[req.VolumeId]
if ok {
n.l.Info("unMounting",
zap.String("volumeId", req.VolumeId),
zap.String("targetPath", req.TargetPath),
zap.String("repo", downloadedBundle.repo),
zap.String("bundleID", downloadedBundle.bundleID),
)
err := downloadedBundle.fs.Unmount(req.TargetPath)
if err != nil {
n.l.Error("Failed to unmount",
zap.Error(err),
)
return nil, err
}
// last mount
if downloadedBundle.refCount == 1 {
path := getPathToLocalFS(n.localFS, downloadedBundle.bundleID, downloadedBundle.repo)
err = os.RemoveAll(path)
if err != nil {
n.l.Error("failed to remove staging folder",
zap.String("path", path),
zap.String("repo", downloadedBundle.repo),
zap.String("bundle", downloadedBundle.bundleID),
)
return nil, err
}
delete(n.bundleMap, req.VolumeId)
} else {
downloadedBundle.refCount--
}
} else {
n.l.Warn("NodeUnpublishVolume for non mounted FS",
zap.String("volumeId", req.VolumeId),
zap.String("targetPath", req.TargetPath),
)
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}
//nolint: golint,stylecheck
func (n *nodeServer) NodeGetId(ctx context.Context, req *csi.NodeGetIdRequest) (*csi.NodeGetIdResponse, error) {
return &csi.NodeGetIdResponse{
NodeId: n.driver.config.NodeID,
}, nil
}
func (n *nodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
return &csi.NodeGetInfoResponse{
NodeId: n.driver.config.NodeID,
}, nil
}
func (n *nodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
cap := csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
},
},
}
return &csi.NodeGetCapabilitiesResponse{
Capabilities: []*csi.NodeServiceCapability{&cap},
}, nil
}