Skip to content

Commit

Permalink
Deduplicate set expression values in metav1.LabelSelector fuzzer.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benluddy authored and jingczhang committed May 7, 2024
1 parent 9155db3 commit 5ad993e
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 5ad993e

Please sign in to comment.