@@ -23,9 +23,6 @@ import (
23
23
v1 "k8s.io/api/core/v1"
24
24
kerrors "k8s.io/apimachinery/pkg/util/errors"
25
25
"k8s.io/apimachinery/pkg/util/sets"
26
- utilfeature "k8s.io/apiserver/pkg/util/feature"
27
- featuregatetesting "k8s.io/component-base/featuregate/testing"
28
- "k8s.io/kubernetes/pkg/features"
29
26
)
30
27
31
28
func checkExpectedEndpoints (expected sets.Set [string ], actual []Endpoint ) error {
@@ -47,12 +44,11 @@ func checkExpectedEndpoints(expected sets.Set[string], actual []Endpoint) error
47
44
48
45
func TestCategorizeEndpoints (t * testing.T ) {
49
46
testCases := []struct {
50
- name string
51
- hintsEnabled bool
52
- pteEnabled bool
53
- nodeLabels map [string ]string
54
- serviceInfo ServicePort
55
- endpoints []Endpoint
47
+ name string
48
+ pteEnabled bool
49
+ nodeLabels map [string ]string
50
+ serviceInfo ServicePort
51
+ endpoints []Endpoint
56
52
57
53
// We distinguish `nil` ("service doesn't use this kind of endpoints") from
58
54
// `sets.Set[string]()` ("service uses this kind of endpoints but has no endpoints").
@@ -65,63 +61,9 @@ func TestCategorizeEndpoints(t *testing.T) {
65
61
allEndpoints sets.Set [string ]
66
62
onlyRemoteEndpoints bool
67
63
}{{
68
- name : "hints enabled, hints annotation == auto" ,
69
- hintsEnabled : true ,
70
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
71
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
72
- endpoints : []Endpoint {
73
- & BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
74
- & BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
75
- & BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : sets.New [string ]("zone-c" ), ready : true },
76
- & BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
77
- },
78
- clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.6:80" ),
79
- localEndpoints : nil ,
80
- }, {
81
- name : "hints, hints annotation == disabled, but endpointslice hints are not ignored since trafficDist feature-gate is always enabled by default" ,
82
- hintsEnabled : true ,
83
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
84
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "disabled" },
85
- endpoints : []Endpoint {
86
- & BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
87
- & BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
88
- & BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : sets.New [string ]("zone-c" ), ready : true },
89
- & BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
90
- },
91
- clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.6:80" ),
92
- localEndpoints : nil ,
93
- }, {
94
-
95
- name : "hints, hints annotation == aUto (wrong capitalization), hints no longer ignored" ,
96
- hintsEnabled : true ,
97
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
98
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "aUto" },
99
- endpoints : []Endpoint {
100
- & BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
101
- & BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
102
- & BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : sets.New [string ]("zone-c" ), ready : true },
103
- & BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
104
- },
105
- clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.6:80" ),
106
- localEndpoints : nil ,
107
- }, {
108
- name : "hints, hints annotation empty but endpointslice hints are not ignored since trafficDist feature-gate is always enabled by default" ,
109
- hintsEnabled : true ,
110
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
111
- serviceInfo : & BaseServicePortInfo { /* hints annotation empty */ },
112
- endpoints : []Endpoint {
113
- & BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
114
- & BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
115
- & BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : sets.New [string ]("zone-c" ), ready : true },
116
- & BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
117
- },
118
- clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.6:80" ),
119
- localEndpoints : nil ,
120
- }, {
121
- name : "hints feature-gate disabled but endpointslice hints are not ignored since trafficDist feature-gate is always enabled by default" ,
122
- hintsEnabled : false ,
123
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
124
- serviceInfo : & BaseServicePortInfo {},
64
+ name : "should use topology since all endpoints have hints, node has a zone label and and there are endpoints for the node's zone" ,
65
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
66
+ serviceInfo : & BaseServicePortInfo {},
125
67
endpoints : []Endpoint {
126
68
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
127
69
& BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
@@ -131,10 +73,9 @@ func TestCategorizeEndpoints(t *testing.T) {
131
73
clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.6:80" ),
132
74
localEndpoints : nil ,
133
75
}, {
134
- name : "externalTrafficPolicy: Local, topology ignored for Local endpoints" ,
135
- hintsEnabled : true ,
136
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
137
- serviceInfo : & BaseServicePortInfo {externalPolicyLocal : true , nodePort : 8080 , hintsAnnotation : "auto" },
76
+ name : "externalTrafficPolicy: Local, topology ignored for Local endpoints" ,
77
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
78
+ serviceInfo : & BaseServicePortInfo {externalPolicyLocal : true , nodePort : 8080 },
138
79
endpoints : []Endpoint {
139
80
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true , isLocal : true },
140
81
& BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true , isLocal : true },
@@ -145,10 +86,9 @@ func TestCategorizeEndpoints(t *testing.T) {
145
86
localEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.4:80" ),
146
87
allEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.4:80" , "10.1.2.6:80" ),
147
88
}, {
148
- name : "internalTrafficPolicy: Local, topology ignored for Local endpoints" ,
149
- hintsEnabled : true ,
150
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
151
- serviceInfo : & BaseServicePortInfo {internalPolicyLocal : true , hintsAnnotation : "auto" , externalPolicyLocal : false , nodePort : 8080 },
89
+ name : "internalTrafficPolicy: Local, topology ignored for Local endpoints" ,
90
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
91
+ serviceInfo : & BaseServicePortInfo {internalPolicyLocal : true , externalPolicyLocal : false , nodePort : 8080 },
152
92
endpoints : []Endpoint {
153
93
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true , isLocal : true },
154
94
& BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true , isLocal : true },
@@ -159,66 +99,48 @@ func TestCategorizeEndpoints(t *testing.T) {
159
99
localEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.4:80" ),
160
100
allEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.4:80" , "10.1.2.6:80" ),
161
101
}, {
162
- name : "empty node labels" ,
163
- hintsEnabled : true ,
164
- nodeLabels : map [string ]string {},
165
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
102
+ name : "empty node labels" ,
103
+ nodeLabels : map [string ]string {},
104
+ serviceInfo : & BaseServicePortInfo {},
166
105
endpoints : []Endpoint {
167
106
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
168
107
},
169
108
clusterEndpoints : sets.New [string ]("10.1.2.3:80" ),
170
109
localEndpoints : nil ,
171
110
}, {
172
- name : "empty zone label" ,
173
- hintsEnabled : true ,
174
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "" },
175
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
111
+ name : "empty zone label" ,
112
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "" },
113
+ serviceInfo : & BaseServicePortInfo {},
176
114
endpoints : []Endpoint {
177
115
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
178
116
},
179
117
clusterEndpoints : sets.New [string ]("10.1.2.3:80" ),
180
118
localEndpoints : nil ,
181
119
}, {
182
- name : "node in different zone, no endpoint filtering" ,
183
- hintsEnabled : true ,
184
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-b" },
185
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
120
+ name : "node in different zone, no endpoint filtering" ,
121
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-b" },
122
+ serviceInfo : & BaseServicePortInfo {},
186
123
endpoints : []Endpoint {
187
124
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
188
125
},
189
126
clusterEndpoints : sets.New [string ]("10.1.2.3:80" ),
190
127
localEndpoints : nil ,
191
128
}, {
192
- name : "normal endpoint filtering, auto annotation" ,
193
- hintsEnabled : true ,
194
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
195
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
196
- endpoints : []Endpoint {
197
- & BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
198
- & BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
199
- & BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : sets.New [string ]("zone-c" ), ready : true },
200
- & BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
201
- },
202
- clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.6:80" ),
203
- localEndpoints : nil ,
204
- }, {
205
- name : "unready endpoint" ,
206
- hintsEnabled : true ,
207
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
208
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
129
+ name : "unready endpoint" ,
130
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
131
+ serviceInfo : & BaseServicePortInfo {},
209
132
endpoints : []Endpoint {
210
133
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
211
134
& BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
212
135
& BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : sets.New [string ]("zone-c" ), ready : true },
213
- & BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : false },
136
+ & BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : false }, // unready
214
137
},
215
138
clusterEndpoints : sets.New [string ]("10.1.2.3:80" ),
216
139
localEndpoints : nil ,
217
140
}, {
218
- name : "only unready endpoints in same zone (should not filter)" ,
219
- hintsEnabled : true ,
220
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
221
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
141
+ name : "only unready endpoints in same zone (should not filter)" ,
142
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
143
+ serviceInfo : & BaseServicePortInfo {},
222
144
endpoints : []Endpoint {
223
145
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : false },
224
146
& BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
@@ -228,36 +150,21 @@ func TestCategorizeEndpoints(t *testing.T) {
228
150
clusterEndpoints : sets .New [string ]("10.1.2.4:80" , "10.1.2.5:80" ),
229
151
localEndpoints : nil ,
230
152
}, {
231
- name : "normal endpoint filtering, Auto annotation" ,
232
- hintsEnabled : true ,
233
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
234
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "Auto" },
235
- endpoints : []Endpoint {
236
- & BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
237
- & BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
238
- & BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : sets.New [string ]("zone-c" ), ready : true },
239
- & BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
240
- },
241
- clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.6:80" ),
242
- localEndpoints : nil ,
243
- }, {
244
- name : "missing hints, no filtering applied" ,
245
- hintsEnabled : true ,
246
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
247
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
153
+ name : "missing hints, no filtering applied" ,
154
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
155
+ serviceInfo : & BaseServicePortInfo {},
248
156
endpoints : []Endpoint {
249
157
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
250
158
& BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true },
251
- & BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : nil , ready : true },
159
+ & BaseEndpointInfo {endpoint : "10.1.2.5:80" , zoneHints : nil , ready : true }, // Endpoint is missing hint.
252
160
& BaseEndpointInfo {endpoint : "10.1.2.6:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true },
253
161
},
254
162
clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.4:80" , "10.1.2.5:80" , "10.1.2.6:80" ),
255
163
localEndpoints : nil ,
256
164
}, {
257
- name : "multiple hints per endpoint, filtering includes any endpoint with zone included" ,
258
- hintsEnabled : true ,
259
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-c" },
260
- serviceInfo : & BaseServicePortInfo {hintsAnnotation : "auto" },
165
+ name : "multiple hints per endpoint, filtering includes any endpoint with zone included" ,
166
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-c" },
167
+ serviceInfo : & BaseServicePortInfo {},
261
168
endpoints : []Endpoint {
262
169
& BaseEndpointInfo {endpoint : "10.1.2.3:80" , zoneHints : sets .New [string ]("zone-a" , "zone-b" , "zone-c" ), ready : true },
263
170
& BaseEndpointInfo {endpoint : "10.1.2.4:80" , zoneHints : sets .New [string ]("zone-b" , "zone-c" ), ready : true },
@@ -267,10 +174,9 @@ func TestCategorizeEndpoints(t *testing.T) {
267
174
clusterEndpoints : sets .New [string ]("10.1.2.3:80" , "10.1.2.4:80" , "10.1.2.6:80" ),
268
175
localEndpoints : nil ,
269
176
}, {
270
- name : "conflicting topology and localness require merging allEndpoints" ,
271
- hintsEnabled : true ,
272
- nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
273
- serviceInfo : & BaseServicePortInfo {internalPolicyLocal : false , externalPolicyLocal : true , nodePort : 8080 , hintsAnnotation : "auto" },
177
+ name : "conflicting topology and localness require merging allEndpoints" ,
178
+ nodeLabels : map [string ]string {v1 .LabelTopologyZone : "zone-a" },
179
+ serviceInfo : & BaseServicePortInfo {internalPolicyLocal : false , externalPolicyLocal : true , nodePort : 8080 },
274
180
endpoints : []Endpoint {
275
181
& BaseEndpointInfo {endpoint : "10.0.0.0:80" , zoneHints : sets.New [string ]("zone-a" ), ready : true , isLocal : true },
276
182
& BaseEndpointInfo {endpoint : "10.0.0.1:80" , zoneHints : sets.New [string ]("zone-b" ), ready : true , isLocal : true },
@@ -431,8 +337,6 @@ func TestCategorizeEndpoints(t *testing.T) {
431
337
432
338
for _ , tc := range testCases {
433
339
t .Run (tc .name , func (t * testing.T ) {
434
- featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .TopologyAwareHints , tc .hintsEnabled )
435
-
436
340
clusterEndpoints , localEndpoints , allEndpoints , hasAnyEndpoints := CategorizeEndpoints (tc .endpoints , tc .serviceInfo , tc .nodeLabels )
437
341
438
342
if tc .clusterEndpoints == nil && clusterEndpoints != nil {
0 commit comments