-
Notifications
You must be signed in to change notification settings - Fork 363
/
runner.go
192 lines (165 loc) · 6.26 KB
/
runner.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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package runner
import (
"context"
"crypto/tls"
"fmt"
"net"
"strconv"
"time"
clusterv3 "github.com/envoyproxy/go-control-plane/envoy/service/cluster/v3"
discoveryv3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
endpointv3 "github.com/envoyproxy/go-control-plane/envoy/service/endpoint/v3"
listenerv3 "github.com/envoyproxy/go-control-plane/envoy/service/listener/v3"
routev3 "github.com/envoyproxy/go-control-plane/envoy/service/route/v3"
runtimev3 "github.com/envoyproxy/go-control-plane/envoy/service/runtime/v3"
secretv3 "github.com/envoyproxy/go-control-plane/envoy/service/secret/v3"
serverv3 "github.com/envoyproxy/go-control-plane/pkg/server/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/crypto"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/message"
"github.com/envoyproxy/gateway/internal/xds/bootstrap"
"github.com/envoyproxy/gateway/internal/xds/cache"
xdstypes "github.com/envoyproxy/gateway/internal/xds/types"
)
const (
// XdsServerAddress is the listening address of the xds-server.
XdsServerAddress = "0.0.0.0"
// Default certificates path for envoy-gateway with Kubernetes provider.
// xdsTLSCertFilepath is the fully qualified path of the file containing the
// xDS server TLS certificate.
xdsTLSCertFilepath = "/certs/tls.crt"
// xdsTLSKeyFilepath is the fully qualified path of the file containing the
// xDS server TLS key.
xdsTLSKeyFilepath = "/certs/tls.key"
// xdsTLSCaFilepath is the fully qualified path of the file containing the
// xDS server trusted CA certificate.
xdsTLSCaFilepath = "/certs/ca.crt"
// TODO: Make these path configurable.
// Default certificates path for envoy-gateway with Host infrastructure provider.
localTLSCertFilepath = "/tmp/envoy-gateway/certs/envoy-gateway/tls.crt"
localTLSKeyFilepath = "/tmp/envoy-gateway/certs/envoy-gateway/tls.key"
localTLSCaFilepath = "/tmp/envoy-gateway/certs/envoy-gateway/ca.crt"
)
type Config struct {
config.Server
Xds *message.Xds
grpc *grpc.Server
cache cache.SnapshotCacheWithCallbacks
}
type Runner struct {
Config
}
func New(cfg *Config) *Runner {
return &Runner{Config: *cfg}
}
func (r *Runner) Name() string {
return string(egv1a1.LogComponentXdsServerRunner)
}
// Start starts the xds-server runner
func (r *Runner) Start(ctx context.Context) (err error) {
r.Logger = r.Logger.WithName(r.Name()).WithValues("runner", r.Name())
// Set up the gRPC server and register the xDS handler.
// Create SnapshotCache before start subscribeAndTranslate,
// prevent panics in case cache is nil.
tlsConfig, err := r.loadTLSConfig()
if err != nil {
return fmt.Errorf("failed to load TLS config: %w", err)
}
r.Logger.Info("loaded TLS certificate and key")
r.grpc = grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)), grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
MinTime: 15 * time.Second,
PermitWithoutStream: true,
}))
r.cache = cache.NewSnapshotCache(true, r.Logger)
registerServer(serverv3.NewServer(ctx, r.cache, r.cache), r.grpc)
// Start and listen xDS gRPC Server.
go r.serveXdsServer(ctx)
// Start message Subscription.
go r.subscribeAndTranslate(ctx)
r.Logger.Info("started")
return
}
func (r *Runner) serveXdsServer(ctx context.Context) {
addr := net.JoinHostPort(XdsServerAddress, strconv.Itoa(bootstrap.DefaultXdsServerPort))
l, err := net.Listen("tcp", addr)
if err != nil {
r.Logger.Error(err, "failed to listen on address", "address", addr)
return
}
go func() {
<-ctx.Done()
r.Logger.Info("grpc server shutting down")
// We don't use GracefulStop here because envoy
// has long-lived hanging xDS requests. There's no
// mechanism to make those pending requests fail,
// so we forcibly terminate the TCP sessions.
r.grpc.Stop()
}()
if err = r.grpc.Serve(l); err != nil {
r.Logger.Error(err, "failed to start grpc based xds server")
}
}
// registerServer registers the given xDS protocol Server with the gRPC
// runtime.
func registerServer(srv serverv3.Server, g *grpc.Server) {
// register services
discoveryv3.RegisterAggregatedDiscoveryServiceServer(g, srv)
secretv3.RegisterSecretDiscoveryServiceServer(g, srv)
clusterv3.RegisterClusterDiscoveryServiceServer(g, srv)
endpointv3.RegisterEndpointDiscoveryServiceServer(g, srv)
listenerv3.RegisterListenerDiscoveryServiceServer(g, srv)
routev3.RegisterRouteDiscoveryServiceServer(g, srv)
runtimev3.RegisterRuntimeDiscoveryServiceServer(g, srv)
}
func (r *Runner) subscribeAndTranslate(ctx context.Context) {
// Subscribe to resources
message.HandleSubscription(message.Metadata{Runner: string(egv1a1.LogComponentXdsServerRunner), Message: "xds"}, r.Xds.Subscribe(ctx),
func(update message.Update[string, *xdstypes.ResourceVersionTable], errChan chan error) {
key := update.Key
val := update.Value
r.Logger.Info("received an update")
var err error
if update.Delete {
err = r.cache.GenerateNewSnapshot(key, nil)
} else if val != nil && val.XdsResources != nil {
if r.cache == nil {
r.Logger.Error(err, "failed to init snapshot cache")
errChan <- err
} else {
// Update snapshot cache
err = r.cache.GenerateNewSnapshot(key, val.XdsResources)
}
}
if err != nil {
r.Logger.Error(err, "failed to generate a snapshot")
errChan <- err
}
},
)
r.Logger.Info("subscriber shutting down")
}
func (r *Runner) loadTLSConfig() (tlsConfig *tls.Config, err error) {
switch {
case r.EnvoyGateway.Provider.IsRunningOnKubernetes():
tlsConfig, err = crypto.LoadTLSConfig(xdsTLSCertFilepath, xdsTLSKeyFilepath, xdsTLSCaFilepath)
if err != nil {
return nil, fmt.Errorf("failed to create tls config: %w", err)
}
case r.EnvoyGateway.Provider.IsRunningOnHost():
tlsConfig, err = crypto.LoadTLSConfig(localTLSCertFilepath, localTLSKeyFilepath, localTLSCaFilepath)
if err != nil {
return nil, fmt.Errorf("failed to create tls config: %w", err)
}
default:
return nil, fmt.Errorf("no valid tls certificates")
}
return
}