Skip to content

Commit 20ef402

Browse files
authored
Merge pull request #3626 from mjudeikis/mjudeikis/codecomment.nits
Fix cache server replication bug
2 parents 6ed8718 + 9e93b04 commit 20ef402

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/reconciler/cache/replication/replication_reconcile_unstructured.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ func ensureMeta(cacheObject *unstructured.Unstructured, localObject *unstructure
6161
}
6262
}()
6363
}
64+
if cacheObjUID, found := cacheObjMeta["uid"]; found {
65+
unstructured.RemoveNestedField(cacheObjMeta, "uid")
66+
defer func() {
67+
if err == nil {
68+
err = unstructured.SetNestedField(cacheObject.Object, cacheObjUID, "metadata", "uid")
69+
}
70+
}()
71+
}
6472
if cacheObjAnnotationsRaw, found := cacheObjMeta["annotations"]; found {
6573
cacheObjAnnotations, ok := cacheObjAnnotationsRaw.(map[string]interface{})
6674
if !ok {

pkg/reconciler/cache/replication/replication_reconcile_unstructured_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,25 @@ func TestEnsureUnstructuredMeta(t *testing.T) {
378378
}
379379
},
380380
},
381+
{
382+
name: "UID is preserved from cache object",
383+
cacheObjectMeta: metav1.ObjectMeta{UID: "cache-uid-123", ResourceVersion: "1", Annotations: map[string]string{"a": "b"}},
384+
localObjectMeta: metav1.ObjectMeta{UID: "local-uid-456", ResourceVersion: "2", Annotations: map[string]string{"a": "b", "foo": "bar"}},
385+
expectObjectMetaChanged: true,
386+
validateCacheObjectMeta: func(t *testing.T, cacheObjectMeta, localObjectMeta metav1.ObjectMeta) {
387+
t.Helper()
388+
389+
expectedCacheObjectMeta := metav1.ObjectMeta{UID: "cache-uid-123", ResourceVersion: "1", Annotations: map[string]string{"a": "b", "foo": "bar"}}
390+
if !reflect.DeepEqual(cacheObjectMeta, expectedCacheObjectMeta) {
391+
t.Errorf("received metadata differs from the expected one :\n%s", cmp.Diff(cacheObjectMeta, expectedCacheObjectMeta))
392+
}
393+
394+
expectedLocalObjectMeta := metav1.ObjectMeta{UID: "local-uid-456", ResourceVersion: "2", Annotations: map[string]string{"a": "b", "foo": "bar"}}
395+
if !reflect.DeepEqual(localObjectMeta, expectedLocalObjectMeta) {
396+
t.Errorf("local object's metadata mustn't be changed, diff :\n%s", cmp.Diff(localObjectMeta, expectedLocalObjectMeta))
397+
}
398+
},
399+
},
381400
}
382401

383402
for _, scenario := range scenarios {

0 commit comments

Comments
 (0)