Cleanup resetStatus

Signed-off-by: Rafael Leal <rafaelealdias@gmail.com>
pull/4785/head
Rafael Leal 2022-05-23 17:53:32 -03:00
parent 131c8a920f
commit e0e3016efa
No known key found for this signature in database
GPG Key ID: 6A8C53E7546F9F5A
2 changed files with 8 additions and 19 deletions

View File

@ -1009,7 +1009,6 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso
}
if groupResource == kuberesource.PersistentVolumes {
resetStatus(obj)
switch {
case hasSnapshot(name, ctx.volumeSnapshots):
oldName := obj.GetName()
@ -1694,21 +1693,17 @@ func resetMetadata(obj *unstructured.Unstructured) (*unstructured.Unstructured,
return obj, nil
}
func resetStatus(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
delete(obj.UnstructuredContent(), "status")
return obj, nil
func resetStatus(obj *unstructured.Unstructured) {
unstructured.RemoveNestedField(obj.UnstructuredContent(), "status")
}
func resetMetadataAndStatus(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
newObj, err := resetMetadata(obj)
_, err := resetMetadata(obj)
if err != nil {
return nil, err
}
newObj, err = resetStatus(obj)
if err != nil {
return nil, err
}
return newObj, nil
resetStatus(obj)
return obj, nil
}
// addRestoreLabels labels the provided object with the restore name and the

View File

@ -2847,30 +2847,24 @@ func TestResetStatus(t *testing.T) {
tests := []struct {
name string
obj *unstructured.Unstructured
expectedErr bool
expectedRes *unstructured.Unstructured
}{
{
name: "no metadata don't cause error",
name: "no status don't cause error",
obj: &unstructured.Unstructured{},
expectedErr: false,
expectedRes: &unstructured.Unstructured{},
},
{
name: "remove status",
obj: NewTestUnstructured().WithMetadata().WithStatus().Unstructured,
expectedErr: false,
expectedRes: NewTestUnstructured().WithMetadata().Unstructured,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res, err := resetStatus(test.obj)
if assert.Equal(t, test.expectedErr, err != nil) {
assert.Equal(t, test.expectedRes, res)
}
resetStatus(test.obj)
assert.Equal(t, test.expectedRes, test.obj)
})
}
}