@@ -2,8 +2,6 @@ package api
2
2
3
3
import (
4
4
"context"
5
- "crypto/rand"
6
- "encoding/base64"
7
5
"errors"
8
6
"fmt"
9
7
"math"
@@ -41,9 +39,7 @@ type Cached struct {
41
39
Client * client.Client
42
40
StagingPath string
43
41
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
47
43
currentVersion int64
48
44
}
49
45
@@ -67,23 +63,18 @@ func (c *Cached) PopulateDiskCache(ctx context.Context, req *pb.PopulateDiskCach
67
63
return & pb.PopulateDiskCacheResponse {Version : version }, nil
68
64
}
69
65
70
- // Fetch the cache into a spot in the staging dir
66
+ // Fetch the cache into the staging dir
71
67
func (c * Cached ) Prepare (ctx context.Context ) error {
72
68
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 )
79
71
if err != nil {
80
72
return err
81
73
}
82
74
83
- c .currentDir = newDir
84
75
c .currentVersion = version
85
76
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 )))
87
78
return nil
88
79
}
89
80
@@ -109,7 +100,7 @@ func (c *Cached) GetPluginCapabilities(ctx context.Context, req *csi.GetPluginCa
109
100
// Probe returns the health and readiness of the plugin
110
101
func (c * Cached ) Probe (ctx context.Context , req * csi.ProbeRequest ) (* csi.ProbeResponse , error ) {
111
102
ready := true
112
- if c .currentDir == "" {
103
+ if c .currentVersion == 0 {
113
104
ready = false
114
105
logger .Warn (ctx , "csi probe failed as daemon hasn't prepared cache yet" , key .Version .Field (c .currentVersion ))
115
106
}
@@ -255,7 +246,7 @@ func (c *Cached) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeS
255
246
// check if the destination exists, and if so, if its writable
256
247
// hardlink the golden copy into this downstream's destination, creating it if need be
257
248
func (c * Cached ) writeCache (destination string ) (int64 , error ) {
258
- if c .currentDir == "" {
249
+ if c .currentVersion == 0 {
259
250
return - 1 , errors .New ("no cache prepared, currentDir is nil" )
260
251
}
261
252
@@ -274,7 +265,7 @@ func (c *Cached) writeCache(destination string) (int64, error) {
274
265
}
275
266
}
276
267
277
- err = files .HardlinkDir (c .currentDir , destination )
268
+ err = files .HardlinkDir (c .StagingPath , destination )
278
269
if err != nil {
279
270
return - 1 , fmt .Errorf ("failed to hardlink cache to destination %s: %v" , destination , err )
280
271
}
@@ -301,12 +292,3 @@ func getFolderSize(path string) (int64, error) {
301
292
})
302
293
return totalSize , err
303
294
}
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