From ef7f32eb844739d8ae5b5feb987f32fa63024226 Mon Sep 17 00:00:00 2001 From: yyzxw <34639446+yyzxw@users.noreply.github.com> Date: Thu, 31 Aug 2023 03:55:11 +0800 Subject: [PATCH] chore: remove duplicate function (#15123) Signed-off-by: yyzxw <1020938856@qq.com> --- server/application/application.go | 22 +++------- server/applicationset/applicationset.go | 18 ++------ util/collections/maps.go | 13 ++++++ util/collections/maps_test.go | 58 +++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 32 deletions(-) diff --git a/server/application/application.go b/server/application/application.go index 9820614d898bb..afc2e0c734670 100644 --- a/server/application/application.go +++ b/server/application/application.go @@ -47,6 +47,7 @@ import ( "github.com/argoproj/argo-cd/v2/server/rbacpolicy" "github.com/argoproj/argo-cd/v2/util/argo" argoutil "github.com/argoproj/argo-cd/v2/util/argo" + "github.com/argoproj/argo-cd/v2/util/collections" "github.com/argoproj/argo-cd/v2/util/db" "github.com/argoproj/argo-cd/v2/util/env" "github.com/argoproj/argo-cd/v2/util/git" @@ -814,19 +815,6 @@ func (s *Server) validateAndUpdateApp(ctx context.Context, newApp *appv1.Applica return a, nil } -func mergeStringMaps(items ...map[string]string) map[string]string { - res := make(map[string]string) - for _, m := range items { - if m == nil { - continue - } - for k, v := range m { - res[k] = v - } - } - return res -} - var informerSyncTimeout = 2 * time.Second // waitSync is a helper to wait until the application informer cache is synced after create/update. @@ -864,8 +852,8 @@ func (s *Server) updateApp(app *appv1.Application, newApp *appv1.Application, ct for i := 0; i < 10; i++ { app.Spec = newApp.Spec if merge { - app.Labels = mergeStringMaps(app.Labels, newApp.Labels) - app.Annotations = mergeStringMaps(app.Annotations, newApp.Annotations) + app.Labels = collections.MergeStringMaps(app.Labels, newApp.Labels) + app.Annotations = collections.MergeStringMaps(app.Annotations, newApp.Annotations) } else { app.Labels = newApp.Labels app.Annotations = newApp.Annotations @@ -1700,8 +1688,8 @@ func isTheSelectedOne(currentNode *appv1.ResourceNode, q *application.Applicatio } for _, parentResource := range currentNode.ParentRefs { - //look up parentResource from resourceNodes - //then check if the parent isTheSelectedOne + // look up parentResource from resourceNodes + // then check if the parent isTheSelectedOne for _, resourceNode := range resourceNodes { if resourceNode.Namespace == parentResource.Namespace && resourceNode.Name == parentResource.Name && diff --git a/server/applicationset/applicationset.go b/server/applicationset/applicationset.go index 26de80530e93b..5dbff2d309a05 100644 --- a/server/applicationset/applicationset.go +++ b/server/applicationset/applicationset.go @@ -28,6 +28,7 @@ import ( servercache "github.com/argoproj/argo-cd/v2/server/cache" "github.com/argoproj/argo-cd/v2/server/rbacpolicy" "github.com/argoproj/argo-cd/v2/util/argo" + "github.com/argoproj/argo-cd/v2/util/collections" "github.com/argoproj/argo-cd/v2/util/db" "github.com/argoproj/argo-cd/v2/util/rbac" "github.com/argoproj/argo-cd/v2/util/security" @@ -214,19 +215,6 @@ func (s *Server) Create(ctx context.Context, q *applicationset.ApplicationSetCre return updated, nil } -func mergeStringMaps(items ...map[string]string) map[string]string { - res := make(map[string]string) - for _, m := range items { - if m == nil { - continue - } - for k, v := range m { - res[k] = v - } - } - return res -} - func (s *Server) updateAppSet(appset *v1alpha1.ApplicationSet, newAppset *v1alpha1.ApplicationSet, ctx context.Context, merge bool) (*v1alpha1.ApplicationSet, error) { if appset != nil && appset.Spec.Template.Spec.Project != newAppset.Spec.Template.Spec.Project { @@ -244,8 +232,8 @@ func (s *Server) updateAppSet(appset *v1alpha1.ApplicationSet, newAppset *v1alph for i := 0; i < 10; i++ { appset.Spec = newAppset.Spec if merge { - appset.Labels = mergeStringMaps(appset.Labels, newAppset.Labels) - appset.Annotations = mergeStringMaps(appset.Annotations, newAppset.Annotations) + appset.Labels = collections.MergeStringMaps(appset.Labels, newAppset.Labels) + appset.Annotations = collections.MergeStringMaps(appset.Annotations, newAppset.Annotations) } else { appset.Labels = newAppset.Labels appset.Annotations = newAppset.Annotations diff --git a/util/collections/maps.go b/util/collections/maps.go index a615f810c4c3a..d7a42943674b7 100644 --- a/util/collections/maps.go +++ b/util/collections/maps.go @@ -21,3 +21,16 @@ func StringMapsEqual(first map[string]string, second map[string]string) bool { } return reflect.DeepEqual(first, second) } + +func MergeStringMaps(items ...map[string]string) map[string]string { + res := make(map[string]string) + for _, m := range items { + if m == nil { + continue + } + for k, v := range m { + res[k] = v + } + } + return res +} diff --git a/util/collections/maps_test.go b/util/collections/maps_test.go index 9f6f31ec5b4d8..39cd7bf0b2b96 100644 --- a/util/collections/maps_test.go +++ b/util/collections/maps_test.go @@ -19,3 +19,61 @@ func TestStringMapsEqual(t *testing.T) { assert.False(t, StringMapsEqual(map[string]string{"foo": "bar"}, nil)) assert.False(t, StringMapsEqual(map[string]string{"foo": "bar"}, map[string]string{"foo": "bar1"})) } + +func TestMergeStringMaps(t *testing.T) { + tests := []struct { + name string + args []map[string]string + want map[string]string + }{ + { + name: "test single map", + args: []map[string]string{ + {"foo": "bar"}, + {"foo1": "bar1"}, + }, + want: map[string]string{ + "foo": "bar", + "foo1": "bar1", + }, + }, + { + name: "test contains nil map", + args: []map[string]string{ + {"foo": "bar"}, + nil, + {"foo1": "bar1"}, + }, + want: map[string]string{ + "foo": "bar", + "foo1": "bar1", + }, + }, + { + name: "test contains multiple maps", + args: []map[string]string{ + {"foo": "bar"}, + { + "foo1": "bar1", + "foo2": "bar2", + }, + { + "foo": "bar1", + "foo2": "bar2", + "foo3": "bar3", + }, + }, + want: map[string]string{ + "foo": "bar1", + "foo1": "bar1", + "foo2": "bar2", + "foo3": "bar3", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equalf(t, tt.want, MergeStringMaps(tt.args...), "MergeStringMaps(%v)", tt.args) + }) + } +}