Skip to content

Commit 66f7b8b

Browse files
authored
fix: lost cluster infomation when failed to update service using multicluster (#49)
1 parent 317e508 commit 66f7b8b

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

multicluster/manager_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,20 @@ var _ = Describe("multicluster", func() {
327327
err = clusterClient.Delete(clusterCtx, &configmap)
328328
Expect(err).NotTo(HaveOccurred())
329329

330+
// Label should be preserved after deleting
331+
val, ok := configmap.GetLabels()[clusterinfo.ClusterLabelKey]
332+
Expect(ok).To(Equal(true))
333+
Expect(val).To(Equal("cluster2"))
334+
335+
configmap.SetLabels(map[string]string{"foo": "bar"})
336+
err = clusterClient.Update(clusterCtx, &configmap)
337+
Expect(err).To(HaveOccurred())
338+
339+
// Label should be preserved after updating
340+
val, ok = configmap.GetLabels()[clusterinfo.ClusterLabelKey]
341+
Expect(ok).To(Equal(true))
342+
Expect(val).To(Equal("cluster2"))
343+
330344
time.Sleep(300 * time.Millisecond)
331345

332346
err = clusterClient.Get(clusterCtx, client.ObjectKey{

multicluster/multi_cluster_client.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func (mcc *multiClusterClient) RemoveClusterClient(cluster string) {
135135
func (mcc *multiClusterClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) (err error) {
136136
var cluster string
137137
defer func() {
138+
attachClusterToObjects(cluster, obj)
138139
metrics.NewClientCountMetrics(cluster, "Create", err)
139140
}()
140141

@@ -162,6 +163,7 @@ func (mcc *multiClusterClient) Create(ctx context.Context, obj client.Object, op
162163
func (mcc *multiClusterClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) (err error) {
163164
var cluster string
164165
defer func() {
166+
attachClusterToObjects(cluster, obj)
165167
metrics.NewClientCountMetrics(cluster, "Delete", err).Inc()
166168
}()
167169

@@ -305,9 +307,7 @@ func (mcc *multiClusterClient) List(ctx context.Context, list client.ObjectList,
305307
func (mcc *multiClusterClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) (err error) {
306308
var cluster string
307309
defer func() {
308-
if err == nil {
309-
attachClusterToObjects(cluster, obj)
310-
}
310+
attachClusterToObjects(cluster, obj)
311311
metrics.NewClientCountMetrics(cluster, "Patch", err).Inc()
312312
}()
313313

@@ -335,9 +335,7 @@ func (mcc *multiClusterClient) Patch(ctx context.Context, obj client.Object, pat
335335
func (mcc *multiClusterClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) (err error) {
336336
var cluster string
337337
defer func() {
338-
if err == nil {
339-
attachClusterToObjects(cluster, obj)
340-
}
338+
attachClusterToObjects(cluster, obj)
341339
metrics.NewClientCountMetrics(cluster, "Update", err).Inc()
342340
}()
343341

@@ -388,9 +386,7 @@ type statusWriter struct {
388386
func (sw *statusWriter) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) (err error) {
389387
var cluster string
390388
defer func() {
391-
if err == nil {
392-
attachClusterToObjects(cluster, obj)
393-
}
389+
attachClusterToObjects(cluster, obj)
394390
metrics.NewClientCountMetrics(cluster, "StatusUpdate", err).Inc()
395391
}()
396392

@@ -415,9 +411,7 @@ func (sw *statusWriter) Update(ctx context.Context, obj client.Object, opts ...c
415411
func (sw *statusWriter) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) (err error) {
416412
var cluster string
417413
defer func() {
418-
if err == nil {
419-
attachClusterToObjects(cluster, obj)
420-
}
414+
attachClusterToObjects(cluster, obj)
421415
metrics.NewClientCountMetrics(cluster, "StatusPatch", err).Inc()
422416
}()
423417

0 commit comments

Comments
 (0)