Refactor enrichObj function (#89210)

This commit is contained in:
Leonor Oliveira 2024-06-14 12:13:59 +01:00 committed by GitHub
parent e1145472c4
commit 1691d80412
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 16 deletions

View File

@ -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")) ctx, cancel := context.WithTimeoutCause(ctx, time.Second*10, errors.New("storage create timeout"))
defer cancel() defer cancel()
objStorage, errEnrichObj := enrichLegacyObject(original, createdCopy, true) if err := enrichLegacyObject(original, createdCopy, true); err != nil {
if errEnrichObj != nil {
cancel() cancel()
} }
startStorage := time.Now() 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) 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 the object is found, create a new updateWrapper with the object found
if foundObj != nil { if foundObj != nil {
resCopy, err := enrichLegacyObject(foundObj, resCopy, false) if err := enrichLegacyObject(foundObj, resCopy, false); err != nil {
if err != nil {
log.Error(err, "could not enrich object") log.Error(err, "could not enrich object")
cancel() cancel()
} }

View File

@ -51,9 +51,8 @@ func (d *DualWriterMode2) Create(ctx context.Context, original runtime.Object, c
} }
d.recordLegacyDuration(false, mode2Str, options.Kind, method, startLegacy) d.recordLegacyDuration(false, mode2Str, options.Kind, method, startLegacy)
createdLegacy, err := enrichLegacyObject(original, created, true) if err := enrichLegacyObject(original, created, true); err != nil {
if err != nil { return created, err
return createdLegacy, err
} }
startStorage := time.Now() 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 the object is found, create a new updateWrapper with the object found
if foundObj != nil { if foundObj != nil {
obj, err = enrichLegacyObject(foundObj, obj, false) err = enrichLegacyObject(foundObj, obj, false)
if err != nil { if err != nil {
return obj, false, err return obj, false, err
} }
@ -344,15 +343,15 @@ func parseList(legacyList []runtime.Object) (metainternalversion.ListOptions, ma
return options, indexMap, nil 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) accessorReturned, err := meta.Accessor(returnedObj)
if err != nil { if err != nil {
return nil, err return err
} }
accessorOriginal, err := meta.Accessor(originalObj) accessorOriginal, err := meta.Accessor(originalObj)
if err != nil { if err != nil {
return nil, err return err
} }
accessorReturned.SetLabels(accessorOriginal.GetLabels()) accessorReturned.SetLabels(accessorOriginal.GetLabels())
@ -371,10 +370,10 @@ func enrichLegacyObject(originalObj, returnedObj runtime.Object, created bool) (
if created { if created {
accessorReturned.SetResourceVersion("") accessorReturned.SetResourceVersion("")
accessorReturned.SetUID("") accessorReturned.SetUID("")
return returnedObj, nil return nil
} }
// otherwise, we propagate the original RV and UID // otherwise, we propagate the original RV and UID
accessorReturned.SetResourceVersion(accessorOriginal.GetResourceVersion()) accessorReturned.SetResourceVersion(accessorOriginal.GetResourceVersion())
accessorReturned.SetUID(accessorOriginal.GetUID()) accessorReturned.SetUID(accessorOriginal.GetUID())
return returnedObj, nil return nil
} }

View File

@ -658,13 +658,13 @@ func TestEnrichReturnedObject(t *testing.T) {
for _, tt := range testCase { for _, tt := range testCase {
t.Run(tt.name, func(t *testing.T) { 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 { if tt.wantErr {
assert.Error(t, err) assert.Error(t, err)
return return
} }
accessorReturned, err := meta.Accessor(returned) accessorReturned, err := meta.Accessor(tt.inputReturned)
assert.NoError(t, err) assert.NoError(t, err)
accessorExpected, err := meta.Accessor(tt.expectedObject) accessorExpected, err := meta.Accessor(tt.expectedObject)