forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve replica flag handling (thanos-io#7855)
Add a string utility parsing function to improve the handling of replica label flags. This allows for easier handling of flags when multiple replica labels are need. * Split flag parts that are comma separated. * Remove any empty strings. * Sort and deduplicate the slice. For example in the case of multiple replica labels like: `--query.replica-label=prometheus_replica,thanos_rule_replica` Signed-off-by: SuperQ <superq@gmail.com>
- Loading branch information
1 parent
05724b9
commit 156977d
Showing
7 changed files
with
95 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package strutil | ||
|
||
import ( | ||
"slices" | ||
"strings" | ||
) | ||
|
||
// ParseFlagLabels helps handle lists of labels passed from kingpin flags. | ||
// * Split flag parts that are comma separated. | ||
// * Remove any empty strings. | ||
// * Sort and deduplicate the slice. | ||
func ParseFlagLabels(f []string) []string { | ||
var result []string | ||
for _, l := range f { | ||
if l == "" { | ||
continue | ||
} | ||
result = append(result, strings.Split(l, ",")...) | ||
} | ||
slices.Sort(result) | ||
return slices.Compact(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package strutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/efficientgo/core/testutil" | ||
) | ||
|
||
func TestParseFlagLabels(t *testing.T) { | ||
testCases := map[string]struct { | ||
flags []string | ||
expected []string | ||
}{ | ||
"single flag with commas": { | ||
flags: []string{ | ||
"a,b,c", | ||
}, | ||
expected: []string{"a", "b", "c"}, | ||
}, | ||
"multiple flags with commas": { | ||
flags: []string{ | ||
"a", "b", "c,d", | ||
}, | ||
expected: []string{"a", "b", "c", "d"}, | ||
}, | ||
"multiple flags empty strings": { | ||
flags: []string{ | ||
"a", "b", "", | ||
}, | ||
expected: []string{"a", "b"}, | ||
}, | ||
} | ||
|
||
for tcName, tc := range testCases { | ||
t.Run(tcName, func(t *testing.T) { | ||
res := ParseFlagLabels(tc.flags) | ||
testutil.Equals(t, tc.expected, res) | ||
}) | ||
} | ||
} |