@@ -3,6 +3,8 @@ package internalstorage
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
7
+ "sync"
6
8
7
9
"gorm.io/gorm"
8
10
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -11,6 +13,8 @@ import (
11
13
"github.com/clusterpedia-io/clusterpedia/pkg/storage"
12
14
)
13
15
16
+ var mutex sync.Mutex
17
+
14
18
type StorageFactory struct {
15
19
db * gorm.DB
16
20
AutoMigration * bool
@@ -19,34 +23,54 @@ type StorageFactory struct {
19
23
}
20
24
21
25
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
+
37
+ var table string
22
38
if s .AutoMigration != nil && * s .AutoMigration {
23
39
switch s .DivisionPolicy {
24
- if err := s .db .AutoMigrate (& Resource {}); err != nil {
25
- return err
26
- }
27
40
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
+ }
28
48
case DivisionPolicyGroupResource :
49
+ gvr := schema.GroupVersionResource {
50
+ Group : config .StorageGroupResource .Group ,
51
+ Version : config .StorageVersion .Version ,
52
+ Resource : config .StorageGroupResource .Resource ,
53
+ }
29
54
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
+ }
31
61
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
+ }
35
66
}
36
67
}
37
68
}
38
69
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 ) {
47
70
return & ResourceStorage {
48
71
db : s .db ,
49
72
codec : config .Codec ,
73
+ table : table ,
50
74
51
75
storageGroupResource : config .StorageGroupResource ,
52
76
storageVersion : config .StorageVersion ,
@@ -65,11 +89,23 @@ func (s *StorageFactory) NewCollectionResourceStorage(cr *internal.CollectionRes
65
89
66
90
func (f * StorageFactory ) GetResourceVersions (ctx context.Context , cluster string ) (map [schema.GroupVersionResource ]map [string ]interface {}, error ) {
67
91
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 ... )
73
109
}
74
110
75
111
resourceversions := make (map [schema.GroupVersionResource ]map [string ]interface {})
@@ -91,12 +127,25 @@ func (f *StorageFactory) GetResourceVersions(ctx context.Context, cluster string
91
127
}
92
128
93
129
func (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
96
145
}
97
146
98
147
func (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 {}{
100
149
"cluster" : cluster ,
101
150
"group" : gvr .Group ,
102
151
"version" : gvr .Version ,
@@ -116,3 +165,13 @@ func (s *StorageFactory) GetCollectionResources(ctx context.Context) ([]*interna
116
165
func (s * StorageFactory ) PrepareCluster (cluster string ) error {
117
166
return nil
118
167
}
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