@@ -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,37 @@ 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+ }
5058
59+ return & ResourceStorage {
60+ db : s .db .Table (table ).Model (model ),
61+ codec : config .Codec ,
5162 storageGroupResource : config .StorageGroupResource ,
5263 storageVersion : config .StorageVersion ,
5364 memoryVersion : config .MemoryVersion ,
@@ -64,12 +75,21 @@ func (s *StorageFactory) NewCollectionResourceStorage(cr *internal.CollectionRes
6475}
6576
6677func (s * StorageFactory ) GetResourceVersions (ctx context.Context , cluster string ) (map [schema.GroupVersionResource ]map [string ]interface {}, error ) {
78+ tables , err := s .db .Migrator ().GetTables ()
79+ if err != nil {
80+ return nil , err
81+ }
82+
6783 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 )
84+ for _ , table := range tables {
85+ result := s .db .WithContext (ctx ).
86+ Table (table ).
87+ Select ("group" , "version" , "resource" , "namespace" , "name" , "resource_version" ).
88+ Where (map [string ]interface {}{"cluster" : cluster }).
89+ Find (& resources )
90+ if result .Error != nil {
91+ return nil , InterpretDBError (cluster , result .Error )
92+ }
7393 }
7494
7595 resourceversions := make (map [schema.GroupVersionResource ]map [string ]interface {})
@@ -91,18 +111,41 @@ func (s *StorageFactory) GetResourceVersions(ctx context.Context, cluster string
91111}
92112
93113func (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 )
114+ tables , err := s .db .Migrator ().GetTables ()
115+ if err != nil {
116+ return err
117+ }
118+
119+ for _ , table := range tables {
120+ result := s .db .WithContext (ctx ).Table (table ).Where (map [string ]interface {}{"cluster" : cluster }).Delete (& Resource {})
121+ if result .Error != nil {
122+ return InterpretDBError (cluster , result .Error )
123+ }
124+ }
125+
126+ return nil
96127}
97128
98129func (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 )
130+ err := s .db .Transaction (func (db * gorm.DB ) error {
131+ result := s .db .WithContext (ctx ).
132+ Table (s .tableName (gvr )).
133+ Where (map [string ]interface {}{
134+ "cluster" : cluster ,
135+ "group" : gvr .Group ,
136+ "version" : gvr .Version ,
137+ "resource" : gvr .Resource ,
138+ }).
139+ Delete (& Resource {})
140+
141+ if result .Error != nil {
142+ return result .Error
143+ }
144+
145+ return nil
146+ })
147+
148+ return InterpretDBError (fmt .Sprintf ("%s/%s" , cluster , gvr ), err )
106149}
107150
108151func (s * StorageFactory ) GetCollectionResources (ctx context.Context ) ([]* internal.CollectionResource , error ) {
@@ -116,3 +159,22 @@ func (s *StorageFactory) GetCollectionResources(ctx context.Context) ([]*interna
116159func (s * StorageFactory ) PrepareCluster (cluster string ) error {
117160 return nil
118161}
162+
163+ // GenerateTableFor return table name using gvr string
164+ func GenerateTableFor (gvr schema.GroupVersionResource ) string {
165+ if gvr .Group == "" {
166+ return fmt .Sprintf ("%s_%s" , gvr .Version , gvr .Resource )
167+ }
168+
169+ group := strings .ReplaceAll (gvr .Group , "." , "_" )
170+ return fmt .Sprintf ("%s_%s_%s" , group , gvr .Version , gvr .Resource )
171+ }
172+
173+ func (s * StorageFactory ) tableName (gvr schema.GroupVersionResource ) string {
174+ table := "resources"
175+ if s .DivisionPolicy == DivisionPolicyGroupVersionResource {
176+ table = GenerateTableFor (gvr )
177+ }
178+
179+ return table
180+ }
0 commit comments