Skip to content

Commit 4d9e4f0

Browse files
committed
move ShuffleStrings to pkg/proxy
Signed-off-by: Yassine TIJANI <[email protected]>
1 parent 5df8781 commit 4d9e4f0

File tree

8 files changed

+51
-52
lines changed

8 files changed

+51
-52
lines changed

pkg/proxy/userspace/roundrobin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"sync"
2525
"time"
2626

27-
"k8s.io/api/core/v1"
27+
v1 "k8s.io/api/core/v1"
2828
"k8s.io/apimachinery/pkg/types"
2929
"k8s.io/klog"
3030
"k8s.io/kubernetes/pkg/proxy"
@@ -240,7 +240,7 @@ func (lb *LoadBalancerRR) OnEndpointsAdd(endpoints *v1.Endpoints) {
240240
// if one does not already exist. The affinity will be updated
241241
// later, once NewService is called.
242242
state = lb.newServiceInternal(svcPort, v1.ServiceAffinity(""), 0)
243-
state.endpoints = slice.ShuffleStrings(newEndpoints)
243+
state.endpoints = util.ShuffleStrings(newEndpoints)
244244

245245
// Reset the round-robin index.
246246
state.index = 0
@@ -274,7 +274,7 @@ func (lb *LoadBalancerRR) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoint
274274
// if one does not already exist. The affinity will be updated
275275
// later, once NewService is called.
276276
state = lb.newServiceInternal(svcPort, v1.ServiceAffinity(""), 0)
277-
state.endpoints = slice.ShuffleStrings(newEndpoints)
277+
state.endpoints = util.ShuffleStrings(newEndpoints)
278278

279279
// Reset the round-robin index.
280280
state.index = 0

pkg/proxy/util/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"//pkg/apis/core/v1/helper:go_default_library",
1515
"//staging/src/k8s.io/api/core/v1:go_default_library",
1616
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
17+
"//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library",
1718
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
1819
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
1920
"//vendor/k8s.io/klog:go_default_library",

pkg/proxy/util/utils.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323
"net"
2424
"strconv"
2525

26-
"k8s.io/api/core/v1"
26+
v1 "k8s.io/api/core/v1"
2727
"k8s.io/apimachinery/pkg/types"
28+
utilrand "k8s.io/apimachinery/pkg/util/rand"
2829
"k8s.io/apimachinery/pkg/util/sets"
2930
"k8s.io/client-go/tools/record"
3031
helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
@@ -271,3 +272,17 @@ func AppendPortIfNeeded(addr string, port int32) string {
271272
}
272273
return fmt.Sprintf("[%s]:%d", addr, port)
273274
}
275+
276+
// ShuffleStrings copies strings from the specified slice into a copy in random
277+
// order. It returns a new slice.
278+
func ShuffleStrings(s []string) []string {
279+
if s == nil {
280+
return nil
281+
}
282+
shuffled := make([]string, len(s))
283+
perm := utilrand.Perm(len(s))
284+
for i, j := range perm {
285+
shuffled[j] = s[i]
286+
}
287+
return shuffled
288+
}

pkg/proxy/util/utils_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"reflect"
2323
"testing"
2424

25-
"k8s.io/api/core/v1"
25+
v1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/types"
2828
"k8s.io/apimachinery/pkg/util/sets"
@@ -510,3 +510,30 @@ func TestAppendPortIfNeeded(t *testing.T) {
510510
}
511511
}
512512
}
513+
514+
func TestShuffleStrings(t *testing.T) {
515+
var src []string
516+
dest := ShuffleStrings(src)
517+
518+
if dest != nil {
519+
t.Errorf("ShuffleStrings for a nil slice got a non-nil slice")
520+
}
521+
522+
src = []string{"a", "b", "c", "d", "e", "f"}
523+
dest = ShuffleStrings(src)
524+
525+
if len(src) != len(dest) {
526+
t.Errorf("Shuffled slice is wrong length, expected %v got %v", len(src), len(dest))
527+
}
528+
529+
m := make(map[string]bool, len(dest))
530+
for _, s := range dest {
531+
m[s] = true
532+
}
533+
534+
for _, k := range src {
535+
if _, exists := m[k]; !exists {
536+
t.Errorf("Element %v missing from shuffled slice", k)
537+
}
538+
}
539+
}

