@@ -75,15 +75,12 @@ func (s *ResourceStorage) Create(ctx context.Context, cluster string, obj runtim
7575 condition := map [string ]interface {}{
7676 "namespace" : metaobj .GetNamespace (),
7777 "name" : metaobj .GetName (),
78- "group" : s .storageGroupResource .Group ,
79- "version" : s .storageVersion .Version ,
80- "resource" : s .storageGroupResource .Resource ,
8178 "deleted" : true ,
8279 }
8380 if cluster != "" {
8481 condition ["cluster" ] = cluster
8582 }
86- dbResult := s .db .Model (& Resource {}).Where (condition ).Delete (& Resource {})
83+ dbResult := s .db .Model (& Resource {}).Table ( GetTable ( newGvr ( s ))). Where (condition ).Delete (& Resource {})
8784 if dbResult .Error != nil {
8885 err = InterpretResourceDBError (cluster , metaobj .GetName (), dbResult .Error )
8986 return fmt .Errorf ("[Create]: Object %s/%s has been created failed in step one, err: %v" , metaobj .GetName (), metaobj .GetNamespace (), err )
@@ -118,7 +115,7 @@ func (s *ResourceStorage) Create(ctx context.Context, cluster string, obj runtim
118115 resource .DeletedAt = sql.NullTime {Time : deletedAt .Time , Valid : true }
119116 }
120117
121- result := s .db .WithContext (ctx ).Create (& resource )
118+ result := s .db .WithContext (ctx ).Table ( GetTable ( newGvr ( s ))). Create (& resource )
122119 return InterpretResourceDBError (cluster , metaobj .GetName (), result .Error )
123120}
124121
@@ -163,23 +160,17 @@ func (s *ResourceStorage) Update(ctx context.Context, cluster string, obj runtim
163160 updatedResource ["deleted_at" ] = sql.NullTime {Time : deletedAt .Time , Valid : true }
164161 }
165162
166- result := s .db .WithContext (ctx ).Model (& Resource {}).Where (map [string ]interface {}{
163+ result := s .db .WithContext (ctx ).Table ( GetTable ( newGvr ( s ))). Model (& Resource {}).Where (map [string ]interface {}{
167164 "cluster" : cluster ,
168- "group" : s .storageGroupResource .Group ,
169- "version" : s .storageVersion .Version ,
170- "resource" : s .storageGroupResource .Resource ,
171165 "namespace" : metaobj .GetNamespace (),
172166 "name" : metaobj .GetName (),
173167 }).Updates (updatedResource )
174168 return InterpretResourceDBError (cluster , metaobj .GetName (), result .Error )
175169}
176170
177171func (s * ResourceStorage ) deleteObject (cluster , namespace , name string ) * gorm.DB {
178- return s .db .Model (& Resource {}).Where (map [string ]interface {}{
172+ return s .db .Table ( GetTable ( newGvr ( s ))). Model (& Resource {}).Where (map [string ]interface {}{
179173 "cluster" : cluster ,
180- "group" : s .storageGroupResource .Group ,
181- "version" : s .storageVersion .Version ,
182- "resource" : s .storageGroupResource .Resource ,
183174 "namespace" : namespace ,
184175 "name" : name ,
185176 }).Delete (& Resource {})
@@ -212,32 +203,26 @@ func (s *ResourceStorage) Delete(ctx context.Context, cluster string, obj runtim
212203 condition := map [string ]interface {}{
213204 "cluster" : cluster ,
214205 "namespace" : metaobj .GetNamespace (),
215- "group" : s .storageGroupResource .Group ,
216- "version" : s .storageVersion .Version ,
217- "resource" : s .storageGroupResource .Resource ,
218206 }
219207 if metaobj .GetName () != "" {
220208 condition ["name" ] = metaobj .GetName ()
221209 }
222210
223- result := s .db .WithContext (ctx ).Model (& Resource {}).Where (condition ).Updates (updatedResource )
211+ result := s .db .WithContext (ctx ).Table ( GetTable ( newGvr ( s ))). Model (& Resource {}).Where (condition ).Updates (updatedResource )
224212 return InterpretResourceDBError (cluster , metaobj .GetName (), result .Error )
225213}
226214
227215func (s * ResourceStorage ) genGetObjectQuery (ctx context.Context , cluster , namespace , name string ) * gorm.DB {
228216 condition := map [string ]interface {}{
229217 "namespace" : namespace ,
230218 "name" : name ,
231- "group" : s .storageGroupResource .Group ,
232- "version" : s .storageVersion .Version ,
233- "resource" : s .storageGroupResource .Resource ,
234219 "deleted" : false ,
235220 }
236221
237222 if cluster != "" {
238223 condition ["cluster" ] = cluster
239224 }
240- return s .db .WithContext (ctx ).Model (& Resource {}).Select ("cluster_resource_version, object" ).Where (condition )
225+ return s .db .WithContext (ctx ).Table ( GetTable ( newGvr ( s ))). Model (& Resource {}).Select ("cluster_resource_version, object" ).Where (condition )
241226}
242227
243228func (s * ResourceStorage ) GetObj (ctx context.Context , cluster , namespace , name string ) (runtime.Object , error ) {
@@ -246,12 +231,9 @@ func (s *ResourceStorage) GetObj(ctx context.Context, cluster, namespace, name s
246231 "namespace" : namespace ,
247232 "name" : name ,
248233 "cluster" : cluster ,
249- "group" : s .storageGroupResource .Group ,
250- "version" : s .storageVersion .Version ,
251- "resource" : s .storageGroupResource .Resource ,
252234 }
253235
254- result := s .db .WithContext (ctx ).Model (& Resource {}).
236+ result := s .db .WithContext (ctx ).Table ( GetTable ( newGvr ( s ))). Model (& Resource {}).
255237 Select ("cluster_resource_version, object" ).Where (condition ).First (& resource )
256238 if result .Error != nil {
257239 return nil , InterpretResourceDBError (cluster , namespace + "/" + name , result .Error )
@@ -269,19 +251,37 @@ func (s *ResourceStorage) GetObj(ctx context.Context, cluster, namespace, name s
269251 return obj , nil
270252}
271253
254+ func (s * ResourceStorage ) GenGetObjectQuery (ctx context.Context , cluster , namespace , name string ) * gorm.DB {
255+ condition := map [string ]interface {}{
256+ "namespace" : namespace ,
257+ "name" : name ,
258+ "deleted" : false ,
259+ }
260+
261+ if cluster != "" {
262+ condition ["cluster" ] = cluster
263+ }
264+ return s .db .WithContext (ctx ).Table (GetTable (newGvr (s ))).Model (& Resource {}).Select ("cluster_resource_version, object" ).Where (condition )
265+ }
266+
272267func (s * ResourceStorage ) Get (ctx context.Context , cluster , namespace , name string , into runtime.Object ) error {
273- var objects [][] byte
274- if result := s .genGetObjectQuery (ctx , cluster , namespace , name ).First (& objects ); result .Error != nil {
268+ var resource Resource
269+ if result := s .GenGetObjectQuery (ctx , cluster , namespace , name ).First (& resource ); result .Error != nil {
275270 return InterpretResourceDBError (cluster , namespace + "/" + name , result .Error )
276271 }
277272
278- obj , _ , err := s .codec .Decode (objects [ 0 ] , nil , into )
273+ obj , _ , err := s .codec .Decode (resource . Object , nil , into )
279274 if err != nil {
280275 return err
281276 }
282277 if obj != into {
283278 return fmt .Errorf ("Failed to decode resource, into is %T" , into )
284279 }
280+ metaObj , err := meta .Accessor (obj )
281+ if err != nil {
282+ return err
283+ }
284+ metaObj .SetResourceVersion (utils .ParseInt642Str (resource .ClusterResourceVersion ))
285285 return nil
286286}
287287
@@ -294,14 +294,11 @@ func (s *ResourceStorage) genListObjectsQuery(ctx context.Context, opts *interna
294294 var condition map [string ]interface {}
295295 if ! isAll {
296296 condition = map [string ]interface {}{
297- "group" : s .storageGroupResource .Group ,
298- "version" : s .storageVersion .Version ,
299- "resource" : s .storageGroupResource .Resource ,
300- "deleted" : false ,
297+ "deleted" : false ,
301298 }
302299 }
303300
304- query := s .db .WithContext (ctx ).Model (& Resource {}).Where (condition )
301+ query := s .db .WithContext (ctx ).Table ( GetTable ( newGvr ( s ))). Model (& Resource {}).Where (condition )
305302 offset , amount , query , err := applyListOptionsToResourceQuery (s .db , query , opts )
306303 return offset , amount , query , result , err
307304}
@@ -310,11 +307,9 @@ func (s *ResourceStorage) genListQuery(ctx context.Context, newfunc func() runti
310307 var result [][]byte
311308
312309 condition := map [string ]interface {}{
313- "group" : s .storageGroupResource .Group ,
314- "version" : s .storageVersion .Version ,
315- "resource" : s .storageGroupResource .Resource ,
310+ "deleted" : false ,
316311 }
317- query := s .db .WithContext (ctx ).Model (& Resource {}).Select ("object" ).Where (condition )
312+ query := s .db .WithContext (ctx ).Table ( GetTable ( newGvr ( s ))). Model (& Resource {}).Select ("object" ).Where (condition )
318313 _ , _ , query , err := applyListOptionsToResourceQuery (s .db , query , opts )
319314 if err != nil {
320315 return nil , err
@@ -531,6 +526,14 @@ func (s *ResourceStorage) fetchInitEvents(ctx context.Context, rv string, newfun
531526 }
532527}
533528
529+ func newGvr (s * ResourceStorage ) schema.GroupVersionResource {
530+ return schema.GroupVersionResource {
531+ Group : s .storageGroupResource .Group ,
532+ Version : s .storageVersion .Version ,
533+ Resource : s .storageGroupResource .Resource ,
534+ }
535+ }
536+
534537func getObjectListAndMaxCrv (objList []Object , onlyMetada bool ) ([]Object , []string , string , error ) {
535538 crvs := make ([]string , 0 , len (objList ))
536539 var maxCrv int64 = 0
@@ -576,12 +579,7 @@ func getObjectListAndMaxCrv(objList []Object, onlyMetada bool) ([]Object, []stri
576579func (s * ResourceStorage ) GetMaxCrv (ctx context.Context ) (string , error ) {
577580 maxCrv := "0"
578581 var metadataList ResourceMetadataList
579- condition := map [string ]interface {}{
580- "group" : s .storageGroupResource .Group ,
581- "version" : s .storageVersion .Version ,
582- "resource" : s .storageGroupResource .Resource ,
583- }
584- result := s .db .WithContext (ctx ).Model (& Resource {}).Select ("cluster_resource_version" ).Where (condition ).Order ("cluster_resource_version DESC" ).Limit (1 ).Find (& metadataList )
582+ result := s .db .WithContext (ctx ).Table (GetTable (newGvr (s ))).Model (& Resource {}).Select ("cluster_resource_version" ).Order ("cluster_resource_version DESC" ).Limit (1 ).Find (& metadataList )
585583 if result .Error != nil {
586584 return maxCrv , InterpretResourceDBError ("" , s .storageGroupResource .Resource , result .Error )
587585 }
@@ -611,15 +609,12 @@ func (s *ResourceStorage) PublishEvent(ctx context.Context, wc *watchcomponents.
611609 }
612610
613611 condition := map [string ]interface {}{
614- "group" : s .storageGroupResource .Group ,
615- "version" : s .storageVersion .Version ,
616- "resource" : s .storageGroupResource .Resource ,
617612 "cluster" : wc .Cluster ,
618613 "namespace" : metaObj .GetNamespace (),
619614 "name" : metaObj .GetName (),
620615 }
621616
622- s .db .WithContext (ctx ).Model (& Resource {}).Where (condition ).Updates (updatedResource )
617+ s .db .WithContext (ctx ).Table ( GetTable ( newGvr ( s ))). Model (& Resource {}).Where (condition ).Updates (updatedResource )
623618}
624619
625620func (s * ResourceStorage ) GenCrv2Event (event * watch.Event ) {
0 commit comments