From c00eddf8e7bef973478eda5523d0c383895b809f Mon Sep 17 00:00:00 2001 From: Pierre HILBERT Date: Fri, 10 Nov 2023 17:33:29 +0100 Subject: [PATCH] Remove duplicate tags (#3740) * Removee duplicate tags * Adding changelog entry --- .../fragments/1697662209-duplicate-tags.yaml | 32 +++++++++++++++++++ internal/pkg/agent/cmd/enroll_cmd.go | 7 +++- internal/pkg/agent/cmd/enroll_cmd_test.go | 7 ++-- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 changelog/fragments/1697662209-duplicate-tags.yaml diff --git a/changelog/fragments/1697662209-duplicate-tags.yaml b/changelog/fragments/1697662209-duplicate-tags.yaml new file mode 100644 index 00000000000..710cc775366 --- /dev/null +++ b/changelog/fragments/1697662209-duplicate-tags.yaml @@ -0,0 +1,32 @@ +# Kind can be one of: +# - breaking-change: a change to previously-documented behavior +# - deprecation: functionality that is being removed in a later release +# - bug-fix: fixes a problem in a previous version +# - enhancement: extends functionality but does not break or fix existing behavior +# - feature: new functionality +# - known-issue: problems that we are aware of in a given version +# - security: impacts on the security of a product or a user’s deployment. +# - upgrade: important information for someone upgrading from a prior version +# - other: does not fit into any of the other categories +kind: enhancement + +# Change summary; a 80ish characters long description of the change. +summary: Remove duplicated tags when specified during the Agent enrollment. + +# Long description; in case the summary is not enough to describe the change +# this field accommodate a description without length limits. +# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. +description: "" + +# Affected component; a word indicating the component this changeset affects. +component: agent + +# PR URL; optional; the PR number that added the changeset. +# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. +# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. +# Please provide it if you are adding a fragment for a different PR. +pr: https://github.com/elastic/elastic-agent/pull/3740 + +# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). +# If not present is automatically filled by the tooling with the issue linked to the PR number. +issue: https://github.com/elastic/elastic-agent/issues/858 diff --git a/internal/pkg/agent/cmd/enroll_cmd.go b/internal/pkg/agent/cmd/enroll_cmd.go index 055807ef16c..37540ae5249 100644 --- a/internal/pkg/agent/cmd/enroll_cmd.go +++ b/internal/pkg/agent/cmd/enroll_cmd.go @@ -1066,10 +1066,15 @@ func expBackoffWithContext(ctx context.Context, init, max time.Duration) backoff func cleanTags(tags []string) []string { var r []string + // Create a map to store unique elements + seen := make(map[string]bool) for _, str := range tags { tag := strings.TrimSpace(str) if tag != "" { - r = append(r, tag) + if _, ok := seen[tag]; !ok { + seen[tag] = true + r = append(r, tag) + } } } return r diff --git a/internal/pkg/agent/cmd/enroll_cmd_test.go b/internal/pkg/agent/cmd/enroll_cmd_test.go index 189ad7b6563..0b1e7d5d4ee 100644 --- a/internal/pkg/agent/cmd/enroll_cmd_test.go +++ b/internal/pkg/agent/cmd/enroll_cmd_test.go @@ -351,9 +351,9 @@ func TestValidateArgs(t *testing.T) { require.Contains(t, cleanedTags, "production") }) - t.Run("comma separated tags are cleaned", func(t *testing.T) { + t.Run("comma separated tags and duplicated tags are cleaned", func(t *testing.T) { cmd := newEnrollCommandWithArgs([]string{}, streams) - err := cmd.Flags().Set("tag", "windows, production") + err := cmd.Flags().Set("tag", "windows, production, windows") require.NoError(t, err) args := buildEnrollmentFlags(cmd, url, enrolmentToken) require.Contains(t, args, "--tag") @@ -362,6 +362,9 @@ func TestValidateArgs(t *testing.T) { cleanedTags := cleanTags(args) require.Contains(t, cleanedTags, "windows") require.Contains(t, cleanedTags, "production") + // Validate that we remove the duplicates + require.Equal(t, len(args), 10) + require.Equal(t, len(cleanedTags), 7) }) t.Run("valid tag and empty tag", func(t *testing.T) {