Skip to content

Commit 9f61116

Browse files
Merge pull request #2122 from p0lyn0mial/cache-replication-shard-annotation-creation
🌱 cache: replication controller: attach the shard annotation during object creation
2 parents 7f60187 + 82a8f63 commit 9f61116

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

pkg/reconciler/cache/replication/replication_reconcile_apiexports_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestReconcileAPIExports(t *testing.T) {
7575
ts.Fatalf("failed to convert unstructured to APIExport: %v", err)
7676
}
7777

78-
expectedApiExport := newAPIExport("foo")
78+
expectedApiExport := newAPIExportWithShardAnnotation("foo")
7979
if !equality.Semantic.DeepEqual(cacheApiExportFromUnstructured, expectedApiExport) {
8080
ts.Errorf("unexpected ApiExport was creaetd:\n%s", cmp.Diff(cacheApiExportFromUnstructured, expectedApiExport))
8181
}
@@ -99,7 +99,7 @@ func TestReconcileAPIExports(t *testing.T) {
9999
return apiExport
100100
}(),
101101
},
102-
initialCacheApiExports: []runtime.Object{newAPIExport("foo")},
102+
initialCacheApiExports: []runtime.Object{newAPIExportWithShardAnnotation("foo")},
103103
initCacheFakeClientWithInitialApiExports: true,
104104
reconcileKey: "root|foo",
105105
validateFunc: func(ts *testing.T, cacheClientActions []clientgotesting.Action, localClientActions []clientgotesting.Action, targetClusterCacheClient, targetClusterLocalClient logicalcluster.Name) {
@@ -127,7 +127,7 @@ func TestReconcileAPIExports(t *testing.T) {
127127
},
128128
{
129129
name: "case 2: cached object is removed when local object was not found",
130-
initialCacheApiExports: []runtime.Object{newAPIExport("foo")},
130+
initialCacheApiExports: []runtime.Object{newAPIExportWithShardAnnotation("foo")},
131131
initCacheFakeClientWithInitialApiExports: true,
132132
reconcileKey: "root|foo",
133133
validateFunc: func(ts *testing.T, cacheClientActions []clientgotesting.Action, localClientActions []clientgotesting.Action, targetClusterCacheClient, targetClusterLocalClient logicalcluster.Name) {
@@ -176,7 +176,7 @@ func TestReconcileAPIExports(t *testing.T) {
176176
return apiExport
177177
}(),
178178
},
179-
initialCacheApiExports: []runtime.Object{newAPIExport("foo")},
179+
initialCacheApiExports: []runtime.Object{newAPIExportWithShardAnnotation("foo")},
180180
initCacheFakeClientWithInitialApiExports: true,
181181
reconcileKey: "root|foo",
182182
validateFunc: func(ts *testing.T, cacheClientActions []clientgotesting.Action, localClientActions []clientgotesting.Action, targetClusterCacheClient, targetClusterLocalClient logicalcluster.Name) {
@@ -196,7 +196,7 @@ func TestReconcileAPIExports(t *testing.T) {
196196
ts.Fatalf("failed to convert unstructured to APIExport: %v", err)
197197
}
198198

199-
expectedApiExport := newAPIExport("foo")
199+
expectedApiExport := newAPIExportWithShardAnnotation("foo")
200200
expectedApiExport.Labels["fooLabel"] = "fooLabelVal"
201201
if !equality.Semantic.DeepEqual(cacheApiExportFromUnstructured, expectedApiExport) {
202202
ts.Errorf("unexpected update to the ApiExport:\n%s", cmp.Diff(cacheApiExportFromUnstructured, expectedApiExport))
@@ -219,7 +219,7 @@ func TestReconcileAPIExports(t *testing.T) {
219219
return apiExport
220220
}(),
221221
},
222-
initialCacheApiExports: []runtime.Object{newAPIExport("foo")},
222+
initialCacheApiExports: []runtime.Object{newAPIExportWithShardAnnotation("foo")},
223223
initCacheFakeClientWithInitialApiExports: true,
224224
reconcileKey: "root|foo",
225225
validateFunc: func(ts *testing.T, cacheClientActions []clientgotesting.Action, localClientActions []clientgotesting.Action, targetClusterCacheClient, targetClusterLocalClient logicalcluster.Name) {
@@ -239,7 +239,7 @@ func TestReconcileAPIExports(t *testing.T) {
239239
ts.Fatalf("failed to convert unstructured to APIExport: %v", err)
240240
}
241241

242-
expectedApiExport := newAPIExport("foo")
242+
expectedApiExport := newAPIExportWithShardAnnotation("foo")
243243
expectedApiExport.Spec.PermissionClaims = []apisv1alpha1.PermissionClaim{{GroupResource: apisv1alpha1.GroupResource{}, IdentityHash: "abc"}}
244244
if !equality.Semantic.DeepEqual(cacheApiExportFromUnstructured, expectedApiExport) {
245245
ts.Errorf("unexpected update to the ApiExport:\n%s", cmp.Diff(cacheApiExportFromUnstructured, expectedApiExport))
@@ -262,7 +262,7 @@ func TestReconcileAPIExports(t *testing.T) {
262262
return apiExport
263263
}(),
264264
},
265-
initialCacheApiExports: []runtime.Object{newAPIExport("foo")},
265+
initialCacheApiExports: []runtime.Object{newAPIExportWithShardAnnotation("foo")},
266266
initCacheFakeClientWithInitialApiExports: true,
267267
reconcileKey: "root|foo",
268268
validateFunc: func(ts *testing.T, cacheClientActions []clientgotesting.Action, localClientActions []clientgotesting.Action, targetClusterCacheClient, targetClusterLocalClient logicalcluster.Name) {
@@ -282,7 +282,7 @@ func TestReconcileAPIExports(t *testing.T) {
282282
ts.Fatalf("failed to convert unstructured to APIExport: %v", err)
283283
}
284284

285-
expectedApiExport := newAPIExport("foo")
285+
expectedApiExport := newAPIExportWithShardAnnotation("foo")
286286
expectedApiExport.Status.VirtualWorkspaces = []apisv1alpha1.VirtualWorkspace{{URL: "https://acme.dev"}}
287287
if !equality.Semantic.DeepEqual(cacheApiExportFromUnstructured, expectedApiExport) {
288288
ts.Errorf("unexpected update to the ApiExport:\n%s", cmp.Diff(cacheApiExportFromUnstructured, expectedApiExport))
@@ -342,7 +342,6 @@ func newAPIExport(name string) *apisv1alpha1.APIExport {
342342
Labels: map[string]string{},
343343
Annotations: map[string]string{
344344
logicalcluster.AnnotationKey: "root",
345-
"kcp.dev/shard": "amber",
346345
},
347346
Name: name,
348347
},
@@ -355,6 +354,12 @@ func newAPIExport(name string) *apisv1alpha1.APIExport {
355354
}
356355
}
357356

357+
func newAPIExportWithShardAnnotation(name string) *apisv1alpha1.APIExport {
358+
apiExport := newAPIExport(name)
359+
apiExport.Annotations["kcp.dev/shard"] = "amber"
360+
return apiExport
361+
}
362+
358363
func newFakeKcpClusterClient(ds *dynamicfake.FakeDynamicClient) *fakeKcpClusterClient {
359364
return &fakeKcpClusterClient{fakeDs: ds}
360365
}

pkg/reconciler/cache/replication/replication_reconcile_unstructured.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ func (c *controller) reconcileUnstructuredObjects(ctx context.Context, cluster l
5656
// TODO: in the future the original RV will have to be stored in an annotation (?)
5757
// so that the clients that need to modify the original/local object can do it
5858
localObject.SetResourceVersion("")
59+
annotations := localObject.GetAnnotations()
60+
if annotations == nil {
61+
annotations = map[string]string{}
62+
}
63+
annotations[genericrequest.AnnotationKey] = c.shardName
64+
localObject.SetAnnotations(annotations)
5965
_, err := c.dynamicCacheClient.Cluster(cluster).Resource(*gvr).Namespace(localObject.GetNamespace()).Create(ctx, localObject, metav1.CreateOptions{})
6066
return err
6167
}

0 commit comments

Comments
 (0)