Skip to content

Commit

Permalink
Remove duplicate tags (#3740)
Browse files Browse the repository at this point in the history
* Removee duplicate tags

* Adding changelog entry
  • Loading branch information
pierrehilbert authored Nov 10, 2023
1 parent 029a65c commit c00eddf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
32 changes: 32 additions & 0 deletions changelog/fragments/1697662209-duplicate-tags.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion internal/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions internal/pkg/agent/cmd/enroll_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down

0 comments on commit c00eddf

Please sign in to comment.