pkg/proxy/winuserspace/roundrobin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"sync"
2525
"time"
2626

27-
"k8s.io/api/core/v1"
27+
v1 "k8s.io/api/core/v1"
2828
"k8s.io/apimachinery/pkg/types"
2929
"k8s.io/klog"
3030
"k8s.io/kubernetes/pkg/proxy"
@@ -230,7 +230,7 @@ func (lb *LoadBalancerRR) OnEndpointsAdd(endpoints *v1.Endpoints) {
230230
// if one does not already exist. The affinity will be updated
231231
// later, once NewService is called.
232232
state = lb.newServiceInternal(svcPort, v1.ServiceAffinity(""), 0)
233-
state.endpoints = slice.ShuffleStrings(newEndpoints)
233+
state.endpoints = util.ShuffleStrings(newEndpoints)
234234

235235
// Reset the round-robin index.
236236
state.index = 0
@@ -264,7 +264,7 @@ func (lb *LoadBalancerRR) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoint
264264
// if one does not already exist. The affinity will be updated
265265
// later, once NewService is called.
266266
state = lb.newServiceInternal(svcPort, v1.ServiceAffinity(""), 0)
267-
state.endpoints = slice.ShuffleStrings(newEndpoints)
267+
state.endpoints = util.ShuffleStrings(newEndpoints)
268268

269269
// Reset the round-robin index.
270270
state.index = 0

pkg/util/slice/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ go_library(
1010
name = "go_default_library",
1111
srcs = ["slice.go"],
1212
importpath = "k8s.io/kubernetes/pkg/util/slice",
13-
deps = ["//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library"],
1413
)
1514

1615
go_test(

pkg/util/slice/slice.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package slice
1919

2020
import (
2121
"sort"
22-
23-
utilrand "k8s.io/apimachinery/pkg/util/rand"
2422
)
2523

2624
// CopyStrings copies the contents of the specified string slice
@@ -41,20 +39,6 @@ func SortStrings(s []string) []string {
4139
return s
4240
}
4341

44-
// ShuffleStrings copies strings from the specified slice into a copy in random
45-
// order. It returns a new slice.
46-
func ShuffleStrings(s []string) []string {
47-
if s == nil {
48-
return nil
49-
}
50-
shuffled := make([]string, len(s))
51-
perm := utilrand.Perm(len(s))
52-
for i, j := range perm {
53-
shuffled[j] = s[i]
54-
}
55-
return shuffled
56-
}
57-
5842
// ContainsString checks if a given slice of strings contains the provided string.
5943
// If a modifier func is provided, it is called with the slice item before the comparation.
6044
func ContainsString(slice []string, s string, modifier func(s string) string) bool {

pkg/util/slice/slice_test.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,6 @@ func TestSortStrings(t *testing.T) {
6363
}
6464
}
6565

66-
func TestShuffleStrings(t *testing.T) {
67-
var src []string
68-
dest := ShuffleStrings(src)
69-
70-
if dest != nil {
71-
t.Errorf("ShuffleStrings for a nil slice got a non-nil slice")
72-
}
73-
74-
src = []string{"a", "b", "c", "d", "e", "f"}
75-
dest = ShuffleStrings(src)
76-
77-
if len(src) != len(dest) {
78-
t.Errorf("Shuffled slice is wrong length, expected %v got %v", len(src), len(dest))
79-
}
80-
81-
m := make(map[string]bool, len(dest))
82-
for _, s := range dest {
83-
m[s] = true
84-
}
85-
86-
for _, k := range src {
87-
if _, exists := m[k]; !exists {
88-
t.Errorf("Element %v missing from shuffled slice", k)
89-
}
90-
}
91-
}
92-
9366
func TestContainsString(t *testing.T) {
9467
src := []string{"aa", "bb", "cc"}
9568
if !ContainsString(src, "bb", nil) {

0 commit comments

Comments
 (0)