-
Notifications
You must be signed in to change notification settings - Fork 444
/
gateway.go
486 lines (433 loc) · 16.3 KB
/
gateway.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package services
import (
"context"
"fmt"
"net"
"sync/atomic"
"time"
"github.com/solo-io/gloo/pkg/bootstrap/leaderelector/singlereplica"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/gloosnapshot"
"github.com/solo-io/gloo/pkg/utils/settingsutil"
"github.com/solo-io/gloo/pkg/utils/statusutils"
"github.com/solo-io/gloo/projects/gloo/pkg/syncer/setup"
"github.com/solo-io/gloo/projects/gateway/pkg/translator"
"github.com/solo-io/gloo/projects/gloo/pkg/upstreams/consul"
"github.com/solo-io/solo-kit/test/helpers"
"github.com/solo-io/solo-kit/pkg/api/external/kubernetes/service"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache"
skkube "github.com/solo-io/solo-kit/pkg/api/v1/resources/common/kubernetes"
corecache "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/factory"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/memory"
gatewayv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/gloo/projects/gloo/pkg/bootstrap"
"google.golang.org/grpc"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"go.uber.org/zap"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
. "github.com/onsi/gomega"
fds_syncer "github.com/solo-io/gloo/projects/discovery/pkg/fds/syncer"
uds_syncer "github.com/solo-io/gloo/projects/discovery/pkg/uds/syncer"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
"k8s.io/client-go/kubernetes"
)
type TestClients struct {
GatewayClient gatewayv1.GatewayClient
HttpGatewayClient gatewayv1.MatchableHttpGatewayClient
VirtualServiceClient gatewayv1.VirtualServiceClient
ProxyClient gloov1.ProxyClient
UpstreamClient gloov1.UpstreamClient
SecretClient gloov1.SecretClient
ArtifactClient gloov1.ArtifactClient
ServiceClient skkube.ServiceClient
GlooPort int
RestXdsPort int
}
// WriteSnapshot writes all resources in the ApiSnapshot to the cache
func (c TestClients) WriteSnapshot(ctx context.Context, snapshot *gloosnapshot.ApiSnapshot) error {
// We intentionally create child resources first to avoid having the validation webhook reject
// the parent resource
writeOptions := clients.WriteOpts{
Ctx: ctx,
OverwriteExisting: false,
}
for _, secret := range snapshot.Secrets {
if _, writeErr := c.SecretClient.Write(secret, writeOptions); writeErr != nil {
return writeErr
}
}
for _, artifact := range snapshot.Artifacts {
if _, writeErr := c.ArtifactClient.Write(artifact, writeOptions); writeErr != nil {
return writeErr
}
}
for _, us := range snapshot.Upstreams {
if _, writeErr := c.UpstreamClient.Write(us, writeOptions); writeErr != nil {
return writeErr
}
}
for _, vs := range snapshot.VirtualServices {
if _, writeErr := c.VirtualServiceClient.Write(vs, writeOptions); writeErr != nil {
return writeErr
}
}
for _, hgw := range snapshot.HttpGateways {
if _, writeErr := c.HttpGatewayClient.Write(hgw, writeOptions); writeErr != nil {
return writeErr
}
}
for _, gw := range snapshot.Gateways {
if _, writeErr := c.GatewayClient.Write(gw, writeOptions); writeErr != nil {
return writeErr
}
}
for _, proxy := range snapshot.Proxies {
if _, writeErr := c.ProxyClient.Write(proxy, writeOptions); writeErr != nil {
return writeErr
}
}
return nil
}
// DeleteSnapshot deletes all resources in the ApiSnapshot from the cache
func (c TestClients) DeleteSnapshot(ctx context.Context, snapshot *gloosnapshot.ApiSnapshot) error {
// We intentionally delete resources in the reverse order that we create resources
// If we delete child resources first, the validation webhook may reject the change
deleteOptions := clients.DeleteOpts{
Ctx: ctx,
IgnoreNotExist: true,
}
for _, gw := range snapshot.Gateways {
gwNamespace, gwName := gw.GetMetadata().Ref().Strings()
if deleteErr := c.GatewayClient.Delete(gwNamespace, gwName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, hgw := range snapshot.HttpGateways {
hgwNamespace, hgwName := hgw.GetMetadata().Ref().Strings()
if deleteErr := c.HttpGatewayClient.Delete(hgwNamespace, hgwName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, vs := range snapshot.VirtualServices {
vsNamespace, vsName := vs.GetMetadata().Ref().Strings()
if deleteErr := c.VirtualServiceClient.Delete(vsNamespace, vsName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, us := range snapshot.Upstreams {
usNamespace, usName := us.GetMetadata().Ref().Strings()
if deleteErr := c.UpstreamClient.Delete(usNamespace, usName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, artifact := range snapshot.Artifacts {
artifactNamespace, artifactName := artifact.GetMetadata().Ref().Strings()
if deleteErr := c.ArtifactClient.Delete(artifactNamespace, artifactName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, secret := range snapshot.Secrets {
secretNamespace, secretName := secret.GetMetadata().Ref().Strings()
if deleteErr := c.SecretClient.Delete(secretNamespace, secretName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
// Proxies are auto generated by Gateway resources
// Therefore we delete Proxies after we have deleted the resources that may regenerate a Proxy
for _, proxy := range snapshot.Proxies {
proxyNamespace, proxyName := proxy.GetMetadata().Ref().Strings()
if deleteErr := c.ProxyClient.Delete(proxyNamespace, proxyName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
return nil
}
var glooPortBase = int32(30400)
func AllocateGlooPort() int32 {
return atomic.AddInt32(&glooPortBase, 1) + int32(config.GinkgoConfig.ParallelNode*1000)
}
func RunGateway(ctx context.Context, justGloo bool) TestClients {
ns := defaults.GlooSystem
ro := &RunOptions{
NsToWrite: ns,
NsToWatch: []string{"default", ns},
WhatToRun: What{
DisableGateway: justGloo,
},
KubeClient: helpers.MustKubeClient(),
}
return RunGlooGatewayUdsFds(ctx, ro)
}
type What struct {
DisableGateway bool
DisableUds bool
DisableFds bool
}
type RunOptions struct {
NsToWrite string
NsToWatch []string
WhatToRun What
GlooPort int32
ValidationPort int32
RestXdsPort int32
Settings *gloov1.Settings
Cache memory.InMemoryResourceCache
KubeClient kubernetes.Interface
ConsulClient consul.ConsulWatcher
ConsulDnsAddress string
}
// noinspection GoUnhandledErrorResult
func RunGlooGatewayUdsFds(ctx context.Context, runOptions *RunOptions) TestClients {
if runOptions.GlooPort == 0 {
runOptions.GlooPort = AllocateGlooPort()
}
if runOptions.ValidationPort == 0 {
runOptions.ValidationPort = AllocateGlooPort()
}
if runOptions.RestXdsPort == 0 {
runOptions.RestXdsPort = AllocateGlooPort()
}
if runOptions.Cache == nil {
runOptions.Cache = memory.NewInMemoryResourceCache()
}
var settings *gloov1.Settings
if nil != runOptions.Settings { //capture any setting set by the test
settings = runOptions.Settings
} else { //we have no settings from testing - create a new setting struct to hold run option values
settings = &gloov1.Settings{}
}
//override needed settings for testing
settings.WatchNamespaces = runOptions.NsToWatch
settings.DiscoveryNamespace = runOptions.NsToWrite
ctx = settingsutil.WithSettings(ctx, settings)
glooOpts := defaultGlooOpts(ctx, runOptions)
glooOpts.ControlPlane.BindAddr.(*net.TCPAddr).Port = int(runOptions.GlooPort)
glooOpts.ValidationServer.BindAddr.(*net.TCPAddr).Port = int(runOptions.ValidationPort)
if glooOpts.Settings == nil {
glooOpts.Settings = &gloov1.Settings{}
}
if glooOpts.Settings.GetGloo() == nil {
glooOpts.Settings.Gloo = &gloov1.GlooOptions{}
}
if glooOpts.Settings.GetGloo().GetRestXdsBindAddr() == "" {
glooOpts.Settings.GetGloo().RestXdsBindAddr = fmt.Sprintf("%s:%d", net.IPv4zero.String(), runOptions.RestXdsPort)
}
glooOpts.ControlPlane.StartGrpcServer = true
glooOpts.ValidationServer.StartGrpcServer = true
glooOpts.GatewayControllerEnabled = !runOptions.WhatToRun.DisableGateway
go setup.RunGloo(glooOpts)
if !runOptions.WhatToRun.DisableFds {
go func() {
defer GinkgoRecover()
fds_syncer.RunFDS(glooOpts)
}()
}
if !runOptions.WhatToRun.DisableUds {
go func() {
defer GinkgoRecover()
uds_syncer.RunUDS(glooOpts)
}()
}
testClients := getTestClients(ctx, runOptions.Cache, glooOpts.KubeServiceClient)
testClients.GlooPort = int(runOptions.GlooPort)
testClients.RestXdsPort = int(runOptions.RestXdsPort)
return testClients
}
func getTestClients(ctx context.Context, cache memory.InMemoryResourceCache, serviceClient skkube.ServiceClient) TestClients {
// construct our own resources:
memFactory := &factory.MemoryResourceClientFactory{
Cache: cache,
}
gatewayClient, err := gatewayv1.NewGatewayClient(ctx, memFactory)
Expect(err).NotTo(HaveOccurred())
httpGatewayClient, err := gatewayv1.NewMatchableHttpGatewayClient(ctx, memFactory)
Expect(err).NotTo(HaveOccurred())
virtualServiceClient, err := gatewayv1.NewVirtualServiceClient(ctx, memFactory)
Expect(err).NotTo(HaveOccurred())
upstreamClient, err := gloov1.NewUpstreamClient(ctx, memFactory)
Expect(err).NotTo(HaveOccurred())
secretClient, err := gloov1.NewSecretClient(ctx, memFactory)
Expect(err).NotTo(HaveOccurred())
artifactClient, err := gloov1.NewArtifactClient(ctx, memFactory)
Expect(err).NotTo(HaveOccurred())
proxyClient, err := gloov1.NewProxyClient(ctx, memFactory)
Expect(err).NotTo(HaveOccurred())
return TestClients{
GatewayClient: gatewayClient,
HttpGatewayClient: httpGatewayClient,
VirtualServiceClient: virtualServiceClient,
UpstreamClient: upstreamClient,
SecretClient: secretClient,
ArtifactClient: artifactClient,
ProxyClient: proxyClient,
ServiceClient: serviceClient,
}
}
func defaultTestConstructOpts(ctx context.Context, runOptions *RunOptions) translator.Opts {
ctx = contextutils.WithLogger(ctx, "gateway")
f := &factory.MemoryResourceClientFactory{
Cache: runOptions.Cache,
}
meta := runOptions.Settings.GetMetadata()
var validation *translator.ValidationOpts
if runOptions.Settings.GetGateway().GetValidation().GetProxyValidationServerAddr() != "" {
if validation == nil {
validation = &translator.ValidationOpts{}
}
validation.ProxyValidationServerAddress = runOptions.Settings.GetGateway().GetValidation().GetProxyValidationServerAddr()
}
if runOptions.Settings.GetGateway().GetValidation().GetAllowWarnings() != nil {
if validation == nil {
validation = &translator.ValidationOpts{}
}
validation.AllowWarnings = runOptions.Settings.GetGateway().GetValidation().GetAllowWarnings().GetValue()
}
if runOptions.Settings.GetGateway().GetValidation().GetAlwaysAccept() != nil {
if validation == nil {
validation = &translator.ValidationOpts{}
}
validation.AlwaysAcceptResources = runOptions.Settings.GetGateway().GetValidation().GetAlwaysAccept().GetValue()
}
return translator.Opts{
GlooNamespace: meta.GetNamespace(),
WriteNamespace: runOptions.NsToWrite,
WatchNamespaces: runOptions.NsToWatch,
StatusReporterNamespace: statusutils.GetStatusReporterNamespaceOrDefault(defaults.GlooSystem),
Gateways: f,
MatchableHttpGateways: f,
VirtualServices: f,
RouteTables: f,
VirtualHostOptions: f,
RouteOptions: f,
Proxies: f,
WatchOpts: clients.WatchOpts{
Ctx: ctx,
RefreshRate: time.Minute,
},
Validation: validation,
DevMode: false,
ConfigStatusMetricOpts: runOptions.Settings.GetObservabilityOptions().GetConfigStatusMetricLabels(),
}
}
func defaultGlooOpts(ctx context.Context, runOptions *RunOptions) bootstrap.Opts {
ctx = contextutils.WithLogger(ctx, "gloo")
logger := contextutils.LoggerFrom(ctx)
grpcServer := grpc.NewServer(grpc.StreamInterceptor(
grpc_middleware.ChainStreamServer(
grpc_ctxtags.StreamServerInterceptor(),
grpc_zap.StreamServerInterceptor(zap.NewNop()),
func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
logger.Infof("gRPC call: %v", info.FullMethod)
return handler(srv, ss)
},
)),
)
grpcServerValidation := grpc.NewServer(grpc.StreamInterceptor(
grpc_middleware.ChainStreamServer(
grpc_ctxtags.StreamServerInterceptor(),
grpc_zap.StreamServerInterceptor(zap.NewNop()),
func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
logger.Infof("gRPC call: %v", info.FullMethod)
return handler(srv, ss)
},
)),
)
f := &factory.MemoryResourceClientFactory{
Cache: runOptions.Cache,
}
var kubeCoreCache corecache.KubeCoreCache
if runOptions.KubeClient != nil {
var err error
kubeCoreCache, err = cache.NewKubeCoreCacheWithOptions(ctx, runOptions.KubeClient, time.Hour, runOptions.NsToWatch)
Expect(err).NotTo(HaveOccurred())
}
var validationOpts *translator.ValidationOpts
if runOptions.Settings.GetGateway().GetValidation().GetProxyValidationServerAddr() != "" {
if validationOpts == nil {
validationOpts = &translator.ValidationOpts{}
}
validationOpts.ProxyValidationServerAddress = runOptions.Settings.GetGateway().GetValidation().GetProxyValidationServerAddr()
}
if runOptions.Settings.GetGateway().GetValidation().GetAllowWarnings() != nil {
if validationOpts == nil {
validationOpts = &translator.ValidationOpts{}
}
validationOpts.AllowWarnings = runOptions.Settings.GetGateway().GetValidation().GetAllowWarnings().GetValue()
}
if runOptions.Settings.GetGateway().GetValidation().GetAlwaysAccept() != nil {
if validationOpts == nil {
validationOpts = &translator.ValidationOpts{}
}
validationOpts.AlwaysAcceptResources = runOptions.Settings.GetGateway().GetValidation().GetAlwaysAccept().GetValue()
}
return bootstrap.Opts{
Settings: runOptions.Settings,
WriteNamespace: runOptions.NsToWrite,
StatusReporterNamespace: statusutils.GetStatusReporterNamespaceOrDefault(defaults.GlooSystem),
Upstreams: f,
UpstreamGroups: f,
Proxies: f,
Secrets: f,
Artifacts: f,
AuthConfigs: f,
RateLimitConfigs: f,
GraphQLApis: f,
Gateways: f,
MatchableHttpGateways: f,
VirtualServices: f,
RouteTables: f,
RouteOptions: f,
VirtualHostOptions: f,
KubeServiceClient: newServiceClient(ctx, f, runOptions),
WatchNamespaces: runOptions.NsToWatch,
WatchOpts: clients.WatchOpts{
Ctx: ctx,
RefreshRate: time.Second / 10,
},
ControlPlane: setup.NewControlPlane(ctx, grpcServer, &net.TCPAddr{
IP: net.IPv4zero,
Port: 8081,
}, nil, true),
ValidationServer: setup.NewValidationServer(ctx, grpcServerValidation, &net.TCPAddr{
IP: net.IPv4zero,
Port: 8081,
}, true),
ProxyDebugServer: setup.NewProxyDebugServer(ctx, grpcServer, &net.TCPAddr{
IP: net.IPv4zero,
Port: 8001,
}, false),
KubeClient: runOptions.KubeClient,
KubeCoreCache: kubeCoreCache,
DevMode: true,
Consul: bootstrap.Consul{
ConsulWatcher: runOptions.ConsulClient,
DnsServer: runOptions.ConsulDnsAddress,
},
GatewayControllerEnabled: true,
ValidationOpts: validationOpts,
Identity: singlereplica.Identity(),
}
}
func newServiceClient(ctx context.Context, memFactory *factory.MemoryResourceClientFactory, runOpts *RunOptions) skkube.ServiceClient {
// If the KubeClient option is set, the kubernetes discovery plugin will be activated and we must provide a
// kubernetes service client in order for service-derived upstreams to be included in the snapshot
if kube := runOpts.KubeClient; kube != nil {
kubeCache, err := cache.NewKubeCoreCache(ctx, kube)
if err != nil {
panic(err)
}
return service.NewServiceClient(kube, kubeCache)
}
// Else return in-memory client
client, err := skkube.NewServiceClient(ctx, memFactory)
if err != nil {
panic(err)
}
return client
}