forked from flomesh-io/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_trafficsplit_recursive_split.go
332 lines (279 loc) · 10.4 KB
/
e2e_trafficsplit_recursive_split.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package e2e
import (
"context"
"fmt"
"sync"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openservicemesh/osm/pkg/constants"
. "github.com/openservicemesh/osm/tests/framework"
)
var _ = OSMDescribe("Test traffic split where root service is same as backend service",
OSMDescribeInfo{
Tier: 1,
Bucket: 9,
},
func() {
Context("HTTP recursive traffic splitting with SMI", func() {
testRecursiveTrafficSplit(constants.ProtocolHTTP)
})
Context("TCP recursive traffic splitting with SMI", func() {
testRecursiveTrafficSplit(constants.ProtocolTCP)
})
})
func testRecursiveTrafficSplit(appProtocol string) {
var (
// to name the header we will use to identify the server that replies
HTTPHeaderName = "podname"
clientAppBaseName = "client"
serverNamespace = "server-namespace"
trafficSplitName = "server"
// Scale number of client services/pods here
numberOfClientServices = 1
clientReplicaSet = 1
// Scale number of server services/pods here
numberOfServerServices = 1
serverReplicaSet = 1
clientServices = []string{}
serverServices = []string{trafficSplitName}
allNamespaces = []string{}
)
for i := 0; i < numberOfClientServices; i++ {
clientServices = append(clientServices, fmt.Sprintf("%s%d", clientAppBaseName, i))
}
allNamespaces = append(allNamespaces, clientServices...)
allNamespaces = append(allNamespaces, serverNamespace) // 1 namespace for all server services (for the trafficsplit)
// Used across the test to wait for concurrent steps to finish
var wg sync.WaitGroup
It("Tests HTTP traffic from Clients to the traffic split Cluster IP", func() {
// Install OSM
Expect(Td.InstallOSM(Td.GetOSMInstallOpts())).To(Succeed())
// Create NSs
Expect(Td.CreateMultipleNs(allNamespaces...)).To(Succeed())
Expect(Td.AddNsToMesh(true, allNamespaces...)).To(Succeed())
// Create server app
serverSvcAccDef, serverDeploymentDef, serverSvcDef, err := Td.SimpleDeploymentApp(
SimpleDeploymentAppDef{
DeploymentName: trafficSplitName,
Namespace: serverNamespace,
ServiceAccountName: trafficSplitName, // TODO refactor
ServiceName: trafficSplitName,
ReplicaCount: int32(serverReplicaSet),
Image: "simonkowallik/httpbin",
Ports: []int{DefaultUpstreamServicePort},
AppProtocol: appProtocol,
Command: HttpbinCmd,
OS: Td.ClusterOS,
})
Expect(err).NotTo(HaveOccurred())
// Expose an env variable such as XHTTPBIN_X_POD_NAME:
// This httpbin fork will pick certain env variable formats and reply the values as headers.
// We will expose pod name as one of these env variables, and will use it
// to identify the pod that replies to the request, and validate the test
serverDeploymentDef.Spec.Template.Spec.Containers[0].Env = []v1.EnvVar{
{
Name: fmt.Sprintf("XHTTPBIN_%s", HTTPHeaderName),
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
}
_, err = Td.CreateServiceAccount(serverNamespace, &serverSvcAccDef)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateDeployment(serverNamespace, serverDeploymentDef)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateService(serverNamespace, serverSvcDef)
Expect(err).NotTo(HaveOccurred())
wg.Add(1)
go func() {
defer GinkgoRecover()
defer wg.Done()
Expect(Td.WaitForPodsRunningReady(serverNamespace, 200*time.Second, numberOfServerServices*serverReplicaSet, nil)).To(Succeed())
}()
// Client apps
for _, clientApp := range clientServices {
svcAccDef, deploymentDef, svcDef, err := Td.SimpleDeploymentApp(
SimpleDeploymentAppDef{
DeploymentName: clientApp,
ServiceAccountName: clientApp,
Namespace: clientApp,
ContainerName: clientApp,
ReplicaCount: int32(clientReplicaSet),
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"while true; do sleep 30; done;"},
Image: "songrgg/alpine-debug",
Ports: []int{DefaultUpstreamServicePort},
OS: Td.ClusterOS,
})
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateServiceAccount(clientApp, &svcAccDef)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateDeployment(clientApp, deploymentDef)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateService(clientApp, svcDef)
Expect(err).NotTo(HaveOccurred())
wg.Add(1)
go func(app string) {
defer GinkgoRecover()
defer wg.Done()
Expect(Td.WaitForPodsRunningReady(app, 200*time.Second, clientReplicaSet, nil)).To(Succeed())
}(clientApp)
}
wg.Wait()
// Create Traffic split with single service with the same name as backend
trafficSplit := TrafficSplitDef{
Name: trafficSplitName,
Namespace: serverNamespace,
TrafficSplitServiceName: trafficSplitName,
Backends: []TrafficSplitBackend{
{
Name: trafficSplitName,
Weight: 100,
},
},
}
// Get the Traffic split structures
tSplit, err := Td.CreateSimpleTrafficSplit(trafficSplit)
Expect(err).To(BeNil())
// Push them in K8s
_, err = Td.CreateTrafficSplit(serverNamespace, tSplit)
Expect(err).To(BeNil())
// Put allow traffic target rules
for _, srcClient := range clientServices {
for _, dstServer := range serverServices {
switch appProtocol {
// HTTP traffic
case constants.ProtocolHTTP:
httpRG, trafficTarget := Td.CreateSimpleAllowPolicy(
SimpleAllowPolicy{
RouteGroupName: fmt.Sprintf("%s-%s", srcClient, dstServer),
TrafficTargetName: fmt.Sprintf("%s-%s", srcClient, dstServer),
SourceNamespace: srcClient,
SourceSVCAccountName: srcClient,
DestinationNamespace: serverNamespace,
DestinationSvcAccountName: dstServer,
})
// Configs have to be put into same NS as server/destination
_, err := Td.CreateHTTPRouteGroup(serverNamespace, httpRG)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateTrafficTarget(serverNamespace, trafficTarget)
Expect(err).NotTo(HaveOccurred())
// TCP traffic
case constants.ProtocolTCP:
tcpRoute, trafficTarget := Td.CreateSimpleTCPAllowPolicy(
SimpleAllowPolicy{
RouteGroupName: fmt.Sprintf("%s-%s", srcClient, dstServer),
TrafficTargetName: fmt.Sprintf("%s-%s", srcClient, dstServer),
SourceNamespace: srcClient,
SourceSVCAccountName: srcClient,
DestinationNamespace: serverNamespace,
DestinationSvcAccountName: dstServer,
},
DefaultUpstreamServicePort,
)
// Configs have to be put into same NS as server/destination
_, err := Td.CreateTCPRoute(serverNamespace, tcpRoute)
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateTrafficTarget(serverNamespace, trafficTarget)
Expect(err).NotTo(HaveOccurred())
default:
Td.T.Fatalf("Unsupported appProtocol %s for test, must be one of [http, tcp]", appProtocol)
}
}
}
By("Issuing http requests from clients to the traffic split FQDN")
// Test traffic
// Create Multiple HTTP request structure
requests := HTTPMultipleRequest{
Sources: []HTTPRequestDef{},
}
for _, ns := range clientServices {
pods, err := Td.Client.CoreV1().Pods(ns).List(context.Background(), metav1.ListOptions{})
Expect(err).To(BeNil())
for _, pod := range pods.Items {
requests.Sources = append(requests.Sources, HTTPRequestDef{
SourceNs: ns,
SourcePod: pod.Name,
SourceContainer: ns,
// Targeting the trafficsplit FQDN
Destination: fmt.Sprintf("%s.%s:%d", trafficSplitName, serverNamespace, DefaultUpstreamServicePort),
})
}
}
var results HTTPMultipleResults
var serversSeen = map[string]bool{} // Just counts unique servers seen
success := Td.WaitForRepeatedSuccess(func() bool {
curlSuccess := true
// Get results
results = Td.MultipleHTTPRequest(&requests)
// Print results
Td.PrettyPrintHTTPResults(&results)
// Verify REST status code results
for _, ns := range results {
for _, podResult := range ns {
if podResult.Err != nil || podResult.StatusCode != 200 {
curlSuccess = false
} else {
// We should see pod header populated
dstPod, ok := podResult.Headers[HTTPHeaderName]
if ok {
// Store and mark that we have seen a response for this server pod
serversSeen[dstPod] = true
}
}
}
}
Td.T.Logf("Unique servers replied %d/%d",
len(serversSeen), numberOfServerServices*serverReplicaSet)
// Success conditions:
// - All clients have been answered consecutively 5 successful HTTP requests
// - We have seen all servers from the traffic split reply at least once
return curlSuccess && (len(serversSeen) == numberOfServerServices*serverReplicaSet)
}, 5, 150*time.Second)
Expect(success).To(BeTrue())
By("Issuing http requests from clients to the allowed individual service backends")
// Test now against the individual services, observe they should still be reachable
requests = HTTPMultipleRequest{
Sources: []HTTPRequestDef{},
}
for _, clientNs := range clientServices {
pods, err := Td.Client.CoreV1().Pods(clientNs).List(context.Background(), metav1.ListOptions{})
Expect(err).To(BeNil())
// For each client pod
for _, pod := range pods.Items {
// reach each service
for _, svcNs := range serverServices {
requests.Sources = append(requests.Sources, HTTPRequestDef{
SourceNs: pod.Namespace,
SourcePod: pod.Name,
SourceContainer: pod.Namespace, // We generally code it like so for test purposes
// direct traffic target against the specific server service in the server namespace
Destination: fmt.Sprintf("%s.%s:%d", svcNs, serverNamespace, DefaultUpstreamServicePort),
})
}
}
}
results = HTTPMultipleResults{}
success = Td.WaitForRepeatedSuccess(func() bool {
// Get results
results = Td.MultipleHTTPRequest(&requests)
// Print results
Td.PrettyPrintHTTPResults(&results)
// Verify REST status code results
for _, ns := range results {
for _, podResult := range ns {
if podResult.Err != nil || podResult.StatusCode != 200 {
return false
}
}
}
return true
}, 2, 150*time.Second)
Expect(success).To(BeTrue())
})
}