mirror of
https://github.com/grafana/grafana.git
synced 2024-11-28 19:54:10 -06:00
Refactor enrichObj function (#89210)
This commit is contained in:
parent
e1145472c4
commit
1691d80412
@ -54,13 +54,12 @@ func (d *DualWriterMode1) Create(ctx context.Context, original runtime.Object, c
|
||||
ctx, cancel := context.WithTimeoutCause(ctx, time.Second*10, errors.New("storage create timeout"))
|
||||
defer cancel()
|
||||
|
||||
objStorage, errEnrichObj := enrichLegacyObject(original, createdCopy, true)
|
||||
if errEnrichObj != nil {
|
||||
if err := enrichLegacyObject(original, createdCopy, true); err != nil {
|
||||
cancel()
|
||||
}
|
||||
|
||||
startStorage := time.Now()
|
||||
_, errObjectSt := d.Storage.Create(ctx, objStorage, createValidation, options)
|
||||
_, errObjectSt := d.Storage.Create(ctx, createdCopy, createValidation, options)
|
||||
d.recordStorageDuration(errObjectSt != nil, mode1Str, options.Kind, method, startStorage)
|
||||
}()
|
||||
|
||||
@ -202,8 +201,7 @@ func (d *DualWriterMode1) Update(ctx context.Context, name string, objInfo rest.
|
||||
|
||||
// if the object is found, create a new updateWrapper with the object found
|
||||
if foundObj != nil {
|
||||
resCopy, err := enrichLegacyObject(foundObj, resCopy, false)
|
||||
if err != nil {
|
||||
if err := enrichLegacyObject(foundObj, resCopy, false); err != nil {
|
||||
log.Error(err, "could not enrich object")
|
||||
cancel()
|
||||
}
|
||||
|
@ -51,9 +51,8 @@ func (d *DualWriterMode2) Create(ctx context.Context, original runtime.Object, c
|
||||
}
|
||||
d.recordLegacyDuration(false, mode2Str, options.Kind, method, startLegacy)
|
||||
|
||||
createdLegacy, err := enrichLegacyObject(original, created, true)
|
||||
if err != nil {
|
||||
return createdLegacy, err
|
||||
if err := enrichLegacyObject(original, created, true); err != nil {
|
||||
return created, err
|
||||
}
|
||||
|
||||
startStorage := time.Now()
|
||||
@ -263,7 +262,7 @@ func (d *DualWriterMode2) Update(ctx context.Context, name string, objInfo rest.
|
||||
|
||||
// if the object is found, create a new updateWrapper with the object found
|
||||
if foundObj != nil {
|
||||
obj, err = enrichLegacyObject(foundObj, obj, false)
|
||||
err = enrichLegacyObject(foundObj, obj, false)
|
||||
if err != nil {
|
||||
return obj, false, err
|
||||
}
|
||||
@ -344,15 +343,15 @@ func parseList(legacyList []runtime.Object) (metainternalversion.ListOptions, ma
|
||||
return options, indexMap, nil
|
||||
}
|
||||
|
||||
func enrichLegacyObject(originalObj, returnedObj runtime.Object, created bool) (runtime.Object, error) {
|
||||
func enrichLegacyObject(originalObj, returnedObj runtime.Object, created bool) error {
|
||||
accessorReturned, err := meta.Accessor(returnedObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
accessorOriginal, err := meta.Accessor(originalObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
accessorReturned.SetLabels(accessorOriginal.GetLabels())
|
||||
@ -371,10 +370,10 @@ func enrichLegacyObject(originalObj, returnedObj runtime.Object, created bool) (
|
||||
if created {
|
||||
accessorReturned.SetResourceVersion("")
|
||||
accessorReturned.SetUID("")
|
||||
return returnedObj, nil
|
||||
return nil
|
||||
}
|
||||
// otherwise, we propagate the original RV and UID
|
||||
accessorReturned.SetResourceVersion(accessorOriginal.GetResourceVersion())
|
||||
accessorReturned.SetUID(accessorOriginal.GetUID())
|
||||
return returnedObj, nil
|
||||
return nil
|
||||
}
|
||||
|
@ -658,13 +658,13 @@ func TestEnrichReturnedObject(t *testing.T) {
|
||||
|
||||
for _, tt := range testCase {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
returned, err := enrichLegacyObject(tt.inputOriginal, tt.inputReturned, tt.isCreated)
|
||||
err := enrichLegacyObject(tt.inputOriginal, tt.inputReturned, tt.isCreated)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
accessorReturned, err := meta.Accessor(returned)
|
||||
accessorReturned, err := meta.Accessor(tt.inputReturned)
|
||||
assert.NoError(t, err)
|
||||
|
||||
accessorExpected, err := meta.Accessor(tt.expectedObject)
|
||||
|
Loading…
Reference in New Issue
Block a user