Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sorting behavior in selector.go #38

Draft
wants to merge 1 commit into
base: release-1.10-dd
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1

import (
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -70,15 +71,21 @@ func TestLabelSelectorAsSelector(t *testing.T) {
}

for i, tc := range tc {
inCopy := tc.in.DeepCopy()
out, err := LabelSelectorAsSelector(tc.in)
// after calling LabelSelectorAsSelector, tc.in shouldn't be modified
if !reflect.DeepEqual(inCopy, tc.in) {
t.Errorf("[%v]expected:\n\t%#v\nbut got:\n\t%#v", i, inCopy, tc.in)
}
if err == nil && tc.expectErr {
t.Errorf("[%v]expected error but got none.", i)
}
if err != nil && !tc.expectErr {
t.Errorf("[%v]did not expect error but got: %v", i, err)
}
if !reflect.DeepEqual(out, tc.out) {
t.Errorf("[%v]expected:\n\t%+v\nbut got:\n\t%+v", i, tc.out, out)
// fmt.Sprint() over String() as nil.String() will panic
if fmt.Sprint(out) != fmt.Sprint(tc.out) {
t.Errorf("[%v]expected:\n\t%s\nbut got:\n\t%s", i, fmt.Sprint(tc.out), fmt.Sprint(out))
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions staging/src/k8s.io/apimachinery/pkg/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func NewRequirement(key string, op selection.Operator, vals []string) (*Requirem
return nil, err
}
}
sort.Strings(vals)
return &Requirement{key: key, operator: op, strValues: vals}, nil
}

Expand Down Expand Up @@ -299,7 +298,9 @@ func (r *Requirement) String() string {
if len(r.strValues) == 1 {
buffer.WriteString(r.strValues[0])
} else { // only > 1 since == 0 prohibited by NewRequirement
buffer.WriteString(strings.Join(r.strValues, ","))
// normalizes value order on output, without mutating the in-memory selector representation
// also avoids normalization when it is not required, and ensures we do not mutate shared data
buffer.WriteString(strings.Join(safeSort(r.strValues), ","))
}

switch r.operator {
Expand All @@ -309,6 +310,17 @@ func (r *Requirement) String() string {
return buffer.String()
}

// safeSort sort input strings without modification
func safeSort(in []string) []string {
if sort.StringsAreSorted(in) {
return in
}
out := make([]string, len(in))
copy(out, in)
sort.Strings(out)
return out
}

// Add adds requirements to the selector. It copies the current selector returning a new one
func (lsel internalSelector) Add(reqs ...Requirement) Selector {
var sel internalSelector
Expand Down
44 changes: 44 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/labels/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,47 @@ func TestAdd(t *testing.T) {
}
}
}

func TestSafeSort(t *testing.T) {
tests := []struct {
name string
in []string
inCopy []string
want []string
}{
{
name: "nil strings",
in: nil,
inCopy: nil,
want: nil,
},
{
name: "ordered strings",
in: []string{"bar", "foo"},
inCopy: []string{"bar", "foo"},
want: []string{"bar", "foo"},
},
{
name: "unordered strings",
in: []string{"foo", "bar"},
inCopy: []string{"foo", "bar"},
want: []string{"bar", "foo"},
},
{
name: "duplicated strings",
in: []string{"foo", "bar", "foo", "bar"},
inCopy: []string{"foo", "bar", "foo", "bar"},
want: []string{"bar", "bar", "foo", "foo"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := safeSort(tt.in); !reflect.DeepEqual(got, tt.want) {
t.Errorf("safeSort() = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(tt.in, tt.inCopy) {
t.Errorf("after safeSort(), input = %v, want %v", tt.in, tt.inCopy)
}
})
}
}