Skip to content

Commit

Permalink
ensure compability with older version
Browse files Browse the repository at this point in the history
Signed-off-by: Congrool <[email protected]>
  • Loading branch information
Congrool committed Nov 15, 2022
1 parent 85fd3d8 commit 392013b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
23 changes: 15 additions & 8 deletions pkg/yurthub/storage/disk/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ func (k storageKey) isRootKey() bool {
}

// Key for disk storage is
// /<Component>/<Resource>/<Namespace>/<Name>, or
// /<Component>/<Resource>/<Name>, if there's no namespace provided in info.
// /<Component>/<Resource>/<Namespace>, if there's no name provided in info.
// /<Component>/<Resource>, if there's no namespace and name provided in info.
// /<Component>/<Resource.Version.Group>/<Namespace>/<Name>, or
// /<Component>/<Resource.Version.Group>/<Name>, if there's no namespace provided in info.
// /<Component>/<Resource.Version.Group>/<Namespace>, if there's no name provided in info.
// /<Component>/<Resource.Version.Group>, if there's no namespace and name provided in info.
// If diskStorage does not run in enhancement mode, it will use the prefix of key as:
// /<Component>/<Resource>/
func (ds *diskStorage) KeyFunc(info storage.KeyBuildInfo) (storage.Key, error) {
isRoot := false
if info.Component == "" {
Expand All @@ -58,12 +60,17 @@ func (ds *diskStorage) KeyFunc(info storage.KeyBuildInfo) (storage.Key, error) {
group = "core"
}

gvrName := strings.Join([]string{info.Resources, info.Version, group}, ".")
var path string
var path, resource string
if ds.enhancementMode {
resource = strings.Join([]string{info.Resources, info.Version, group}, ".")
} else {
resource = info.Resources
}

if info.Resources == "namespaces" {
path = filepath.Join(info.Component, gvrName, info.Name)
path = filepath.Join(info.Component, resource, info.Name)
} else {
path = filepath.Join(info.Component, gvrName, info.Namespace, info.Name)
path = filepath.Join(info.Component, resource, info.Namespace, info.Name)
}

return storageKey{
Expand Down
33 changes: 32 additions & 1 deletion pkg/yurthub/storage/disk/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type diskStorage struct {
keyPendingStatus map[string]struct{}
serializer runtime.Serializer
fsOperator *fs.FileSystemOperator
enhancementMode bool
}

// NewDiskStorage creates a storage.Store for caching data into local disk
Expand All @@ -76,7 +77,13 @@ func NewDiskStorage(dir string) (storage.Store, error) {
fsOperator: fsOperator,
}

err := ds.Recover()
enhancementMode, err := ifEnhancement(ds.baseDir, *ds.fsOperator)
if err != nil {
return nil, fmt.Errorf("cannot detect running mode of disk storage, %v", err)
}
ds.enhancementMode = enhancementMode

err = ds.Recover()
if err != nil {
// we should ensure that there no tmp file last when local storage start to work.
// Otherwise, it means the baseDir cannot serve as local storage dir, because there're some subpath
Expand Down Expand Up @@ -589,6 +596,30 @@ func (ds *diskStorage) unLockKey(key storageKey) {
delete(ds.keyPendingStatus, key.Key())
}

func ifEnhancement(baseDir string, fsOperator fs.FileSystemOperator) (bool, error) {
compDirs, err := fsOperator.List(baseDir, fs.ListModeDirs, false)
if err != nil {
return false, fmt.Errorf("failed to list dirs under %s, %v", baseDir, err)
}

for _, compDir := range compDirs {
resDirs, err := fsOperator.List(compDir, fs.ListModeDirs, false)
if err != nil {
return false, fmt.Errorf("failed to list dirs under %s, %v", compDir, err)
}

for _, resDir := range resDirs {
// Containing resources of old format, so we should run in old mode.
_, name := filepath.Split(resDir)
if len(strings.Split(name, ".")) == 1 {
return false, nil
}
}
}

return true, nil
}

func getTmpKey(key storageKey) storageKey {
dir, file := filepath.Split(key.Key())
return storageKey{
Expand Down
52 changes: 52 additions & 0 deletions pkg/yurthub/storage/disk/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1368,3 +1368,55 @@ func TestExtractInfoFromPath(t *testing.T) {
})
}
}

func TestIfEnhancement(t *testing.T) {
cases := []struct {
existingFile map[string][]byte
want bool
description string
}{
{
existingFile: map[string][]byte{
"/kubelet/pods/default/nginx": []byte("nginx-pod"),
},
want: false,
description: "should not run in enhancement mode if there's old cache",
},
{
existingFile: map[string][]byte{},
want: true,
description: "should run in enhancement mode if there's no old cache",
},
{
existingFile: map[string][]byte{
"/kubelet/pods.v1.core/default/nginx": []byte("nginx-pod"),
},
want: true,
description: "should run in enhancement mode if all cache are resource.version.group format",
},
}

for _, c := range cases {
baseDir := diskStorageTestBaseDir
t.Run(c.description, func(t *testing.T) {
os.RemoveAll(baseDir)
fsOperator := fs.FileSystemOperator{}
fsOperator.CreateDir(baseDir)

for f, b := range c.existingFile {
path := filepath.Join(baseDir, f)
if err := fsOperator.CreateFile(path, b); err != nil {
t.Errorf("failed to create file %s, %v", path, err)
}
}

mode, err := ifEnhancement(baseDir, fsOperator)
if err != nil {
t.Errorf("failed to create disk storage, %v", err)
}
if mode != c.want {
t.Errorf("unexpected running mode, want: %v, got: %v", c.want, mode)
}
})
}
}

0 comments on commit 392013b

Please sign in to comment.