@@ -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,35 +23,61 @@ 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
+ gvr := schema.GroupVersionResource {
38
+ Group : config .StorageGroupResource .Group ,
39
+ Version : config .StorageVersion .Version ,
40
+ Resource : config .StorageGroupResource .Resource ,
41
+ }
42
+ table := s .tableName (gvr )
43
+
44
+ var model interface {}
22
45
if s .AutoMigration != nil && * s .AutoMigration {
46
+ model = & Resource {}
47
+
23
48
switch s .DivisionPolicy {
24
- if err := s .db .AutoMigrate (& Resource {}); err != nil {
25
- return err
49
+ case DivisionPolicyNone :
50
+ if exist := s .db .Migrator ().HasTable (table ); ! exist {
51
+ if err := s .db .AutoMigrate (& Resource {}); err != nil {
52
+ return nil , err
53
+ }
26
54
}
27
- case "" , DivisionPolicyNone :
28
55
case DivisionPolicyGroupResource :
56
+ model = & GroupVersionResource {}
29
57
30
- }
58
+ if exist := s .db .Migrator ().HasTable (table ); ! exist {
59
+ if err := s .db .AutoMigrate (& GroupVersionResource {}); err != nil {
60
+ return nil , err
61
+ }
31
62
32
- if s .DivisionPolicy == "" || s .DivisionPolicy == DivisionPolicyNone {
33
- if err := s .db .AutoMigrate (& Resource {}); err != nil {
34
- return err
63
+ err := s .db .Migrator ().RenameTable ("groupversionresources" , table )
64
+ if err != nil {
65
+ return nil , err
66
+ }
67
+ }
68
+ default :
69
+ if exist := s .db .Migrator ().HasTable (table ); ! exist {
70
+ if err := s .db .AutoMigrate (& Resource {}); err != nil {
71
+ return nil , err
72
+ }
35
73
}
36
74
}
37
75
}
38
76
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
77
return & ResourceStorage {
48
- db : s .db ,
49
- codec : config . Codec ,
50
-
78
+ db : s .db . Table ( table ) ,
79
+ model : model ,
80
+ codec : config . Codec ,
51
81
storageGroupResource : config .StorageGroupResource ,
52
82
storageVersion : config .StorageVersion ,
53
83
memoryVersion : config .MemoryVersion ,
@@ -65,7 +95,8 @@ func (s *StorageFactory) NewCollectionResourceStorage(cr *internal.CollectionRes
65
95
66
96
func (s * StorageFactory ) GetResourceVersions (ctx context.Context , cluster string ) (map [schema.GroupVersionResource ]map [string ]interface {}, error ) {
67
97
var resources []Resource
68
- result := s .db .WithContext (ctx ).Select ("group" , "version" , "resource" , "namespace" , "name" , "resource_version" ).
98
+ result := s .db .WithContext (ctx ).
99
+ Select ("group" , "version" , "resource" , "namespace" , "name" , "resource_version" ).
69
100
Where (map [string ]interface {}{"cluster" : cluster }).
70
101
Find (& resources )
71
102
if result .Error != nil {
@@ -91,18 +122,46 @@ func (s *StorageFactory) GetResourceVersions(ctx context.Context, cluster string
91
122
}
92
123
93
124
func (s * StorageFactory ) CleanCluster (ctx context.Context , cluster string ) error {
94
- result := s .db .WithContext (ctx ).Where (map [string ]interface {}{"cluster" : cluster }).Delete (& Resource {})
95
- return InterpretDBError (cluster , result .Error )
125
+ mutex .Lock ()
126
+ tables , err := s .db .Migrator ().GetTables ()
127
+ if err != nil {
128
+ mutex .Unlock ()
129
+ return err
130
+ }
131
+ mutex .Unlock ()
132
+
133
+ for _ , table := range tables {
134
+ result := s .db .WithContext (ctx ).Table (table ).Where (map [string ]interface {}{"cluster" : cluster }).Delete (& Resource {})
135
+ if result .Error != nil {
136
+ return InterpretDBError (cluster , result .Error )
137
+ }
138
+ }
139
+
140
+ return nil
96
141
}
97
142
98
143
func (s * StorageFactory ) CleanClusterResource (ctx context.Context , cluster string , gvr schema.GroupVersionResource ) error {
99
- result := s .db .WithContext (ctx ).Where (map [string ]interface {}{
100
- "cluster" : cluster ,
101
- "group" : gvr .Group ,
102
- "version" : gvr .Version ,
103
- "resource" : gvr .Resource ,
104
- }).Delete (& Resource {})
105
- return InterpretDBError (fmt .Sprintf ("%s/%s" , cluster , gvr ), result .Error )
144
+ table := s .tableName (gvr )
145
+
146
+ err := s .db .Transaction (func (db * gorm.DB ) error {
147
+ result := s .db .WithContext (ctx ).
148
+ Table (table ).
149
+ Where (map [string ]interface {}{
150
+ "cluster" : cluster ,
151
+ "group" : gvr .Group ,
152
+ "version" : gvr .Version ,
153
+ "resource" : gvr .Resource ,
154
+ }).
155
+ Delete (& Resource {})
156
+
157
+ if result .Error != nil {
158
+ return result .Error
159
+ }
160
+
161
+ return nil
162
+ })
163
+
164
+ return InterpretDBError (fmt .Sprintf ("%s/%s" , cluster , gvr ), err )
106
165
}
107
166
108
167
func (s * StorageFactory ) GetCollectionResources (ctx context.Context ) ([]* internal.CollectionResource , error ) {
@@ -116,3 +175,22 @@ func (s *StorageFactory) GetCollectionResources(ctx context.Context) ([]*interna
116
175
func (s * StorageFactory ) PrepareCluster (cluster string ) error {
117
176
return nil
118
177
}
178
+
179
+ func (s * StorageFactory ) tableName (gvr schema.GroupVersionResource ) string {
180
+ table := "resources"
181
+ if s .DivisionPolicy == DivisionPolicyCustom {
182
+ table = GenerateTableFor (gvr )
183
+ }
184
+
185
+ return table
186
+ }
187
+
188
+ // GenerateTableFor return table name using gvr string
189
+ func GenerateTableFor (gvr schema.GroupVersionResource ) string {
190
+ if gvr .Group == "" {
191
+ return fmt .Sprintf ("%s_%s" , gvr .Version , gvr .Resource )
192
+ }
193
+
194
+ group := strings .ReplaceAll (gvr .Group , "." , "_" )
195
+ return fmt .Sprintf ("%s_%s_%s" , group , gvr .Version , gvr .Resource )
196
+ }
0 commit comments