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 29, 2024
1 parent 89b8466 commit ee900b1
Show file tree
Hide file tree
Showing 2 changed files with 43 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
52 changes: 37 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,43 @@ type StorageFactory struct {
}

func (s *StorageFactory) AutoMigrate() error {

Check failure on line 25 in pkg/storage/internalstorage/storage.go

View workflow job for this annotation

GitHub Actions / Lint with golangci-lint

unnecessary leading newline (whitespace)

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:
if err := s.db.AutoMigrate(&Resource{}); err != nil {
return nil, err
}

}
gvr := schema.GroupVersionResource{
Group: config.StorageGroupResource.Group,
Version: config.StorageVersion.Version,
Resource: config.StorageGroupResource.Resource,
}

if s.DivisionPolicy == "" || s.DivisionPolicy == DivisionPolicyNone {
if err := s.db.AutoMigrate(&Resource{}); err != nil {
return err
err := s.db.Migrator().RenameTable("resources", GenerateTableFor(gvr))
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 +132,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 ee900b1

Please sign in to comment.