From a4c2f78b284f0208a26935a348556e49f48e5f1c Mon Sep 17 00:00:00 2001 From: Ben Luddy Date: Mon, 23 Oct 2023 09:44:21 -0400 Subject: [PATCH] Deduplicate set expression values in metav1.LabelSelector fuzzer. Internal versions of ScaleStatus types use metav1.LabelSelector to represent label selectors, while external versions use the textual representation. During conversion to and from text, match expressions are sorted by key, and values for set operations "in" and "notin" are sorted and deduplicated. This loss of order and duplication is detected by roundtrip testing. The existing fuzz function for metav1.LabelSelector sorts match expressions by key and sorts, but does not deduplicate, set expression values. That function now also deduplicates set expression values so that fuzzed metav1.LabelSelectors can faithfully roundtrip through the textual label selector representation. --- .../k8s.io/apimachinery/pkg/apis/meta/fuzzer/fuzzer.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/fuzzer/fuzzer.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/fuzzer/fuzzer.go index 6bc1356ed46..b47f0c0ddb2 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/fuzzer/fuzzer.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/fuzzer/fuzzer.go @@ -33,6 +33,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/sets" ) func genericFuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} { @@ -249,8 +250,9 @@ func v1FuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} { } if j.MatchExpressions != nil { - // NB: the label selector parser code sorts match expressions by key, and sorts the values, - // so we need to make sure ours are sorted as well here to preserve round-trip comparison. + // NB: the label selector parser code sorts match expressions by key, and + // sorts and deduplicates the values, so we need to make sure ours are + // sorted and deduplicated as well here to preserve round-trip comparison. // In practice, not sorting doesn't hurt anything... for i := range j.MatchExpressions { @@ -266,7 +268,7 @@ func v1FuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} { for i := range req.Values { req.Values[i] = randomLabelPart(c, true) } - sort.Strings(req.Values) + req.Values = sets.List(sets.New(req.Values...)) } else { req.Values = nil }