forked from envoyproxy/go-control-plane
/
main.go
359 lines (306 loc) · 9.98 KB
/
main.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2018 Envoyproxy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main contains the test driver for testing xDS manually.
package main
import (
"context"
cryptotls "crypto/tls"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
cachev2 "github.com/envoyproxy/go-control-plane/pkg/cache/v2"
cachev3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
serverv2 "github.com/envoyproxy/go-control-plane/pkg/server/v2"
serverv3 "github.com/envoyproxy/go-control-plane/pkg/server/v3"
"github.com/envoyproxy/go-control-plane/pkg/test"
testv2 "github.com/envoyproxy/go-control-plane/pkg/test/v2"
testv3 "github.com/envoyproxy/go-control-plane/pkg/test/v3"
resourcev2 "github.com/envoyproxy/go-control-plane/pkg/test/resource/v2"
resourcev3 "github.com/envoyproxy/go-control-plane/pkg/test/resource/v3"
)
var (
debug bool
port uint
gatewayPort uint
upstreamPort uint
upstreamMessage string
basePort uint
alsPort uint
delay time.Duration
requests int
updates int
mode string
clusters int
httpListeners int
tcpListeners int
runtimes int
tls bool
mux bool
nodeID string
)
func init() {
flag.BoolVar(&debug, "debug", false, "Use debug logging")
//
// These parameters control the ports that the integration test
// components use to talk to one another
//
// The port that the Envoy xDS client uses to talk to the control
// plane xDS server (part of this program)
flag.UintVar(&port, "port", 18000, "xDS management server port")
// The port that the Envoy REST client uses to talk to the control
// plane gateway (which translates from REST to xDS)
flag.UintVar(&gatewayPort, "gateway", 18001, "Management HTTP gateway (from HTTP to xDS) server port")
// The port that Envoy uses to talk to the upstream http "echo"
// server
flag.UintVar(&upstreamPort, "upstream", 18080, "Upstream HTTP/1.1 port")
// The port that the tests below use to talk to Envoy's proxy of the
// upstream server
flag.UintVar(&basePort, "base", 9000, "Envoy Proxy listener port")
// The control plane accesslog server port (currently unused)
flag.UintVar(&alsPort, "als", 18090, "Control plane accesslog server port")
//
// These parameters control Envoy configuration
//
// Tell Envoy to request configurations from the control plane using
// this protocol
flag.StringVar(&mode, "xds", resourcev2.Ads, "Management protocol to test (ADS, xDS, REST)")
// Tell Envoy to use this Node ID
flag.StringVar(&nodeID, "nodeID", "test-id", "Node ID")
// Tell Envoy to use TLS to talk to the control plane
flag.BoolVar(&tls, "tls", false, "Enable TLS on all listeners and use SDS for secret delivery")
// Tell Envoy to configure this many clusters for each snapshot
flag.IntVar(&clusters, "clusters", 4, "Number of clusters")
// Tell Envoy to configure this many Runtime Discovery Service
// layers for each snapshot
flag.IntVar(&runtimes, "runtimes", 1, "Number of RTDS layers")
//
// These parameters control the test harness
//
// The message that the tests expect to receive from the upstream
// server
flag.StringVar(&upstreamMessage, "message", "Default message", "Upstream HTTP server response message")
// Time to wait between test request batches
flag.DurationVar(&delay, "delay", 500*time.Millisecond, "Interval between request batch retries")
// Each test loads a configuration snapshot into the control plane
// which is then picked up by Envoy. This parameter specifies how
// many snapshots to test
flag.IntVar(&updates, "u", 3, "Number of snapshot updates")
// Each snapshot test sends this many requests to the upstream
// server for each snapshot for each listener port
flag.IntVar(&requests, "r", 5, "Number of requests between snapshot updates")
// Test this many HTTP listeners per snapshot
flag.IntVar(&httpListeners, "http", 2, "Number of HTTP listeners (and RDS configs)")
// Test this many TCP listeners per snapshot
flag.IntVar(&tcpListeners, "tcp", 2, "Number of TCP pass-through listeners")
// Enable a muxed cache with partial snapshots
flag.BoolVar(&mux, "mux", false, "Enable muxed linear cache for EDS")
}
// main returns code 1 if any of the batches failed to pass all requests
func main() {
flag.Parse()
ctx := context.Background()
// create a cache
signal := make(chan struct{})
cbv2 := &testv2.Callbacks{Signal: signal, Debug: debug}
cbv3 := &testv3.Callbacks{Signal: signal, Debug: debug}
configv2 := cachev2.NewSnapshotCache(mode == resourcev2.Ads, cachev2.IDHash{}, logger{})
configv3 := cachev3.NewSnapshotCache(mode == resourcev2.Ads, cachev3.IDHash{}, logger{})
srv2 := serverv2.NewServer(context.Background(), configv2, cbv2)
// mux integration
var configCachev3 cachev3.Cache = configv3
typeURL := "type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment"
eds := cachev3.NewLinearCache(typeURL)
if mux {
configCachev3 = &cachev3.MuxCache{
Classify: func(req cachev3.Request) string {
if req.TypeUrl == typeURL {
return "eds"
}
return "default"
},
Caches: map[string]cachev3.Cache{
"default": configv3,
"eds": eds,
},
}
}
srv3 := serverv3.NewServer(context.Background(), configCachev3, cbv3)
alsv2 := &testv2.AccessLogService{}
alsv3 := &testv3.AccessLogService{}
// create a test snapshot
snapshotsv2 := resourcev2.TestSnapshot{
Xds: mode,
UpstreamPort: uint32(upstreamPort),
BasePort: uint32(basePort),
NumClusters: clusters,
NumHTTPListeners: httpListeners,
NumTCPListeners: tcpListeners,
TLS: tls,
NumRuntimes: runtimes,
}
snapshotsv3 := resourcev3.TestSnapshot{
Xds: mode,
UpstreamPort: uint32(upstreamPort),
BasePort: uint32(basePort),
NumClusters: clusters,
NumHTTPListeners: httpListeners,
NumTCPListeners: tcpListeners,
TLS: tls,
NumRuntimes: runtimes,
}
// start the xDS server
go test.RunAccessLogServer(ctx, alsv2, alsv3, alsPort)
go test.RunManagementServer(ctx, srv2, srv3, port)
go test.RunManagementGateway(ctx, srv2, srv3, gatewayPort, logger{})
log.Println("waiting for the first request...")
select {
case <-signal:
break
case <-time.After(1 * time.Minute):
log.Println("timeout waiting for the first request")
os.Exit(1)
}
log.Printf("initial snapshot %+v\n", snapshotsv2)
log.Printf("executing sequence updates=%d request=%d\n", updates, requests)
for i := 0; i < updates; i++ {
snapshotsv2.Version = fmt.Sprintf("v%d", i)
log.Printf("update snapshot %v\n", snapshotsv2.Version)
snapshotsv3.Version = fmt.Sprintf("v%d", i)
log.Printf("update snapshot %v\n", snapshotsv3.Version)
snapshotv2 := snapshotsv2.Generate()
snapshotv3 := snapshotsv3.Generate()
if err := snapshotv2.Consistent(); err != nil {
log.Printf("snapshot inconsistency: %+v\n", snapshotv2)
}
if err := snapshotv3.Consistent(); err != nil {
log.Printf("snapshot inconsistency: %+v\n", snapshotv3)
}
err := configv2.SetSnapshot(nodeID, snapshotv2)
if err != nil {
log.Printf("snapshot error %q for %+v\n", err, snapshotv2)
os.Exit(1)
}
err = configv3.SetSnapshot(nodeID, snapshotv3)
if err != nil {
log.Printf("snapshot error %q for %+v\n", err, snapshotv3)
os.Exit(1)
}
if mux {
for name, res := range snapshotv3.GetResources(typeURL) {
eds.UpdateResource(name, res)
}
}
// pass is true if all requests succeed at least once in a run
pass := false
for j := 0; j < requests; j++ {
ok, failed := callEcho()
if failed == 0 && !pass {
pass = true
}
log.Printf("request batch %d, ok %v, failed %v, pass %v\n", j, ok, failed, pass)
select {
case <-time.After(delay):
case <-ctx.Done():
return
}
}
alsv2.Dump(func(s string) {
if debug {
log.Println(s)
}
})
cbv2.Report()
alsv3.Dump(func(s string) {
if debug {
log.Println(s)
}
})
cbv3.Report()
if !pass {
log.Printf("failed all requests in a run %d\n", i)
os.Exit(1)
}
}
log.Printf("Test for %s passed!\n", mode)
}
// callEcho calls upstream echo service on all listener ports and returns an error
// if any of the listeners returned an error.
func callEcho() (int, int) {
total := httpListeners + tcpListeners
ok, failed := 0, 0
ch := make(chan error, total)
// spawn requests
for i := 0; i < total; i++ {
go func(i int) {
client := http.Client{
Timeout: 100 * time.Millisecond,
Transport: &http.Transport{
TLSClientConfig: &cryptotls.Config{InsecureSkipVerify: true},
},
}
proto := "http"
if tls {
proto = "https"
}
req, err := client.Get(fmt.Sprintf("%s://127.0.0.1:%d", proto, basePort+uint(i)))
if err != nil {
ch <- err
return
}
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
ch <- err
return
}
if string(body) != upstreamMessage {
ch <- fmt.Errorf("unexpected return %q", string(body))
return
}
ch <- nil
}(i)
}
for {
out := <-ch
if out == nil {
ok++
} else {
failed++
}
if ok+failed == total {
return ok, failed
}
}
}
type logger struct{}
func (logger logger) Debugf(format string, args ...interface{}) {
if debug {
log.Printf(format+"\n", args...)
}
}
func (logger logger) Infof(format string, args ...interface{}) {
if debug {
log.Printf(format+"\n", args...)
}
}
func (logger logger) Warnf(format string, args ...interface{}) {
log.Printf(format+"\n", args...)
}
func (logger logger) Errorf(format string, args ...interface{}) {
log.Printf(format+"\n", args...)
}