Skip to content

Commit 19c77f5

Browse files
authored
Merge pull request #95 from gadget-inc/cached_into_shared_dir
Cached into shared staging dir
2 parents b12b353 + 33955f1 commit 19c77f5

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

pkg/api/cached.go

+8-26
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package api
22

33
import (
44
"context"
5-
"crypto/rand"
6-
"encoding/base64"
75
"errors"
86
"fmt"
97
"math"
@@ -41,9 +39,7 @@ type Cached struct {
4139
Client *client.Client
4240
StagingPath string
4341

44-
// the current directory holding a fully formed downloaded cache
45-
currentDir string
46-
// the current version of the cache on disk at currentDir
42+
// the current version of the cache on disk
4743
currentVersion int64
4844
}
4945

@@ -67,23 +63,18 @@ func (c *Cached) PopulateDiskCache(ctx context.Context, req *pb.PopulateDiskCach
6763
return &pb.PopulateDiskCacheResponse{Version: version}, nil
6864
}
6965

70-
// Fetch the cache into a spot in the staging dir
66+
// Fetch the cache into the staging dir
7167
func (c *Cached) Prepare(ctx context.Context) error {
7268
start := time.Now()
73-
folderName, err := randomString()
74-
if err != nil {
75-
return err
76-
}
77-
newDir := path.Join(c.StagingPath, folderName)
78-
version, count, err := c.Client.GetCache(ctx, newDir)
69+
70+
version, count, err := c.Client.GetCache(ctx, c.StagingPath)
7971
if err != nil {
8072
return err
8173
}
8274

83-
c.currentDir = newDir
8475
c.currentVersion = version
8576

86-
logger.Info(ctx, "downloaded golden copy", key.Directory.Field(newDir), key.DurationMS.Field(time.Since(start)), key.Version.Field(version), key.Count.Field(int64(count)))
77+
logger.Info(ctx, "downloaded golden copy", key.DurationMS.Field(time.Since(start)), key.Version.Field(version), key.Count.Field(int64(count)))
8778
return nil
8879
}
8980

@@ -109,7 +100,7 @@ func (c *Cached) GetPluginCapabilities(ctx context.Context, req *csi.GetPluginCa
109100
// Probe returns the health and readiness of the plugin
110101
func (c *Cached) Probe(ctx context.Context, req *csi.ProbeRequest) (*csi.ProbeResponse, error) {
111102
ready := true
112-
if c.currentDir == "" {
103+
if c.currentVersion == 0 {
113104
ready = false
114105
logger.Warn(ctx, "csi probe failed as daemon hasn't prepared cache yet", key.Version.Field(c.currentVersion))
115106
}
@@ -255,7 +246,7 @@ func (c *Cached) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeS
255246
// check if the destination exists, and if so, if its writable
256247
// hardlink the golden copy into this downstream's destination, creating it if need be
257248
func (c *Cached) writeCache(destination string) (int64, error) {
258-
if c.currentDir == "" {
249+
if c.currentVersion == 0 {
259250
return -1, errors.New("no cache prepared, currentDir is nil")
260251
}
261252

@@ -274,7 +265,7 @@ func (c *Cached) writeCache(destination string) (int64, error) {
274265
}
275266
}
276267

277-
err = files.HardlinkDir(c.currentDir, destination)
268+
err = files.HardlinkDir(c.StagingPath, destination)
278269
if err != nil {
279270
return -1, fmt.Errorf("failed to hardlink cache to destination %s: %v", destination, err)
280271
}
@@ -301,12 +292,3 @@ func getFolderSize(path string) (int64, error) {
301292
})
302293
return totalSize, err
303294
}
304-
305-
func randomString() (string, error) {
306-
// Generate a secure random string for the temporary directory name
307-
randBytes := make([]byte, 10) // Adjust the size of the byte slice as needed
308-
if _, err := rand.Read(randBytes); err != nil {
309-
return "", err
310-
}
311-
return base64.URLEncoding.EncodeToString(randBytes), nil
312-
}

0 commit comments

Comments
 (0)