Skip to content

Commit b03565f

Browse files
author
calvin
committed
support split table for internal storage
Signed-off-by: calvin <[email protected]>
1 parent 89b8466 commit b03565f

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

pkg/storage/internalstorage/register.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ func NewStorageFactory(configPath string) (storage.StorageFactory, error) {
9393
sqlDB.SetMaxOpenConns(connPool.MaxOpenConns)
9494
sqlDB.SetConnMaxLifetime(connPool.ConnMaxLifetime)
9595

96-
return &StorageFactory{db}, nil
96+
return &StorageFactory{
97+
db: db,
98+
AutoMigration: cfg.AutoMigration,
99+
DivisionPolicy: cfg.DivisionPolicy,
100+
Mapper: cfg.Mapper,
101+
}, nil
97102
}
98103

99104
func newLogger(cfg *Config) (logger.Interface, error) {

pkg/storage/internalstorage/storage.go

+41-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package internalstorage
33
import (
44
"context"
55
"fmt"
6+
"strings"
7+
"sync"
68

79
"gorm.io/gorm"
810
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -11,6 +13,8 @@ import (
1113
"github.com/clusterpedia-io/clusterpedia/pkg/storage"
1214
)
1315

16+
var mutex sync.Mutex
17+
1418
type StorageFactory struct {
1519
db *gorm.DB
1620
AutoMigration *bool
@@ -19,31 +23,47 @@ type StorageFactory struct {
1923
}
2024

2125
func (s *StorageFactory) AutoMigrate() error {
26+
return nil
27+
}
28+
29+
func (s *StorageFactory) GetSupportedRequestVerbs() []string {
30+
return []string{"get", "list"}
31+
}
32+
33+
func (s *StorageFactory) NewResourceStorage(config *storage.ResourceStorageConfig) (storage.ResourceStorage, error) {
34+
mutex.Lock()
35+
defer mutex.Unlock()
36+
2237
if s.AutoMigration != nil && *s.AutoMigration {
2338
switch s.DivisionPolicy {
24-
if err := s.db.AutoMigrate(&Resource{}); err != nil {
25-
return err
26-
}
2739
case "", DivisionPolicyNone:
40+
if exist := s.db.Migrator().HasTable("resources"); !exist {
41+
if err := s.db.AutoMigrate(&Resource{}); err != nil {
42+
return nil, err
43+
}
44+
}
2845
case DivisionPolicyGroupResource:
46+
gvr := schema.GroupVersionResource{
47+
Group: config.StorageGroupResource.Group,
48+
Version: config.StorageVersion.Version,
49+
Resource: config.StorageGroupResource.Resource,
50+
}
2951

30-
}
52+
table := GenerateTableFor(gvr)
53+
54+
if exist := s.db.Migrator().HasTable(table); !exist {
55+
if err := s.db.AutoMigrate(&Resource{}); err != nil {
56+
return nil, err
57+
}
3158

32-
if s.DivisionPolicy == "" || s.DivisionPolicy == DivisionPolicyNone {
33-
if err := s.db.AutoMigrate(&Resource{}); err != nil {
34-
return err
59+
err := s.db.Migrator().RenameTable("resources", table)
60+
if err != nil {
61+
return nil, err
62+
}
3563
}
3664
}
3765
}
3866

39-
return nil
40-
}
41-
42-
func (s *StorageFactory) GetSupportedRequestVerbs() []string {
43-
return []string{"get", "list"}
44-
}
45-
46-
func (s *StorageFactory) NewResourceStorage(config *storage.ResourceStorageConfig) (storage.ResourceStorage, error) {
4767
return &ResourceStorage{
4868
db: s.db,
4969
codec: config.Codec,
@@ -116,3 +136,9 @@ func (s *StorageFactory) GetCollectionResources(ctx context.Context) ([]*interna
116136
func (s *StorageFactory) PrepareCluster(cluster string) error {
117137
return nil
118138
}
139+
140+
// GenerateTableFor return table name using gvr string
141+
func GenerateTableFor(gvr schema.GroupVersionResource) string {
142+
group := strings.ReplaceAll(gvr.Group, ".", "_")
143+
return fmt.Sprintf("%s_%s_%s", group, gvr.Version, gvr.Resource)
144+
}

0 commit comments

Comments
 (0)