From 66f7b8b1991faf065c08b9613a401a285895b511 Mon Sep 17 00:00:00 2001 From: shaofan-hs <133250733+shaofan-hs@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:28:12 +0800 Subject: [PATCH] fix: lost cluster infomation when failed to update service using multicluster (#49) --- multicluster/manager_test.go | 14 ++++++++++++++ multicluster/multi_cluster_client.go | 18 ++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/multicluster/manager_test.go b/multicluster/manager_test.go index 70c8def..928b455 100644 --- a/multicluster/manager_test.go +++ b/multicluster/manager_test.go @@ -327,6 +327,20 @@ var _ = Describe("multicluster", func() { err = clusterClient.Delete(clusterCtx, &configmap) Expect(err).NotTo(HaveOccurred()) + // Label should be preserved after deleting + val, ok := configmap.GetLabels()[clusterinfo.ClusterLabelKey] + Expect(ok).To(Equal(true)) + Expect(val).To(Equal("cluster2")) + + configmap.SetLabels(map[string]string{"foo": "bar"}) + err = clusterClient.Update(clusterCtx, &configmap) + Expect(err).To(HaveOccurred()) + + // Label should be preserved after updating + val, ok = configmap.GetLabels()[clusterinfo.ClusterLabelKey] + Expect(ok).To(Equal(true)) + Expect(val).To(Equal("cluster2")) + time.Sleep(300 * time.Millisecond) err = clusterClient.Get(clusterCtx, client.ObjectKey{ diff --git a/multicluster/multi_cluster_client.go b/multicluster/multi_cluster_client.go index 49a2468..536b57d 100644 --- a/multicluster/multi_cluster_client.go +++ b/multicluster/multi_cluster_client.go @@ -135,6 +135,7 @@ func (mcc *multiClusterClient) RemoveClusterClient(cluster string) { func (mcc *multiClusterClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) (err error) { var cluster string defer func() { + attachClusterToObjects(cluster, obj) metrics.NewClientCountMetrics(cluster, "Create", err) }() @@ -162,6 +163,7 @@ func (mcc *multiClusterClient) Create(ctx context.Context, obj client.Object, op func (mcc *multiClusterClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) (err error) { var cluster string defer func() { + attachClusterToObjects(cluster, obj) metrics.NewClientCountMetrics(cluster, "Delete", err).Inc() }() @@ -305,9 +307,7 @@ func (mcc *multiClusterClient) List(ctx context.Context, list client.ObjectList, func (mcc *multiClusterClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) (err error) { var cluster string defer func() { - if err == nil { - attachClusterToObjects(cluster, obj) - } + attachClusterToObjects(cluster, obj) metrics.NewClientCountMetrics(cluster, "Patch", err).Inc() }() @@ -335,9 +335,7 @@ func (mcc *multiClusterClient) Patch(ctx context.Context, obj client.Object, pat func (mcc *multiClusterClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) (err error) { var cluster string defer func() { - if err == nil { - attachClusterToObjects(cluster, obj) - } + attachClusterToObjects(cluster, obj) metrics.NewClientCountMetrics(cluster, "Update", err).Inc() }() @@ -388,9 +386,7 @@ type statusWriter struct { func (sw *statusWriter) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) (err error) { var cluster string defer func() { - if err == nil { - attachClusterToObjects(cluster, obj) - } + attachClusterToObjects(cluster, obj) metrics.NewClientCountMetrics(cluster, "StatusUpdate", err).Inc() }() @@ -415,9 +411,7 @@ func (sw *statusWriter) Update(ctx context.Context, obj client.Object, opts ...c func (sw *statusWriter) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) (err error) { var cluster string defer func() { - if err == nil { - attachClusterToObjects(cluster, obj) - } + attachClusterToObjects(cluster, obj) metrics.NewClientCountMetrics(cluster, "StatusPatch", err).Inc() }()