Skip to content

Commit

Permalink
fix: lost cluster infomation when failed to update service using mult…
Browse files Browse the repository at this point in the history
…icluster (#49)
  • Loading branch information
shaofan-hs authored Jun 14, 2024
1 parent 317e508 commit 66f7b8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
14 changes: 14 additions & 0 deletions multicluster/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
18 changes: 6 additions & 12 deletions multicluster/multi_cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}()

Expand Down Expand Up @@ -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()
}()

Expand Down Expand Up @@ -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()
}()

Expand Down Expand Up @@ -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()
}()

Expand Down Expand Up @@ -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()
}()

Expand All @@ -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()
}()

Expand Down

0 comments on commit 66f7b8b

Please sign in to comment.