@@ -105,11 +105,9 @@ var _ = common.SIGDescribe("Traffic Distribution", func() {
105
105
pod * v1.Pod
106
106
}
107
107
108
- ////////////////////////////////////////////////////////////////////////////
109
- // Main test specifications.
110
- ////////////////////////////////////////////////////////////////////////////
111
-
112
- ginkgo .It ("should route traffic to an endpoint in the same zone when using PreferClose" , func (ctx context.Context ) {
108
+ // allocateClientsAndServers figures out where to put clients and servers for
109
+ // a simple "same-zone" traffic distribution test.
110
+ allocateClientsAndServers := func (ctx context.Context ) ([]* clientPod , []* serverPod ) {
113
111
ginkgo .By ("finding 3 zones with schedulable nodes" )
114
112
nodeList , err := e2enode .GetReadySchedulableNodes (ctx , c )
115
113
framework .ExpectNoError (err )
@@ -149,41 +147,52 @@ var _ = common.SIGDescribe("Traffic Distribution", func() {
149
147
clientPods [1 ].endpoints = []* serverPod {serverPods [1 ]}
150
148
clientPods [2 ].endpoints = serverPods
151
149
150
+ return clientPods , serverPods
151
+ }
152
+
153
+ // createService creates the service for a traffic distribution test
154
+ createService := func (ctx context.Context , trafficDist string ) * v1.Service {
155
+ serviceName := "traffic-dist-test-service"
156
+ ginkgo .By (fmt .Sprintf ("creating a service %q with trafficDistribution %q" , serviceName , trafficDist ))
157
+ return createServiceReportErr (ctx , c , f .Namespace .Name , & v1.Service {
158
+ ObjectMeta : metav1.ObjectMeta {
159
+ Name : serviceName ,
160
+ },
161
+ Spec : v1.ServiceSpec {
162
+ Selector : map [string ]string {
163
+ "app" : f .UniqueName ,
164
+ },
165
+ TrafficDistribution : & trafficDist ,
166
+ Ports : []v1.ServicePort {{
167
+ Port : 80 ,
168
+ TargetPort : intstr .FromInt32 (9376 ),
169
+ Protocol : v1 .ProtocolTCP ,
170
+ }},
171
+ },
172
+ })
173
+ }
174
+
175
+ // createPods creates endpoint pods for svc as described by serverPods, waits for
176
+ // the EndpointSlices to be updated, and creates clientPods as described by
177
+ // clientPods.
178
+ createPods := func (ctx context.Context , svc * v1.Service , clientPods []* clientPod , serverPods []* serverPod ) {
152
179
var podsToCreate []* v1.Pod
153
- servingPodLabels := map [string ]string {"app" : f .UniqueName }
154
180
for i , sp := range serverPods {
155
181
node := sp .node .Name
156
182
zone := sp .node .Labels [v1 .LabelTopologyZone ]
157
183
pod := e2epod .NewAgnhostPod (f .Namespace .Name , fmt .Sprintf ("server-%d-%s" , i , node ), nil , nil , nil , "serve-hostname" )
158
184
ginkgo .By (fmt .Sprintf ("creating a server pod %q on node %q in zone %q" , pod .Name , node , zone ))
159
185
nodeSelection := e2epod.NodeSelection {Name : node }
160
186
e2epod .SetNodeSelection (& pod .Spec , nodeSelection )
161
- pod .Labels = servingPodLabels
187
+ pod .Labels = svc . Spec . Selector
162
188
163
189
sp .pod = pod
164
190
podsToCreate = append (podsToCreate , pod )
165
191
}
166
192
e2epod .NewPodClient (f ).CreateBatch (ctx , podsToCreate )
167
193
168
- trafficDist := v1 .ServiceTrafficDistributionPreferClose
169
- svc := createServiceReportErr (ctx , c , f .Namespace .Name , & v1.Service {
170
- ObjectMeta : metav1.ObjectMeta {
171
- Name : "traffic-dist-test-service" ,
172
- },
173
- Spec : v1.ServiceSpec {
174
- Selector : servingPodLabels ,
175
- TrafficDistribution : & trafficDist ,
176
- Ports : []v1.ServicePort {{
177
- Port : 80 ,
178
- TargetPort : intstr .FromInt32 (9376 ),
179
- Protocol : v1 .ProtocolTCP ,
180
- }},
181
- },
182
- })
183
- ginkgo .By (fmt .Sprintf ("creating a service=%q with trafficDistribution=%v" , svc .GetName (), * svc .Spec .TrafficDistribution ))
184
-
185
194
ginkgo .By ("waiting for EndpointSlices to be created" )
186
- err = framework .WaitForServiceEndpointsNum (ctx , c , svc .Namespace , svc .Name , len (serverPods ), 1 * time .Second , e2eservice .ServiceEndpointsTimeout )
195
+ err : = framework .WaitForServiceEndpointsNum (ctx , c , svc .Namespace , svc .Name , len (serverPods ), 1 * time .Second , e2eservice .ServiceEndpointsTimeout )
187
196
framework .ExpectNoError (err )
188
197
slices := endpointSlicesForService (svc .Name )
189
198
framework .Logf ("got slices:\n %v" , format .Object (slices , 1 ))
@@ -204,7 +213,11 @@ var _ = common.SIGDescribe("Traffic Distribution", func() {
204
213
podsToCreate = append (podsToCreate , pod )
205
214
}
206
215
e2epod .NewPodClient (f ).CreateBatch (ctx , podsToCreate )
216
+ }
207
217
218
+ // checkTrafficDistribution checks that traffic from clientPods is distributed in
219
+ // the expected way.
220
+ checkTrafficDistribution := func (ctx context.Context , clientPods []* clientPod ) {
208
221
for _ , cp := range clientPods {
209
222
wantedEndpoints := sets .New [string ]()
210
223
for _ , sp := range cp .endpoints {
@@ -241,5 +254,16 @@ var _ = common.SIGDescribe("Traffic Distribution", func() {
241
254
242
255
gomega .Eventually (ctx , requestsFromClient (cp .pod )).WithPolling (5 * time .Second ).WithTimeout (e2eservice .KubeProxyLagTimeout ).Should (requestsSucceed )
243
256
}
257
+ }
258
+
259
+ ////////////////////////////////////////////////////////////////////////////
260
+ // Main test specifications.
261
+ ////////////////////////////////////////////////////////////////////////////
262
+
263
+ ginkgo .It ("should route traffic to an endpoint in the same zone when using PreferClose" , func (ctx context.Context ) {
264
+ clientPods , serverPods := allocateClientsAndServers (ctx )
265
+ svc := createService (ctx , v1 .ServiceTrafficDistributionPreferClose )
266
+ createPods (ctx , svc , clientPods , serverPods )
267
+ checkTrafficDistribution (ctx , clientPods )
244
268
})
245
269
})
0 commit comments