@@ -19,14 +19,17 @@ package utils
19
19
import (
20
20
"encoding/json"
21
21
"fmt"
22
- "io/ioutil"
23
22
"os"
24
23
"path/filepath"
25
24
)
26
25
27
26
var (
28
27
// DefaultCacheDir used for caching
29
- DefaultCacheDir = "/tmp/ovscache"
28
+ DefaultCacheDir = "/var/lib/cni/ovs-cni/cache"
29
+ // OldDefaultCacheDir path to the old caching dir
30
+ OldDefaultCacheDir = "/tmp/ovscache"
31
+ // used for tests
32
+ rootDir = ""
30
33
)
31
34
32
35
// SaveCache takes in key as string and a json encoded struct Conf and save this Conf in cache dir
@@ -35,13 +38,13 @@ func SaveCache(key string, conf interface{}) error {
35
38
if err != nil {
36
39
return fmt .Errorf ("error serializing delegate conf: %v" , err )
37
40
}
38
-
41
+ path := getKeyPath (key )
42
+ cacheDir := filepath .Dir (path )
39
43
// save the rendered conf for cmdDel
40
- if err = os .MkdirAll (DefaultCacheDir , 0700 ); err != nil {
41
- return fmt .Errorf ("failed to create the sriov data directory(%q): %v" , DefaultCacheDir , err )
44
+ if err = os .MkdirAll (cacheDir , 0700 ); err != nil {
45
+ return fmt .Errorf ("failed to create the sriov data directory(%q): %v" , cacheDir , err )
42
46
}
43
- path := getKeyPath (key )
44
- err = ioutil .WriteFile (path , confBytes , 0600 )
47
+ err = os .WriteFile (path , confBytes , 0600 )
45
48
if err != nil {
46
49
return fmt .Errorf ("failed to write container data in the path(%q): %v" , path , err )
47
50
}
@@ -51,22 +54,56 @@ func SaveCache(key string, conf interface{}) error {
51
54
// ReadCache read cached conf from disk for the given key and returns data in byte array
52
55
func ReadCache (key string ) ([]byte , error ) {
53
56
path := getKeyPath (key )
54
- data , err := ioutil .ReadFile (path )
57
+ oldPath := getOldKeyPath (key )
58
+ data , err := readCacheFile (path )
55
59
if err != nil {
56
- return nil , fmt .Errorf ("failed to read container data in the path(%q): %v" , path , err )
60
+ return nil , err
61
+ }
62
+ if data == nil {
63
+ data , err = readCacheFile (oldPath )
64
+ if err != nil {
65
+ return nil , err
66
+ }
57
67
}
58
- return data , err
68
+ if data == nil {
69
+ return nil , fmt .Errorf ("failed to read container data from old(%q) and current(%q) path: not found" , oldPath , path )
70
+ }
71
+ return data , nil
59
72
}
60
73
61
74
// CleanCache removes cached conf from disk for the given key
62
75
func CleanCache (key string ) error {
63
- path := getKeyPath (key )
64
- if err := os .Remove (path ); err != nil {
65
- return fmt .Errorf ("error removing Conf file %s: %q" , path , err )
76
+ if err := removeCacheFile (getKeyPath (key )); err != nil {
77
+ return nil
78
+ }
79
+ return removeCacheFile (getOldKeyPath (key ))
80
+ }
81
+
82
+ // read content from the file in the provided path, returns nil, nil
83
+ // if file not found
84
+ func readCacheFile (path string ) ([]byte , error ) {
85
+ data , err := os .ReadFile (path )
86
+ if err != nil {
87
+ if os .IsNotExist (err ) {
88
+ return nil , nil
89
+ }
90
+ return nil , fmt .Errorf ("failed to read container data in the path(%q): %v" , path , err )
91
+ }
92
+ return data , nil
93
+ }
94
+
95
+ // remove file in the provided path, returns nil if file not found
96
+ func removeCacheFile (path string ) error {
97
+ if err := os .RemoveAll (path ); err != nil {
98
+ return fmt .Errorf ("failed to remove container data from the path(%q): %v" , path , err )
66
99
}
67
100
return nil
68
101
}
69
102
70
103
func getKeyPath (key string ) string {
71
- return filepath .Join (DefaultCacheDir , key )
104
+ return filepath .Join (rootDir , DefaultCacheDir , key )
105
+ }
106
+
107
+ func getOldKeyPath (key string ) string {
108
+ return filepath .Join (rootDir , OldDefaultCacheDir , key )
72
109
}
0 commit comments