@@ -3,6 +3,7 @@ package internalstorage
33import (
44 "context"
55 "fmt"
6+ "strings"
67
78 "gorm.io/gorm"
89 "k8s.io/apimachinery/pkg/runtime/schema"
@@ -12,30 +13,13 @@ import (
1213)
1314
1415type StorageFactory struct {
15- db * gorm.DB
16- AutoMigration * bool
17- DivisionPolicy DivisionPolicy
18- Mapper [] ResourceMapper
16+ db * gorm.DB
17+ model interface {}
18+ SkipAutoMigration bool
19+ DivisionPolicy DivisionPolicy
1920}
2021
2122func (s * StorageFactory ) AutoMigrate () error {
22- if s .AutoMigration != nil && * s .AutoMigration {
23- switch s.DivisionPolicy {
24- if err := s .db .AutoMigrate (& Resource {}); err != nil {
25- return err
26- }
27- case "" , DivisionPolicyNone :
28- case DivisionPolicyGroupResource :
29-
30- }
31-
32- if s .DivisionPolicy == "" || s .DivisionPolicy == DivisionPolicyNone {
33- if err := s .db .AutoMigrate (& Resource {}); err != nil {
34- return err
35- }
36- }
37- }
38-
3923 return nil
4024}
4125
@@ -44,10 +28,40 @@ func (s *StorageFactory) GetSupportedRequestVerbs() []string {
4428}
4529
4630func (s * StorageFactory ) NewResourceStorage (config * storage.ResourceStorageConfig ) (storage.ResourceStorage , error ) {
47- return & ResourceStorage {
48- db : s .db ,
49- codec : config .Codec ,
31+ gvr := schema.GroupVersionResource {
32+ Group : config .StorageGroupResource .Group ,
33+ Version : config .StorageVersion .Version ,
34+ Resource : config .StorageGroupResource .Resource ,
35+ }
36+ table := s .tableName (gvr )
37+
38+ var model interface {}
39+ switch s .DivisionPolicy {
40+ case DivisionPolicyGroupVersionResource :
41+ model = & GroupVersionResource {}
42+ if ! s .SkipAutoMigration {
43+ if exist := s .db .Migrator ().HasTable (table ); ! exist {
44+ if err := s .db .AutoMigrate (& GroupVersionResource {}); err != nil {
45+ return nil , err
46+ }
47+
48+ if err := s .db .Migrator ().RenameTable ("group_version_resources" , table ); err != nil {
49+ if ! s .db .Migrator ().HasTable (table ) {
50+ return nil , err
51+ }
52+ }
53+ }
54+ }
55+ default :
56+ model = & Resource {}
57+ }
58+
59+ s .model = model
5060
61+ return & ResourceStorage {
62+ db : s .db .Table (table ),
63+ model : model ,
64+ codec : config .Codec ,
5165 storageGroupResource : config .StorageGroupResource ,
5266 storageVersion : config .StorageVersion ,
5367 memoryVersion : config .MemoryVersion ,
@@ -64,12 +78,22 @@ func (s *StorageFactory) NewCollectionResourceStorage(cr *internal.CollectionRes
6478}
6579
6680func (s * StorageFactory ) GetResourceVersions (ctx context.Context , cluster string ) (map [schema.GroupVersionResource ]map [string ]interface {}, error ) {
81+ tables , err := s .db .Migrator ().GetTables ()
82+ if err != nil {
83+ return nil , err
84+ }
85+
6786 var resources []Resource
68- result := s .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 )
87+ for _ , table := range tables {
88+ result := s .db .WithContext (ctx ).
89+ Model (s .model ).
90+ Table (table ).
91+ Select ("group" , "version" , "resource" , "namespace" , "name" , "resource_version" ).
92+ Where (map [string ]interface {}{"cluster" : cluster }).
93+ Find (& resources )
94+ if result .Error != nil {
95+ return nil , InterpretDBError (cluster , result .Error )
96+ }
7397 }
7498
7599 resourceversions := make (map [schema.GroupVersionResource ]map [string ]interface {})
@@ -91,18 +115,42 @@ func (s *StorageFactory) GetResourceVersions(ctx context.Context, cluster string
91115}
92116
93117func (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 )
118+ tables , err := s .db .Migrator ().GetTables ()
119+ if err != nil {
120+ return err
121+ }
122+
123+ for _ , table := range tables {
124+ result := s .db .WithContext (ctx ).Table (table ).Model (s .model ).Where (map [string ]interface {}{"cluster" : cluster }).Delete (& Resource {})
125+ if result .Error != nil {
126+ return InterpretDBError (cluster , result .Error )
127+ }
128+ }
129+
130+ return nil
96131}
97132
98133func (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 )
134+ err := s .db .Transaction (func (db * gorm.DB ) error {
135+ result := s .db .WithContext (ctx ).
136+ Table (s .tableName (gvr )).
137+ Model (s .model ).
138+ Where (map [string ]interface {}{
139+ "cluster" : cluster ,
140+ "group" : gvr .Group ,
141+ "version" : gvr .Version ,
142+ "resource" : gvr .Resource ,
143+ }).
144+ Delete (& Resource {})
145+
146+ if result .Error != nil {
147+ return result .Error
148+ }
149+
150+ return nil
151+ })
152+
153+ return InterpretDBError (fmt .Sprintf ("%s/%s" , cluster , gvr ), err )
106154}
107155
108156func (s * StorageFactory ) GetCollectionResources (ctx context.Context ) ([]* internal.CollectionResource , error ) {
@@ -116,3 +164,22 @@ func (s *StorageFactory) GetCollectionResources(ctx context.Context) ([]*interna
116164func (s * StorageFactory ) PrepareCluster (cluster string ) error {
117165 return nil
118166}
167+
168+ // GenerateTableFor return table name using gvr string
169+ func GenerateTableFor (gvr schema.GroupVersionResource ) string {
170+ if gvr .Group == "" {
171+ return fmt .Sprintf ("%s_%s" , gvr .Version , gvr .Resource )
172+ }
173+
174+ group := strings .ReplaceAll (gvr .Group , "." , "_" )
175+ return fmt .Sprintf ("%s_%s_%s" , group , gvr .Version , gvr .Resource )
176+ }
177+
178+ func (s * StorageFactory ) tableName (gvr schema.GroupVersionResource ) string {
179+ table := "resources"
180+ if s .DivisionPolicy == DivisionPolicyGroupVersionResource {
181+ table = GenerateTableFor (gvr )
182+ }
183+
184+ return table
185+ }
0 commit comments