@@ -3,6 +3,8 @@ package internalstorage
33import (
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+
1418type StorageFactory struct {
1519 db * gorm.DB
1620 AutoMigration * bool
@@ -19,34 +23,54 @@ type StorageFactory struct {
1923}
2024
2125func (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+
37+ var table string
2238 if s .AutoMigration != nil && * s .AutoMigration {
2339 switch s .DivisionPolicy {
24- if err := s .db .AutoMigrate (& Resource {}); err != nil {
25- return err
26- }
2740 case "" , DivisionPolicyNone :
41+ table = "resources"
42+
43+ if exist := s .db .Migrator ().HasTable (table ); ! exist {
44+ if err := s .db .AutoMigrate (& Resource {}); err != nil {
45+ return nil , err
46+ }
47+ }
2848 case DivisionPolicyGroupResource :
49+ gvr := schema.GroupVersionResource {
50+ Group : config .StorageGroupResource .Group ,
51+ Version : config .StorageVersion .Version ,
52+ Resource : config .StorageGroupResource .Resource ,
53+ }
2954
30- }
55+ table = GenerateTableFor (gvr )
56+
57+ if exist := s .db .Migrator ().HasTable (table ); ! exist {
58+ if err := s .db .AutoMigrate (& Resource {}); err != nil {
59+ return nil , err
60+ }
3161
32- if s .DivisionPolicy == "" || s .DivisionPolicy == DivisionPolicyNone {
33- if err := s .db .AutoMigrate (& Resource {}); err != nil {
34- return err
62+ err := s .db .Migrator ().RenameTable ("resources" , table )
63+ if err != nil {
64+ return nil , err
65+ }
3566 }
3667 }
3768 }
3869
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 ) {
4770 return & ResourceStorage {
4871 db : s .db ,
4972 codec : config .Codec ,
73+ table : table ,
5074
5175 storageGroupResource : config .StorageGroupResource ,
5276 storageVersion : config .StorageVersion ,
@@ -65,11 +89,23 @@ func (s *StorageFactory) NewCollectionResourceStorage(cr *internal.CollectionRes
6589
6690func (f * StorageFactory ) GetResourceVersions (ctx context.Context , cluster string ) (map [schema.GroupVersionResource ]map [string ]interface {}, error ) {
6791 var resources []Resource
68- result := f .db .WithContext (ctx ).Select ("group" , "version" , "resource" , "namespace" , "name" , "resource_version" ).
69- Where (map [string ]interface {}{"cluster" : cluster }).
70- Find (& resources )
71- if result .Error != nil {
72- return nil , InterpretDBError (cluster , result .Error )
92+ mutex .Lock ()
93+ tables , err := f .db .Migrator ().GetTables ()
94+ if err != nil {
95+ mutex .Unlock ()
96+ return nil , err
97+ }
98+ mutex .Unlock ()
99+ for _ , table := range tables {
100+ var tableResources []Resource
101+ result := f .db .WithContext (ctx ).Table (table ).Select ("group" , "version" , "resource" , "namespace" , "name" , "resource_version" ).
102+ Where (map [string ]interface {}{"cluster" : cluster }).
103+ Find (& tableResources )
104+ if result .Error != nil {
105+ return nil , InterpretDBError (cluster , result .Error )
106+ }
107+
108+ resources = append (resources , tableResources ... )
73109 }
74110
75111 resourceversions := make (map [schema.GroupVersionResource ]map [string ]interface {})
@@ -91,12 +127,25 @@ func (f *StorageFactory) GetResourceVersions(ctx context.Context, cluster string
91127}
92128
93129func (f * StorageFactory ) CleanCluster (ctx context.Context , cluster string ) error {
94- result := f .db .WithContext (ctx ).Where (map [string ]interface {}{"cluster" : cluster }).Delete (& Resource {})
95- return InterpretDBError (cluster , result .Error )
130+ mutex .Lock ()
131+ tables , err := f .db .Migrator ().GetTables ()
132+ if err != nil {
133+ mutex .Unlock ()
134+ return err
135+ }
136+ mutex .Unlock ()
137+
138+ for _ , table := range tables {
139+ result := f .db .WithContext (ctx ).Table (table ).Where (map [string ]interface {}{"cluster" : cluster }).Delete (& Resource {})
140+ if result .Error != nil {
141+ return InterpretDBError (cluster , result .Error )
142+ }
143+ }
144+ return nil
96145}
97146
98147func (s * StorageFactory ) CleanClusterResource (ctx context.Context , cluster string , gvr schema.GroupVersionResource ) error {
99- result := s .db .WithContext (ctx ).Where (map [string ]interface {}{
148+ result := s .db .WithContext (ctx ).Table ( GenerateTableFor ( gvr )). Where (map [string ]interface {}{
100149 "cluster" : cluster ,
101150 "group" : gvr .Group ,
102151 "version" : gvr .Version ,
@@ -116,3 +165,13 @@ func (s *StorageFactory) GetCollectionResources(ctx context.Context) ([]*interna
116165func (s * StorageFactory ) PrepareCluster (cluster string ) error {
117166 return nil
118167}
168+
169+ // GenerateTableFor return table name using gvr string
170+ func GenerateTableFor (gvr schema.GroupVersionResource ) string {
171+ if gvr .Group == "" {
172+ return fmt .Sprintf ("%s_%s" , gvr .Version , gvr .Resource )
173+ }
174+
175+ group := strings .ReplaceAll (gvr .Group , "." , "_" )
176+ return fmt .Sprintf ("%s_%s_%s" , group , gvr .Version , gvr .Resource )
177+ }
0 commit comments