Skip to content

Commit

Permalink
fix: clusterInfo implement key interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vie-serendipity committed Jul 16, 2024
1 parent 02b6157 commit 62f8c4b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
18 changes: 9 additions & 9 deletions pkg/yurthub/cachemanager/storage_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
32 changes: 9 additions & 23 deletions pkg/yurthub/storage/disk/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
path := filepath.Join(ds.baseDir, key.Key())
if path == "" {
return storage.ErrUnknownClusterInfoType
}

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) {
path := filepath.Join(ds.baseDir, key.Key())
if path == "" {
return nil, storage.ErrUnknownClusterInfoType
}

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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/yurthub/storage/etcd/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions pkg/yurthub/storage/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package storage

import "strings"

type Key interface {
Key() string
}
Expand All @@ -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 ""
}
}
15 changes: 5 additions & 10 deletions pkg/yurthub/storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 62f8c4b

Please sign in to comment.