From 2d0bf7c31b15f90aec711ceeba5331ff81d9109f Mon Sep 17 00:00:00 2001 From: vie-serendipity <2733147505@qq.com> Date: Tue, 16 Jul 2024 12:00:12 +0800 Subject: [PATCH] fix: clusterInfo implement key interface --- pkg/yurthub/cachemanager/storage_wrapper.go | 18 ++++++------ pkg/yurthub/storage/disk/storage.go | 32 ++++++--------------- pkg/yurthub/storage/etcd/storage.go | 4 +-- pkg/yurthub/storage/key.go | 20 +++++++++++++ pkg/yurthub/storage/store.go | 15 ++++------ 5 files changed, 45 insertions(+), 44 deletions(-) diff --git a/pkg/yurthub/cachemanager/storage_wrapper.go b/pkg/yurthub/cachemanager/storage_wrapper.go index 90965ff49b4..4a8f07289ca 100644 --- a/pkg/yurthub/cachemanager/storage_wrapper.go +++ b/pkg/yurthub/cachemanager/storage_wrapper.go @@ -45,8 +45,8 @@ type StorageWrapper interface { ListResourceKeysOfComponent(component string, gvr schema.GroupVersionResource) ([]storage.Key, error) ReplaceComponentList(component string, gvr schema.GroupVersionResource, namespace string, contents map[storage.Key]runtime.Object) error DeleteComponentResources(component string) error - SaveClusterInfo(key storage.ClusterInfoKey, content []byte) error - GetClusterInfo(key storage.ClusterInfoKey) ([]byte, error) + SaveClusterInfo(key storage.Key, content []byte) error + GetClusterInfo(key storage.Key) ([]byte, error) GetStorage() storage.Store GetCacheResult() (int, string) } @@ -225,7 +225,7 @@ func (sw *storageWrapper) ReplaceComponentList(component string, gvr schema.Grou contents := make(map[storage.Key][]byte, len(objs)) for key, obj := range objs { if err := sw.backendSerializer.Encode(obj, &buf); err != nil { - for k := range contents { + for k := range objs { sw.errorKeys.put(k.Key(), err.Error()) } klog.Errorf("could not encode object in update for %s, %v", key.Key(), err) @@ -238,12 +238,12 @@ func (sw *storageWrapper) ReplaceComponentList(component string, gvr schema.Grou err := sw.store.ReplaceComponentList(component, gvr, namespace, contents) if err != nil { - for key := range contents { + for key := range objs { sw.errorKeys.put(key.Key(), err.Error()) } return err } - for key := range contents { + for key := range objs { sw.errorKeys.del(key.Key()) } return nil @@ -263,16 +263,16 @@ func (sw *storageWrapper) DeleteComponentResources(component string) error { return nil } -func (sw *storageWrapper) SaveClusterInfo(key storage.ClusterInfoKey, content []byte) error { +func (sw *storageWrapper) SaveClusterInfo(key storage.Key, content []byte) error { err := sw.store.SaveClusterInfo(key, content) if err != nil { - sw.errorKeys.put(string(key.ClusterInfoType), fmt.Sprintf("failed to store cluster info, %v", err.Error())) + sw.errorKeys.put(key.Key(), fmt.Sprintf("failed to store cluster info, %v", err.Error())) return err } - sw.errorKeys.del(string(key.ClusterInfoType)) + sw.errorKeys.del(key.Key()) return nil } -func (sw *storageWrapper) GetClusterInfo(key storage.ClusterInfoKey) ([]byte, error) { +func (sw *storageWrapper) GetClusterInfo(key storage.Key) ([]byte, error) { return sw.store.GetClusterInfo(key) } diff --git a/pkg/yurthub/storage/disk/storage.go b/pkg/yurthub/storage/disk/storage.go index 25d32d988a5..1ee41164f05 100644 --- a/pkg/yurthub/storage/disk/storage.go +++ b/pkg/yurthub/storage/disk/storage.go @@ -421,50 +421,36 @@ func (ds *diskStorage) DeleteComponentResources(component string) error { return nil } -func (ds *diskStorage) SaveClusterInfo(key storage.ClusterInfoKey, content []byte) error { - var path string - switch key.ClusterInfoType { - case storage.APIsInfo, storage.Version: - path = filepath.Join(ds.baseDir, string(key.ClusterInfoType)) - case storage.APIResourcesInfo: - translatedURLPath := strings.ReplaceAll(key.UrlPath, "/", "_") - path = filepath.Join(ds.baseDir, translatedURLPath) - default: +func (ds *diskStorage) SaveClusterInfo(key storage.Key, content []byte) error { + if key.Key() == "" { return storage.ErrUnknownClusterInfoType } - + path := filepath.Join(ds.baseDir, key.Key()) if err := ds.fsOperator.CreateFile(path, content); err != nil { if err == fs.ErrExists { // file exists, overwrite it with content if werr := ds.fsOperator.Write(path, content); werr != nil { - return fmt.Errorf("could not update clusterInfo %s at path %s, %v", key.ClusterInfoType, path, werr) + return fmt.Errorf("could not update clusterInfo at path %s, %v", path, werr) } return nil } - return fmt.Errorf("could not create %s clusterInfo file at path %s, %v", key.ClusterInfoType, path, err) + return fmt.Errorf("could not create clusterInfo file at path %s, %v", path, err) } return nil } -func (ds *diskStorage) GetClusterInfo(key storage.ClusterInfoKey) ([]byte, error) { - var path string - switch key.ClusterInfoType { - case storage.APIsInfo, storage.Version: - path = filepath.Join(ds.baseDir, string(key.ClusterInfoType)) - case storage.APIResourcesInfo: - translatedURLPath := strings.ReplaceAll(key.UrlPath, "/", "_") - path = filepath.Join(ds.baseDir, translatedURLPath) - default: +func (ds *diskStorage) GetClusterInfo(key storage.Key) ([]byte, error) { + if key.Key() == "" { return nil, storage.ErrUnknownClusterInfoType } - + path := filepath.Join(ds.baseDir, key.Key()) var buf []byte var err error if buf, err = ds.fsOperator.Read(path); err != nil { if err == fs.ErrNotExists { return nil, storage.ErrStorageNotFound } - return nil, fmt.Errorf("could not read %s clusterInfo file at %s, %v", key.ClusterInfoType, path, err) + return nil, fmt.Errorf("could not read clusterInfo file at %s, %v", path, err) } return buf, nil } diff --git a/pkg/yurthub/storage/etcd/storage.go b/pkg/yurthub/storage/etcd/storage.go index 2164f18a4b3..ef3bae02680 100644 --- a/pkg/yurthub/storage/etcd/storage.go +++ b/pkg/yurthub/storage/etcd/storage.go @@ -514,9 +514,9 @@ func fresherThan(rv string, key string) clientv3.Cmp { type doNothingAboutClusterInfo struct{} -func (d doNothingAboutClusterInfo) SaveClusterInfo(_ storage.ClusterInfoKey, _ []byte) error { +func (d doNothingAboutClusterInfo) SaveClusterInfo(_ storage.Key, _ []byte) error { return nil } -func (d doNothingAboutClusterInfo) GetClusterInfo(_ storage.ClusterInfoKey) ([]byte, error) { +func (d doNothingAboutClusterInfo) GetClusterInfo(_ storage.Key) ([]byte, error) { return nil, nil } diff --git a/pkg/yurthub/storage/key.go b/pkg/yurthub/storage/key.go index 729740f3253..889392d57d8 100644 --- a/pkg/yurthub/storage/key.go +++ b/pkg/yurthub/storage/key.go @@ -16,6 +16,8 @@ limitations under the License. package storage +import "strings" + type Key interface { Key() string } @@ -28,3 +30,21 @@ type KeyBuildInfo struct { Group string Version string } + +type ClusterInfoKey struct { + ClusterInfoType + UrlPath string +} + +type ClusterInfoType string + +func (key ClusterInfoKey) Key() string { + switch key.ClusterInfoType { + case APIsInfo, Version: + return string(key.ClusterInfoType) + case APIResourcesInfo: + return strings.ReplaceAll(key.UrlPath, "/", "_") + default: + return "" + } +} diff --git a/pkg/yurthub/storage/store.go b/pkg/yurthub/storage/store.go index 6aad24574e8..74af96f353d 100644 --- a/pkg/yurthub/storage/store.go +++ b/pkg/yurthub/storage/store.go @@ -16,14 +16,9 @@ limitations under the License. package storage -import "k8s.io/apimachinery/pkg/runtime/schema" - -type ClusterInfoKey struct { - ClusterInfoType - UrlPath string -} - -type ClusterInfoType string +import ( + "k8s.io/apimachinery/pkg/runtime/schema" +) const ( Version ClusterInfoType = "version" @@ -45,10 +40,10 @@ type Store interface { type clusterInfoHandler interface { // SaveClusterInfo will save content of cluster info into storage. // If the content has already existed in the storage, it will be overwritten with content. - SaveClusterInfo(key ClusterInfoKey, content []byte) error + SaveClusterInfo(key Key, content []byte) error // GetClusterInfo will get the cluster info of clusterInfoType from storage. // If the cluster info is not found in the storage, return ErrStorageNotFound. - GetClusterInfo(key ClusterInfoKey) ([]byte, error) + GetClusterInfo(key Key) ([]byte, error) } // objectRelatedHandler contains functions for manipulating resource objects in the format of key-value