Skip to content

Commit

Permalink
chore: remove duplicate function (argoproj#15123)
Browse files Browse the repository at this point in the history
Signed-off-by: yyzxw <1020938856@qq.com>
  • Loading branch information
yyzxw authored Aug 30, 2023
1 parent d557be4 commit ef7f32e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 32 deletions.
22 changes: 5 additions & 17 deletions server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down
18 changes: 3 additions & 15 deletions server/applicationset/applicationset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions util/collections/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
58 changes: 58 additions & 0 deletions util/collections/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit ef7f32e

Please sign in to comment.