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