Skip to content

Commit

Permalink
support split table for internal storage
Browse files Browse the repository at this point in the history
Signed-off-by: calvin <[email protected]>
  • Loading branch information
calvin committed Jan 31, 2024
1 parent 89b8466 commit b03565f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
7 changes: 6 additions & 1 deletion pkg/storage/internalstorage/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ func NewStorageFactory(configPath string) (storage.StorageFactory, error) {
sqlDB.SetMaxOpenConns(connPool.MaxOpenConns)
sqlDB.SetConnMaxLifetime(connPool.ConnMaxLifetime)

return &StorageFactory{db}, nil
return &StorageFactory{
db: db,
AutoMigration: cfg.AutoMigration,
DivisionPolicy: cfg.DivisionPolicy,
Mapper: cfg.Mapper,
}, nil
}

func newLogger(cfg *Config) (logger.Interface, error) {
Expand Down
56 changes: 41 additions & 15 deletions pkg/storage/internalstorage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package internalstorage
import (
"context"
"fmt"
"strings"
"sync"

"gorm.io/gorm"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -11,6 +13,8 @@ import (
"github.com/clusterpedia-io/clusterpedia/pkg/storage"
)

var mutex sync.Mutex

type StorageFactory struct {
db *gorm.DB
AutoMigration *bool
Expand All @@ -19,31 +23,47 @@ type StorageFactory struct {
}

func (s *StorageFactory) AutoMigrate() error {
return nil
}

func (s *StorageFactory) GetSupportedRequestVerbs() []string {
return []string{"get", "list"}
}

func (s *StorageFactory) NewResourceStorage(config *storage.ResourceStorageConfig) (storage.ResourceStorage, error) {
mutex.Lock()
defer mutex.Unlock()

if s.AutoMigration != nil && *s.AutoMigration {
switch s.DivisionPolicy {
if err := s.db.AutoMigrate(&Resource{}); err != nil {
return err
}
case "", DivisionPolicyNone:
if exist := s.db.Migrator().HasTable("resources"); !exist {
if err := s.db.AutoMigrate(&Resource{}); err != nil {
return nil, err
}
}
case DivisionPolicyGroupResource:
gvr := schema.GroupVersionResource{
Group: config.StorageGroupResource.Group,
Version: config.StorageVersion.Version,
Resource: config.StorageGroupResource.Resource,
}

}
table := GenerateTableFor(gvr)

if exist := s.db.Migrator().HasTable(table); !exist {
if err := s.db.AutoMigrate(&Resource{}); err != nil {
return nil, err
}

if s.DivisionPolicy == "" || s.DivisionPolicy == DivisionPolicyNone {
if err := s.db.AutoMigrate(&Resource{}); err != nil {
return err
err := s.db.Migrator().RenameTable("resources", table)
if err != nil {
return nil, err
}
}
}
}

return nil
}

func (s *StorageFactory) GetSupportedRequestVerbs() []string {
return []string{"get", "list"}
}

func (s *StorageFactory) NewResourceStorage(config *storage.ResourceStorageConfig) (storage.ResourceStorage, error) {
return &ResourceStorage{
db: s.db,
codec: config.Codec,
Expand Down Expand Up @@ -116,3 +136,9 @@ func (s *StorageFactory) GetCollectionResources(ctx context.Context) ([]*interna
func (s *StorageFactory) PrepareCluster(cluster string) error {
return nil
}

// GenerateTableFor return table name using gvr string
func GenerateTableFor(gvr schema.GroupVersionResource) string {
group := strings.ReplaceAll(gvr.Group, ".", "_")
return fmt.Sprintf("%s_%s_%s", group, gvr.Version, gvr.Resource)
}

0 comments on commit b03565f

Please sign in to comment.