mirror of
https://github.com/argoproj/argo-cd.git
synced 2026-02-20 01:28:45 +01:00
chore: replacing custom map util functions with golang std (#20311)
Signed-off-by: Daan Verstraten <daanverstraten@hotmail.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package template
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
@@ -18,7 +19,6 @@ import (
|
||||
rendmock "github.com/argoproj/argo-cd/v2/applicationset/utils/mocks"
|
||||
"github.com/argoproj/argo-cd/v2/pkg/apis/application"
|
||||
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
|
||||
"github.com/argoproj/argo-cd/v2/util/collections"
|
||||
)
|
||||
|
||||
func TestGenerateApplications(t *testing.T) {
|
||||
@@ -344,7 +344,7 @@ func TestGenerateAppsUsingPullRequestGenerator(t *testing.T) {
|
||||
assert.EqualValues(t, cases.expectedApp[0].ObjectMeta.Name, gotApp[0].ObjectMeta.Name)
|
||||
assert.EqualValues(t, cases.expectedApp[0].Spec.Source.TargetRevision, gotApp[0].Spec.Source.TargetRevision)
|
||||
assert.EqualValues(t, cases.expectedApp[0].Spec.Destination.Namespace, gotApp[0].Spec.Destination.Namespace)
|
||||
assert.True(t, collections.StringMapsEqual(cases.expectedApp[0].ObjectMeta.Labels, gotApp[0].ObjectMeta.Labels))
|
||||
assert.True(t, maps.Equal(cases.expectedApp[0].ObjectMeta.Labels, gotApp[0].ObjectMeta.Labels))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package v1alpha1
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -36,7 +37,6 @@ import (
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"github.com/argoproj/argo-cd/v2/common"
|
||||
"github.com/argoproj/argo-cd/v2/util/collections"
|
||||
"github.com/argoproj/argo-cd/v2/util/env"
|
||||
"github.com/argoproj/argo-cd/v2/util/helm"
|
||||
utilhttp "github.com/argoproj/argo-cd/v2/util/http"
|
||||
@@ -1939,11 +1939,11 @@ func (c *Cluster) Equals(other *Cluster) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if !collections.StringMapsEqual(c.Annotations, other.Annotations) {
|
||||
if !maps.Equal(c.Annotations, other.Annotations) {
|
||||
return false
|
||||
}
|
||||
|
||||
if !collections.StringMapsEqual(c.Labels, other.Labels) {
|
||||
if !maps.Equal(c.Labels, other.Labels) {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -931,8 +931,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 = collections.MergeStringMaps(app.Labels, newApp.Labels)
|
||||
app.Annotations = collections.MergeStringMaps(app.Annotations, newApp.Annotations)
|
||||
app.Labels = collections.Merge(app.Labels, newApp.Labels)
|
||||
app.Annotations = collections.Merge(app.Annotations, newApp.Annotations)
|
||||
} else {
|
||||
app.Labels = newApp.Labels
|
||||
app.Annotations = newApp.Annotations
|
||||
|
||||
@@ -300,8 +300,8 @@ func (s *Server) updateAppSet(appset *v1alpha1.ApplicationSet, newAppset *v1alph
|
||||
for i := 0; i < 10; i++ {
|
||||
appset.Spec = newAppset.Spec
|
||||
if merge {
|
||||
appset.Labels = collections.MergeStringMaps(appset.Labels, newAppset.Labels)
|
||||
appset.Annotations = collections.MergeStringMaps(appset.Annotations, newAppset.Annotations)
|
||||
appset.Labels = collections.Merge(appset.Labels, newAppset.Labels)
|
||||
appset.Annotations = collections.Merge(appset.Annotations, newAppset.Annotations)
|
||||
} else {
|
||||
appset.Labels = newAppset.Labels
|
||||
appset.Annotations = newAppset.Annotations
|
||||
|
||||
@@ -1,36 +1,16 @@
|
||||
package collections
|
||||
|
||||
import "reflect"
|
||||
import "maps"
|
||||
|
||||
// CopyStringMap creates copy of a string map
|
||||
func CopyStringMap(in map[string]string) map[string]string {
|
||||
out := map[string]string{}
|
||||
for k, v := range in {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// StringMapsEqual compares two string maps assuming that nil and empty map are considered equal
|
||||
func StringMapsEqual(first map[string]string, second map[string]string) bool {
|
||||
if first == nil {
|
||||
first = map[string]string{}
|
||||
}
|
||||
if second == nil {
|
||||
second = map[string]string{}
|
||||
}
|
||||
return reflect.DeepEqual(first, second)
|
||||
}
|
||||
|
||||
func MergeStringMaps(items ...map[string]string) map[string]string {
|
||||
res := make(map[string]string)
|
||||
// Merge takes a collection of maps and returns a single map, where by items are merged of all given sets.
|
||||
// If any keys overlap, Then the last specified key takes precedence.
|
||||
// Example:
|
||||
//
|
||||
// data := collections.Merge(map[string]string{"foo": "bar1", "baz": "bar1"}, map[string]string{"foo": "bar2"}) // returns: map[string]string{"foo": "bar2", "empty": "bar1"}
|
||||
func Merge[K comparable, V any](items ...map[K]V) map[K]V {
|
||||
res := make(map[K]V)
|
||||
for _, m := range items {
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
for k, v := range m {
|
||||
res[k] = v
|
||||
}
|
||||
maps.Copy(res, m)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -6,20 +6,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCopyStringMap(t *testing.T) {
|
||||
out := CopyStringMap(map[string]string{"foo": "bar"})
|
||||
assert.Equal(t, map[string]string{"foo": "bar"}, out)
|
||||
}
|
||||
|
||||
func TestStringMapsEqual(t *testing.T) {
|
||||
assert.True(t, StringMapsEqual(nil, nil))
|
||||
assert.True(t, StringMapsEqual(nil, map[string]string{}))
|
||||
assert.True(t, StringMapsEqual(map[string]string{}, nil))
|
||||
assert.True(t, StringMapsEqual(map[string]string{"foo": "bar"}, map[string]string{"foo": "bar"}))
|
||||
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
|
||||
@@ -73,7 +59,7 @@ func TestMergeStringMaps(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.want, MergeStringMaps(tt.args...), "MergeStringMaps(%v)", tt.args)
|
||||
assert.Equalf(t, tt.want, Merge(tt.args...), "Merge[string, string](%v)", tt.args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -20,7 +21,6 @@ import (
|
||||
|
||||
"github.com/argoproj/argo-cd/v2/common"
|
||||
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
|
||||
"github.com/argoproj/argo-cd/v2/util/collections"
|
||||
"github.com/argoproj/argo-cd/v2/util/settings"
|
||||
)
|
||||
|
||||
@@ -405,12 +405,12 @@ func SecretToCluster(s *apiv1.Secret) (*appv1.Cluster, error) {
|
||||
// copy labels and annotations excluding system ones
|
||||
labels := map[string]string{}
|
||||
if s.Labels != nil {
|
||||
labels = collections.CopyStringMap(s.Labels)
|
||||
labels = maps.Clone(s.Labels)
|
||||
delete(labels, common.LabelKeySecretType)
|
||||
}
|
||||
annotations := map[string]string{}
|
||||
if s.Annotations != nil {
|
||||
annotations = collections.CopyStringMap(s.Annotations)
|
||||
annotations = maps.Clone(s.Annotations)
|
||||
// delete system annotations
|
||||
delete(annotations, apiv1.LastAppliedConfigAnnotation)
|
||||
delete(annotations, common.AnnotationKeyManagedBy)
|
||||
|
||||
Reference in New Issue
Block a user