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.
This commit is contained in:
Ben Luddy 2023-10-23 09:44:21 -04:00
parent 15ee05afd4
commit a4c2f78b28
No known key found for this signature in database
GPG Key ID: A6551E73A5974C30

View File

@ -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
}