UniStore: Fix DualWriter compare method (#94705)

* UniStore: Fix DualWriter compare method

Signed-off-by: Maicon Costa <maiconscosta@gmail.com>

---------

Signed-off-by: Maicon Costa <maiconscosta@gmail.com>
This commit is contained in:
maicon 2024-10-16 11:22:49 -03:00 committed by GitHub
parent 4a60f29709
commit c4f906f7fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 6 deletions

View File

@ -274,20 +274,18 @@ func Compare(storageObj, legacyObj runtime.Object) bool {
if storageObj == nil || legacyObj == nil {
return storageObj == nil && legacyObj == nil
}
return bytes.Equal(removeMeta(storageObj), removeMeta(legacyObj))
return bytes.Equal(extractSpec(storageObj), extractSpec(legacyObj))
}
func removeMeta(obj runtime.Object) []byte {
func extractSpec(obj runtime.Object) []byte {
cpy := obj.DeepCopyObject()
unstObj, err := defaultConverter.ToUnstructured(cpy)
if err != nil {
return nil
}
// we don't want to compare meta fields
delete(unstObj, "metadata")
delete(unstObj, "objectMeta")
jsonObj, err := json.Marshal(unstObj)
// we just want to compare the spec field
jsonObj, err := json.Marshal(unstObj["spec"])
if err != nil {
return nil
}

View File

@ -66,6 +66,7 @@ func TestSetDualWritingMode(t *testing.T) {
func TestCompare(t *testing.T) {
var exampleObjGen1 = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 1}, Spec: example.PodSpec{Hostname: "one"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
var exampleObjGen2 = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: example.PodSpec{Hostname: "one"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
var exampleObjGen3 = &example.Pod{TypeMeta: metav1.TypeMeta{Kind: "pod", APIVersion: "pods/v0"}, ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: example.PodSpec{Hostname: "one"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
var exampleObjDifferentTitle = &example.Pod{ObjectMeta: metav1.ObjectMeta{Generation: 2}, Spec: example.PodSpec{Hostname: "two"}, Status: example.PodStatus{StartTime: &metav1.Time{Time: time.Unix(0, 0)}}}
testCase := []struct {
@ -86,6 +87,12 @@ func TestCompare(t *testing.T) {
input2: exampleObjGen2,
expected: true,
},
{
name: "should return true when objects are the same, but different TypeMeta (kind and apiversion)",
input1: exampleObjGen1,
input2: exampleObjGen3,
expected: true,
},
{
name: "should return false when objects are different",
input1: exampleObjGen1,